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