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