]> git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/testHttpRequestMethod.cc
57c4e819c8b43e6d15d1b7796d26d571a611d57a
[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 /*
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 -> METHOD_NONE */
24 CPPUNIT_ASSERT(HttpRequestMethod(NULL,NULL) == METHOD_NONE);
25 /* parsing a literal should work */
26 CPPUNIT_ASSERT(HttpRequestMethod("GET", NULL) == METHOD_GET);
27 CPPUNIT_ASSERT(HttpRequestMethod("QWERTY", NULL) == 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 -> METHOD_NONE */
38 CPPUNIT_ASSERT(HttpRequestMethod(NULL, NULL) == METHOD_NONE);
39 /* parsing a literal should work */
40 CPPUNIT_ASSERT(HttpRequestMethod("GET", NULL) == METHOD_GET);
41 /* parsing with an explicit end should work */
42 buffer = "POSTPLUS";
43 CPPUNIT_ASSERT(HttpRequestMethod(buffer, buffer + 4) == METHOD_POST);
44 }
45
46 /*
47 * we should be able to assign a method_t to a HttpRequestMethod
48 */
49 void
50 testHttpRequestMethod::testAssignFrommethod_t()
51 {
52 HttpRequestMethod method;
53 method = METHOD_NONE;
54 CPPUNIT_ASSERT_EQUAL(HttpRequestMethod(METHOD_NONE), method);
55 method = METHOD_POST;
56 CPPUNIT_ASSERT_EQUAL(HttpRequestMethod(METHOD_POST), method);
57 }
58
59 /*
60 * a default constructed HttpRequestMethod is == METHOD_NONE
61 */
62 void
63 testHttpRequestMethod::testDefaultConstructor()
64 {
65 HttpRequestMethod lhs;
66 HttpRequestMethod rhs(METHOD_NONE);
67 CPPUNIT_ASSERT_EQUAL(lhs, rhs);
68 }
69
70 /*
71 * we should be able to construct a HttpRequestMethod from a method_t
72 */
73 void
74 testHttpRequestMethod::testConstructmethod_t()
75 {
76 CPPUNIT_ASSERT_EQUAL(HttpRequestMethod(METHOD_NONE), HttpRequestMethod(METHOD_NONE));
77 CPPUNIT_ASSERT_EQUAL(HttpRequestMethod(METHOD_POST), HttpRequestMethod(METHOD_POST));
78 CPPUNIT_ASSERT(HttpRequestMethod(METHOD_NONE) != HttpRequestMethod(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 CPPUNIT_ASSERT_EQUAL(String("POST"), String(HttpRequestMethod("post",NULL).image()));
88 }
89
90 /*
91 * an HttpRequestMethod should be comparable to a method_t without false
92 * matches
93 */
94 void
95 testHttpRequestMethod::testEqualmethod_t()
96 {
97 CPPUNIT_ASSERT(HttpRequestMethod(METHOD_NONE) == METHOD_NONE);
98 CPPUNIT_ASSERT(not (HttpRequestMethod(METHOD_POST) == METHOD_GET));
99 CPPUNIT_ASSERT(HttpRequestMethod(METHOD_GET) == METHOD_GET);
100 CPPUNIT_ASSERT(not (HttpRequestMethod(METHOD_TRACE) == METHOD_SEARCH));
101 }
102
103 /*
104 * an HttpRequestMethod should testable for inequality without fail maatches
105 */
106 void
107 testHttpRequestMethod::testNotEqualmethod_t()
108 {
109 CPPUNIT_ASSERT(HttpRequestMethod(METHOD_NONE) != METHOD_GET);
110 CPPUNIT_ASSERT(not (HttpRequestMethod(METHOD_POST) != METHOD_POST));
111 CPPUNIT_ASSERT(HttpRequestMethod(METHOD_GET) != METHOD_NONE);
112 CPPUNIT_ASSERT(not (HttpRequestMethod(METHOD_SEARCH) != METHOD_SEARCH));
113 }
114
115 /*
116 * we should be able to send it to a stream and get the normalised version
117 */
118 void
119 testHttpRequestMethod::testStream()
120 {
121 std::ostringstream buffer;
122 buffer << HttpRequestMethod("get",NULL);
123 CPPUNIT_ASSERT_EQUAL(String("GET"), String(buffer.str().c_str()));
124 }