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