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