]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/testHttpRequestMethod.cc
2 * Copyright (C) 1996-2018 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
10 #include <cppunit/TestAssert.h>
12 #include "http/RequestMethod.h"
13 #include "SquidConfig.h"
14 #include "testHttpRequestMethod.h"
18 CPPUNIT_TEST_SUITE_REGISTRATION( testHttpRequestMethod
);
21 * We should be able to make an HttpRequestMethod straight from a string.
24 testHttpRequestMethod::testConstructCharStart()
28 /* parse an empty string -> Http::METHOD_NONE */
29 CPPUNIT_ASSERT(HttpRequestMethod(SBuf()) == Http::METHOD_NONE
);
31 /* parsing a literal should work */
32 CPPUNIT_ASSERT(HttpRequestMethod(SBuf("GET")) == Http::METHOD_GET
);
33 CPPUNIT_ASSERT(HttpRequestMethod(SBuf("QWERTY")) == Http::METHOD_OTHER
);
37 /* parse an empty string -> Http::METHOD_NONE */
39 a
.HttpRequestMethodXXX(NULL
);
40 CPPUNIT_ASSERT(a
== Http::METHOD_NONE
);
42 /* parsing a literal should work */
44 b
.HttpRequestMethodXXX("GET");
45 CPPUNIT_ASSERT(b
== Http::METHOD_GET
);
46 CPPUNIT_ASSERT_EQUAL(SBuf("GET"), b
.image());
48 c
.HttpRequestMethodXXX("QWERTY");
49 CPPUNIT_ASSERT(c
== Http::METHOD_OTHER
);
50 CPPUNIT_ASSERT_EQUAL(SBuf("QWERTY"), c
.image());
52 // parsing error should not leave stale results
53 b
.HttpRequestMethodXXX(NULL
);
54 CPPUNIT_ASSERT(b
== Http::METHOD_NONE
);
55 CPPUNIT_ASSERT_EQUAL(SBuf("NONE"), b
.image());
59 * We can also parse precise ranges of characters with SBuf
62 testHttpRequestMethod::testConstructCharStartEnd()
65 /* parse an empty string -> Http::METHOD_NONE */
66 CPPUNIT_ASSERT(HttpRequestMethod(SBuf()) == Http::METHOD_NONE
);
67 /* parsing a literal should work */
68 CPPUNIT_ASSERT(HttpRequestMethod(SBuf("GET")) == Http::METHOD_GET
);
69 /* parsing with an explicit end should work */
71 CPPUNIT_ASSERT(HttpRequestMethod(SBuf(buffer
, 4)) == Http::METHOD_POST
);
75 * we should be able to assign a Http::MethodType to a HttpRequestMethod
78 testHttpRequestMethod::testAssignFrommethod_t()
80 HttpRequestMethod method
;
81 method
= Http::METHOD_NONE
;
82 CPPUNIT_ASSERT_EQUAL(HttpRequestMethod(Http::METHOD_NONE
), method
);
83 method
= Http::METHOD_POST
;
84 CPPUNIT_ASSERT_EQUAL(HttpRequestMethod(Http::METHOD_POST
), method
);
88 * a default constructed HttpRequestMethod is == Http::METHOD_NONE
91 testHttpRequestMethod::testDefaultConstructor()
93 HttpRequestMethod lhs
;
94 HttpRequestMethod
rhs(Http::METHOD_NONE
);
95 CPPUNIT_ASSERT_EQUAL(lhs
, rhs
);
99 * we should be able to construct a HttpRequestMethod from a Http::MethodType
102 testHttpRequestMethod::testConstructmethod_t()
104 CPPUNIT_ASSERT_EQUAL(HttpRequestMethod(Http::METHOD_NONE
), HttpRequestMethod(Http::METHOD_NONE
));
105 CPPUNIT_ASSERT_EQUAL(HttpRequestMethod(Http::METHOD_POST
), HttpRequestMethod(Http::METHOD_POST
));
106 CPPUNIT_ASSERT(HttpRequestMethod(Http::METHOD_NONE
) != HttpRequestMethod(Http::METHOD_POST
));
110 * we should be able to get a char const * version of the method.
113 testHttpRequestMethod::testImage()
115 // relaxed RFC-compliance parse HTTP methods are upgraded to correct case
116 Config
.onoff
.relaxed_header_parser
= 1;
117 CPPUNIT_ASSERT_EQUAL(SBuf("POST"), HttpRequestMethod(SBuf("POST")).image());
118 CPPUNIT_ASSERT_EQUAL(SBuf("POST"), HttpRequestMethod(SBuf("pOsT")).image());
119 CPPUNIT_ASSERT_EQUAL(SBuf("POST"), HttpRequestMethod(SBuf("post")).image());
121 // strict RFC-compliance parse HTTP methods are case sensitive
122 Config
.onoff
.relaxed_header_parser
= 0;
123 CPPUNIT_ASSERT_EQUAL(SBuf("POST"), HttpRequestMethod(SBuf("POST")).image());
124 CPPUNIT_ASSERT_EQUAL(SBuf("pOsT"), HttpRequestMethod(SBuf("pOsT")).image());
125 CPPUNIT_ASSERT_EQUAL(SBuf("post"), HttpRequestMethod(SBuf("post")).image());
129 * an HttpRequestMethod should be comparable to a Http::MethodType without false
133 testHttpRequestMethod::testEqualmethod_t()
135 CPPUNIT_ASSERT(HttpRequestMethod(Http::METHOD_NONE
) == Http::METHOD_NONE
);
136 CPPUNIT_ASSERT(not (HttpRequestMethod(Http::METHOD_POST
) == Http::METHOD_GET
));
137 CPPUNIT_ASSERT(HttpRequestMethod(Http::METHOD_GET
) == Http::METHOD_GET
);
138 CPPUNIT_ASSERT(not (HttpRequestMethod(Http::METHOD_TRACE
) == Http::METHOD_SEARCH
));
142 * an HttpRequestMethod should testable for inequality without fail maatches
145 testHttpRequestMethod::testNotEqualmethod_t()
147 CPPUNIT_ASSERT(HttpRequestMethod(Http::METHOD_NONE
) != Http::METHOD_GET
);
148 CPPUNIT_ASSERT(not (HttpRequestMethod(Http::METHOD_POST
) != Http::METHOD_POST
));
149 CPPUNIT_ASSERT(HttpRequestMethod(Http::METHOD_GET
) != Http::METHOD_NONE
);
150 CPPUNIT_ASSERT(not (HttpRequestMethod(Http::METHOD_SEARCH
) != Http::METHOD_SEARCH
));
154 * we should be able to send it to a stream and get the normalised version
157 testHttpRequestMethod::testStream()
159 // relaxed RFC-compliance parse HTTP methods are upgraded to correct case
160 Config
.onoff
.relaxed_header_parser
= 1;
161 std::ostringstream buffer
;
162 buffer
<< HttpRequestMethod(SBuf("get"));
163 CPPUNIT_ASSERT_EQUAL(String("GET"), String(buffer
.str().c_str()));
165 // strict RFC-compliance parse HTTP methods are case sensitive
166 Config
.onoff
.relaxed_header_parser
= 0;
167 std::ostringstream buffer2
;
168 buffer2
<< HttpRequestMethod(SBuf("get"));
169 CPPUNIT_ASSERT_EQUAL(String("get"), String(buffer2
.str().c_str()));