]> git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/testHttpRequestMethod.cc
Merge from trunk
[thirdparty/squid.git] / src / tests / testHttpRequestMethod.cc
1 #define SQUID_UNIT_TEST 1
2
3 #include "squid.h"
4 #include <cppunit/TestAssert.h>
5
6 #include "http/RequestMethod.h"
7 #include "Mem.h"
8 #include "SquidConfig.h"
9 #include "testHttpRequestMethod.h"
10
11 #if HAVE_SSTREAM
12 #include <sstream>
13 #endif
14
15 CPPUNIT_TEST_SUITE_REGISTRATION( testHttpRequestMethod );
16
17 /*
18 * We should be able to make an HttpRequestMethod straight from a string.
19 */
20 void
21 testHttpRequestMethod::testConstructCharStart()
22 {
23 /* parse an empty string -> Http::METHOD_NONE */
24 CPPUNIT_ASSERT(HttpRequestMethod(NULL,NULL) == Http::METHOD_NONE);
25 /* parsing a literal should work */
26 CPPUNIT_ASSERT(HttpRequestMethod("GET", NULL) == Http::METHOD_GET);
27 CPPUNIT_ASSERT(HttpRequestMethod("QWERTY", NULL) == Http::METHOD_OTHER);
28 }
29
30 /*
31 * We can also parse precise ranges of characters
32 */
33 void
34 testHttpRequestMethod::testConstructCharStartEnd()
35 {
36 char const * buffer;
37 /* parse an empty string -> Http::METHOD_NONE */
38 CPPUNIT_ASSERT(HttpRequestMethod(NULL, NULL) == Http::METHOD_NONE);
39 /* parsing a literal should work */
40 CPPUNIT_ASSERT(HttpRequestMethod("GET", NULL) == Http::METHOD_GET);
41 /* parsing with an explicit end should work */
42 buffer = "POSTPLUS";
43 CPPUNIT_ASSERT(HttpRequestMethod(buffer, buffer + 4) == Http::METHOD_POST);
44 }
45
46 /*
47 * we should be able to assign a Http::MethodType to a HttpRequestMethod
48 */
49 void
50 testHttpRequestMethod::testAssignFrommethod_t()
51 {
52 HttpRequestMethod method;
53 method = Http::METHOD_NONE;
54 CPPUNIT_ASSERT_EQUAL(HttpRequestMethod(Http::METHOD_NONE), method);
55 method = Http::METHOD_POST;
56 CPPUNIT_ASSERT_EQUAL(HttpRequestMethod(Http::METHOD_POST), method);
57 }
58
59 /*
60 * a default constructed HttpRequestMethod is == Http::METHOD_NONE
61 */
62 void
63 testHttpRequestMethod::testDefaultConstructor()
64 {
65 HttpRequestMethod lhs;
66 HttpRequestMethod rhs(Http::METHOD_NONE);
67 CPPUNIT_ASSERT_EQUAL(lhs, rhs);
68 }
69
70 /*
71 * we should be able to construct a HttpRequestMethod from a Http::MethodType
72 */
73 void
74 testHttpRequestMethod::testConstructmethod_t()
75 {
76 CPPUNIT_ASSERT_EQUAL(HttpRequestMethod(Http::METHOD_NONE), HttpRequestMethod(Http::METHOD_NONE));
77 CPPUNIT_ASSERT_EQUAL(HttpRequestMethod(Http::METHOD_POST), HttpRequestMethod(Http::METHOD_POST));
78 CPPUNIT_ASSERT(HttpRequestMethod(Http::METHOD_NONE) != HttpRequestMethod(Http::METHOD_POST));
79 }
80
81 /*
82 * we should be able to get a char const * version of the method.
83 */
84 void
85 testHttpRequestMethod::testImage()
86 {
87 // relaxed RFC-compliance parse HTTP methods are upgraded to correct case
88 Config.onoff.relaxed_header_parser = 1;
89 CPPUNIT_ASSERT_EQUAL(String("POST"), String(HttpRequestMethod("POST",NULL).image()));
90 CPPUNIT_ASSERT_EQUAL(String("POST"), String(HttpRequestMethod("pOsT",NULL).image()));
91 CPPUNIT_ASSERT_EQUAL(String("POST"), String(HttpRequestMethod("post",NULL).image()));
92
93 // strict RFC-compliance parse HTTP methods are case sensitive
94 Config.onoff.relaxed_header_parser = 0;
95 CPPUNIT_ASSERT_EQUAL(String("POST"), String(HttpRequestMethod("POST",NULL).image()));
96 CPPUNIT_ASSERT_EQUAL(String("pOsT"), String(HttpRequestMethod("pOsT",NULL).image()));
97 CPPUNIT_ASSERT_EQUAL(String("post"), String(HttpRequestMethod("post",NULL).image()));
98 }
99
100 /*
101 * an HttpRequestMethod should be comparable to a Http::MethodType without false
102 * matches
103 */
104 void
105 testHttpRequestMethod::testEqualmethod_t()
106 {
107 CPPUNIT_ASSERT(HttpRequestMethod(Http::METHOD_NONE) == Http::METHOD_NONE);
108 CPPUNIT_ASSERT(not (HttpRequestMethod(Http::METHOD_POST) == Http::METHOD_GET));
109 CPPUNIT_ASSERT(HttpRequestMethod(Http::METHOD_GET) == Http::METHOD_GET);
110 CPPUNIT_ASSERT(not (HttpRequestMethod(Http::METHOD_TRACE) == Http::METHOD_SEARCH));
111 }
112
113 /*
114 * an HttpRequestMethod should testable for inequality without fail maatches
115 */
116 void
117 testHttpRequestMethod::testNotEqualmethod_t()
118 {
119 CPPUNIT_ASSERT(HttpRequestMethod(Http::METHOD_NONE) != Http::METHOD_GET);
120 CPPUNIT_ASSERT(not (HttpRequestMethod(Http::METHOD_POST) != Http::METHOD_POST));
121 CPPUNIT_ASSERT(HttpRequestMethod(Http::METHOD_GET) != Http::METHOD_NONE);
122 CPPUNIT_ASSERT(not (HttpRequestMethod(Http::METHOD_SEARCH) != Http::METHOD_SEARCH));
123 }
124
125 /*
126 * we should be able to send it to a stream and get the normalised version
127 */
128 void
129 testHttpRequestMethod::testStream()
130 {
131 // relaxed RFC-compliance parse HTTP methods are upgraded to correct case
132 Config.onoff.relaxed_header_parser = 1;
133 std::ostringstream buffer;
134 buffer << HttpRequestMethod("get", NULL);
135 CPPUNIT_ASSERT_EQUAL(String("GET"), String(buffer.str().c_str()));
136
137 // strict RFC-compliance parse HTTP methods are case sensitive
138 Config.onoff.relaxed_header_parser = 0;
139 std::ostringstream buffer2;
140 buffer2 << HttpRequestMethod("get", NULL);
141 CPPUNIT_ASSERT_EQUAL(String("get"), String(buffer2.str().c_str()));
142 }