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