]> git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/testUriScheme.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / tests / testUriScheme.cc
1 /*
2 * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #include "squid.h"
10
11 #include <cppunit/TestAssert.h>
12
13 #include "anyp/UriScheme.h"
14 #include "SquidString.h"
15 #include "tests/testUriScheme.h"
16
17 #include <sstream>
18
19 CPPUNIT_TEST_SUITE_REGISTRATION( testUriScheme );
20
21 #if 0
22 /*
23 * We should be able to make an HttpRequestMethod straight from a string.
24 */
25 void
26 testHttpRequestMethod::testConstructCharStart()
27 {
28 /* parse an empty string -> METHOD_NONE */
29 CPPUNIT_ASSERT(METHOD_NONE == HttpRequestMethod(NULL));
30 /* parsing a literal should work */
31 CPPUNIT_ASSERT(METHOD_GET == HttpRequestMethod("GET", NULL));
32 }
33
34 /*
35 * We can also parse precise ranges of characters
36 */
37 void
38 testHttpRequestMethod::testConstructCharStartEnd()
39 {
40 char const * buffer;
41 /* parse an empty string -> METHOD_NONE */
42 CPPUNIT_ASSERT(METHOD_NONE == HttpRequestMethod(NULL, NULL));
43 /* parsing a literal should work */
44 CPPUNIT_ASSERT(METHOD_GET == HttpRequestMethod("GET", NULL));
45 /* parsing with an explicit end should work */
46 buffer = "POSTPLUS";
47 CPPUNIT_ASSERT(METHOD_POST == HttpRequestMethod(buffer, buffer + 4));
48 }
49
50 #endif
51
52 /*
53 * we should be able to assign a protocol_t to a AnyP::UriScheme for ease
54 * of code conversion
55 */
56 void
57 testUriScheme::testAssignFromprotocol_t()
58 {
59 AnyP::UriScheme empty_scheme;
60 AnyP::UriScheme scheme;
61 scheme = AnyP::PROTO_NONE;
62 CPPUNIT_ASSERT_EQUAL(empty_scheme, scheme);
63
64 AnyP::UriScheme https_scheme(AnyP::PROTO_HTTPS);
65 scheme = AnyP::PROTO_HTTPS;
66 CPPUNIT_ASSERT_EQUAL(https_scheme, scheme);
67 }
68
69 /*
70 * We should be able to get a protocol_t from a AnyP::UriScheme for ease
71 * of migration
72 */
73 void
74 testUriScheme::testCastToprotocol_t()
75 {
76 /* explicit cast */
77 AnyP::ProtocolType protocol = static_cast<AnyP::ProtocolType>(AnyP::UriScheme());
78 CPPUNIT_ASSERT_EQUAL(AnyP::PROTO_NONE, protocol);
79 /* and implicit */
80 protocol = AnyP::UriScheme(AnyP::PROTO_HTTP);
81 CPPUNIT_ASSERT_EQUAL(AnyP::PROTO_HTTP, protocol);
82 }
83
84 /*
85 * a default constructed AnyP::UriScheme is == AnyP::PROTO_NONE
86 */
87 void
88 testUriScheme::testDefaultConstructor()
89 {
90 AnyP::UriScheme lhs;
91 AnyP::UriScheme rhs(AnyP::PROTO_NONE);
92 CPPUNIT_ASSERT_EQUAL(lhs, rhs);
93 }
94
95 /*
96 * we should be able to construct a AnyP::UriScheme from the old 'protocol_t' enum.
97 */
98 void
99 testUriScheme::testConstructprotocol_t()
100 {
101 AnyP::UriScheme lhs_none(AnyP::PROTO_NONE), rhs_none(AnyP::PROTO_NONE);
102 CPPUNIT_ASSERT_EQUAL(lhs_none, rhs_none);
103
104 AnyP::UriScheme lhs_cacheobj(AnyP::PROTO_CACHE_OBJECT), rhs_cacheobj(AnyP::PROTO_CACHE_OBJECT);
105 CPPUNIT_ASSERT_EQUAL(lhs_cacheobj, rhs_cacheobj);
106 CPPUNIT_ASSERT(lhs_none != rhs_cacheobj);
107 }
108
109 /*
110 * we should be able to get a char const * version of the method.
111 */
112 void
113 testUriScheme::testC_str()
114 {
115 SBuf lhs("wais");
116 AnyP::UriScheme wais(AnyP::PROTO_WAIS);
117 SBuf rhs(wais.image());
118 CPPUNIT_ASSERT_EQUAL(lhs, rhs);
119 }
120
121 /*
122 * a AnyP::UriScheme replaces protocol_t, so we should be able to test for equality on
123 * either the left or right hand side seamlessly.
124 */
125 void
126 testUriScheme::testEqualprotocol_t()
127 {
128 CPPUNIT_ASSERT(AnyP::UriScheme() == AnyP::PROTO_NONE);
129 CPPUNIT_ASSERT(not (AnyP::UriScheme(AnyP::PROTO_WAIS) == AnyP::PROTO_HTTP));
130 CPPUNIT_ASSERT(AnyP::PROTO_HTTP == AnyP::UriScheme(AnyP::PROTO_HTTP));
131 CPPUNIT_ASSERT(not (AnyP::PROTO_CACHE_OBJECT == AnyP::UriScheme(AnyP::PROTO_HTTP)));
132 }
133
134 /*
135 * a AnyP::UriScheme should testable for inequality with a protocol_t.
136 */
137 void
138 testUriScheme::testNotEqualprotocol_t()
139 {
140 CPPUNIT_ASSERT(AnyP::UriScheme(AnyP::PROTO_NONE) != AnyP::PROTO_HTTP);
141 CPPUNIT_ASSERT(not (AnyP::UriScheme(AnyP::PROTO_HTTP) != AnyP::PROTO_HTTP));
142 CPPUNIT_ASSERT(AnyP::PROTO_NONE != AnyP::UriScheme(AnyP::PROTO_HTTP));
143 CPPUNIT_ASSERT(not (AnyP::PROTO_WAIS != AnyP::UriScheme(AnyP::PROTO_WAIS)));
144 }
145
146 /*
147 * we should be able to send it to a stream and get the normalised version
148 */
149 void
150 testUriScheme::testStream()
151 {
152 std::ostringstream buffer;
153 buffer << AnyP::UriScheme(AnyP::PROTO_HTTP);
154 String http_str("http");
155 String from_buf(buffer.str().c_str());
156 CPPUNIT_ASSERT_EQUAL(http_str, from_buf);
157 }
158