]> git.ipfire.org Git - thirdparty/squid.git/blame - src/tests/testHttpRequestMethod.cc
Source Format Enforcement (#1234)
[thirdparty/squid.git] / src / tests / testHttpRequestMethod.cc
CommitLineData
4e0938ef 1/*
b8ae064d 2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
4e0938ef
AJ
3 *
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.
7 */
ec94e362 8
582c2af2 9#include "squid.h"
985c86bc 10#include <cppunit/TestAssert.h>
11
f35c0145 12#include "http/RequestMethod.h"
35efdb9b 13#include "SquidConfig.h"
985c86bc 14#include "testHttpRequestMethod.h"
985c86bc 15
27e059d4 16#include <sstream>
985c86bc 17
18CPPUNIT_TEST_SUITE_REGISTRATION( testHttpRequestMethod );
19
985c86bc 20/*
21 * We should be able to make an HttpRequestMethod straight from a string.
22 */
23void
24testHttpRequestMethod::testConstructCharStart()
25{
f9688132
AJ
26 // string in SBuf
27
28 /* parse an empty string -> Http::METHOD_NONE */
29 CPPUNIT_ASSERT(HttpRequestMethod(SBuf()) == Http::METHOD_NONE);
30
31 /* parsing a literal should work */
32 CPPUNIT_ASSERT(HttpRequestMethod(SBuf("GET")) == Http::METHOD_GET);
33 CPPUNIT_ASSERT(HttpRequestMethod(SBuf("QWERTY")) == Http::METHOD_OTHER);
34
35 // string in char*
36
c2a7cefd 37 /* parse an empty string -> Http::METHOD_NONE */
f9688132 38 HttpRequestMethod a;
aee3523a 39 a.HttpRequestMethodXXX(nullptr);
f9688132
AJ
40 CPPUNIT_ASSERT(a == Http::METHOD_NONE);
41
985c86bc 42 /* parsing a literal should work */
f9688132
AJ
43 HttpRequestMethod b;
44 b.HttpRequestMethodXXX("GET");
45 CPPUNIT_ASSERT(b == Http::METHOD_GET);
46 CPPUNIT_ASSERT_EQUAL(SBuf("GET"), b.image());
47 HttpRequestMethod c;
48 c.HttpRequestMethodXXX("QWERTY");
49 CPPUNIT_ASSERT(c == Http::METHOD_OTHER);
50 CPPUNIT_ASSERT_EQUAL(SBuf("QWERTY"), c.image());
51
52 // parsing error should not leave stale results
aee3523a 53 b.HttpRequestMethodXXX(nullptr);
f9688132
AJ
54 CPPUNIT_ASSERT(b == Http::METHOD_NONE);
55 CPPUNIT_ASSERT_EQUAL(SBuf("NONE"), b.image());
985c86bc 56}
57
58/*
f9688132 59 * We can also parse precise ranges of characters with SBuf
985c86bc 60 */
61void
62testHttpRequestMethod::testConstructCharStartEnd()
63{
64 char const * buffer;
c2a7cefd 65 /* parse an empty string -> Http::METHOD_NONE */
f9688132 66 CPPUNIT_ASSERT(HttpRequestMethod(SBuf()) == Http::METHOD_NONE);
985c86bc 67 /* parsing a literal should work */
f9688132 68 CPPUNIT_ASSERT(HttpRequestMethod(SBuf("GET")) == Http::METHOD_GET);
985c86bc 69 /* parsing with an explicit end should work */
70 buffer = "POSTPLUS";
7a4fa6a0 71 CPPUNIT_ASSERT(HttpRequestMethod(SBuf(buffer, 4)) == Http::METHOD_POST);
985c86bc 72}
73
74/*
c2a7cefd 75 * we should be able to assign a Http::MethodType to a HttpRequestMethod
985c86bc 76 */
77void
78testHttpRequestMethod::testAssignFrommethod_t()
79{
80 HttpRequestMethod method;
c2a7cefd
AJ
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);
985c86bc 85}
86
87/*
c2a7cefd 88 * a default constructed HttpRequestMethod is == Http::METHOD_NONE
985c86bc 89 */
90void
91testHttpRequestMethod::testDefaultConstructor()
92{
41030a36 93 HttpRequestMethod lhs;
c2a7cefd 94 HttpRequestMethod rhs(Http::METHOD_NONE);
41030a36 95 CPPUNIT_ASSERT_EQUAL(lhs, rhs);
985c86bc 96}
97
98/*
c2a7cefd 99 * we should be able to construct a HttpRequestMethod from a Http::MethodType
985c86bc 100 */
101void
102testHttpRequestMethod::testConstructmethod_t()
103{
c2a7cefd
AJ
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));
985c86bc 107}
108
109/*
110 * we should be able to get a char const * version of the method.
111 */
112void
914b89a2 113testHttpRequestMethod::testImage()
985c86bc 114{
35efdb9b
AJ
115 // relaxed RFC-compliance parse HTTP methods are upgraded to correct case
116 Config.onoff.relaxed_header_parser = 1;
f9688132
AJ
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());
35efdb9b
AJ
120
121 // strict RFC-compliance parse HTTP methods are case sensitive
122 Config.onoff.relaxed_header_parser = 0;
f9688132
AJ
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());
985c86bc 126}
127
128/*
c2a7cefd 129 * an HttpRequestMethod should be comparable to a Http::MethodType without false
985c86bc 130 * matches
131 */
132void
133testHttpRequestMethod::testEqualmethod_t()
134{
c2a7cefd
AJ
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));
985c86bc 139}
140
141/*
142 * an HttpRequestMethod should testable for inequality without fail maatches
143 */
144void
145testHttpRequestMethod::testNotEqualmethod_t()
146{
c2a7cefd
AJ
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));
985c86bc 151}
152
153/*
154 * we should be able to send it to a stream and get the normalised version
155 */
156void
157testHttpRequestMethod::testStream()
158{
35efdb9b
AJ
159 // relaxed RFC-compliance parse HTTP methods are upgraded to correct case
160 Config.onoff.relaxed_header_parser = 1;
985c86bc 161 std::ostringstream buffer;
f9688132 162 buffer << HttpRequestMethod(SBuf("get"));
30abd221 163 CPPUNIT_ASSERT_EQUAL(String("GET"), String(buffer.str().c_str()));
35efdb9b
AJ
164
165 // strict RFC-compliance parse HTTP methods are case sensitive
166 Config.onoff.relaxed_header_parser = 0;
167 std::ostringstream buffer2;
f9688132 168 buffer2 << HttpRequestMethod(SBuf("get"));
35efdb9b 169 CPPUNIT_ASSERT_EQUAL(String("get"), String(buffer2.str().c_str()));
985c86bc 170}
f53969cc 171