testHttpRequestMethod.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
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  */
8 
9 #include "squid.h"
10 #include "compat/cppunit.h"
11 #include "http/RequestMethod.h"
12 #include "SquidConfig.h"
13 
14 #include <sstream>
15 
16 class TestHttpRequestMethod : public CPPUNIT_NS::TestFixture
17 {
29 
30 public:
31 protected:
33  void testConstructmethod_t();
36  void testImage();
38  void testEqualmethod_t();
39  void testNotEqualmethod_t();
40  void testStream();
41 };
42 
44 
45 /*
46  * We should be able to make an HttpRequestMethod straight from a string.
47  */
48 void
50 {
51  // string in SBuf
52 
53  /* parse an empty string -> Http::METHOD_NONE */
54  CPPUNIT_ASSERT(HttpRequestMethod(SBuf()) == Http::METHOD_NONE);
55 
56  /* parsing a literal should work */
57  CPPUNIT_ASSERT(HttpRequestMethod(SBuf("GET")) == Http::METHOD_GET);
58  CPPUNIT_ASSERT(HttpRequestMethod(SBuf("QWERTY")) == Http::METHOD_OTHER);
59 
60  // string in char*
61 
62  /* parse an empty string -> Http::METHOD_NONE */
64  a.HttpRequestMethodXXX(nullptr);
65  CPPUNIT_ASSERT(a == Http::METHOD_NONE);
66 
67  /* parsing a literal should work */
69  b.HttpRequestMethodXXX("GET");
70  CPPUNIT_ASSERT(b == Http::METHOD_GET);
71  CPPUNIT_ASSERT_EQUAL(SBuf("GET"), b.image());
73  c.HttpRequestMethodXXX("QWERTY");
74  CPPUNIT_ASSERT(c == Http::METHOD_OTHER);
75  CPPUNIT_ASSERT_EQUAL(SBuf("QWERTY"), c.image());
76 
77  // parsing error should not leave stale results
78  b.HttpRequestMethodXXX(nullptr);
79  CPPUNIT_ASSERT(b == Http::METHOD_NONE);
80  CPPUNIT_ASSERT_EQUAL(SBuf("NONE"), b.image());
81 }
82 
83 /*
84  * We can also parse precise ranges of characters with SBuf
85  */
86 void
88 {
89  char const * buffer;
90  /* parse an empty string -> Http::METHOD_NONE */
91  CPPUNIT_ASSERT(HttpRequestMethod(SBuf()) == Http::METHOD_NONE);
92  /* parsing a literal should work */
93  CPPUNIT_ASSERT(HttpRequestMethod(SBuf("GET")) == Http::METHOD_GET);
94  /* parsing with an explicit end should work */
95  buffer = "POSTPLUS";
96  CPPUNIT_ASSERT(HttpRequestMethod(SBuf(buffer, 4)) == Http::METHOD_POST);
97 }
98 
99 /*
100  * we should be able to assign a Http::MethodType to a HttpRequestMethod
101  */
102 void
104 {
105  HttpRequestMethod method;
106  method = Http::METHOD_NONE;
107  CPPUNIT_ASSERT_EQUAL(HttpRequestMethod(Http::METHOD_NONE), method);
108  method = Http::METHOD_POST;
109  CPPUNIT_ASSERT_EQUAL(HttpRequestMethod(Http::METHOD_POST), method);
110 }
111 
112 /*
113  * a default constructed HttpRequestMethod is == Http::METHOD_NONE
114  */
115 void
117 {
118  HttpRequestMethod lhs;
120  CPPUNIT_ASSERT_EQUAL(lhs, rhs);
121 }
122 
123 /*
124  * we should be able to construct a HttpRequestMethod from a Http::MethodType
125  */
126 void
128 {
132 }
133 
134 /*
135  * we should be able to get a char const * version of the method.
136  */
137 void
139 {
140  // relaxed RFC-compliance parse HTTP methods are upgraded to correct case
142  CPPUNIT_ASSERT_EQUAL(SBuf("POST"), HttpRequestMethod(SBuf("POST")).image());
143  CPPUNIT_ASSERT_EQUAL(SBuf("POST"), HttpRequestMethod(SBuf("pOsT")).image());
144  CPPUNIT_ASSERT_EQUAL(SBuf("POST"), HttpRequestMethod(SBuf("post")).image());
145 
146  // strict RFC-compliance parse HTTP methods are case sensitive
148  CPPUNIT_ASSERT_EQUAL(SBuf("POST"), HttpRequestMethod(SBuf("POST")).image());
149  CPPUNIT_ASSERT_EQUAL(SBuf("pOsT"), HttpRequestMethod(SBuf("pOsT")).image());
150  CPPUNIT_ASSERT_EQUAL(SBuf("post"), HttpRequestMethod(SBuf("post")).image());
151 }
152 
153 /*
154  * an HttpRequestMethod should be comparable to a Http::MethodType without false
155  * matches
156  */
157 void
159 {
161  CPPUNIT_ASSERT(not (HttpRequestMethod(Http::METHOD_POST) == Http::METHOD_GET));
163  CPPUNIT_ASSERT(not (HttpRequestMethod(Http::METHOD_TRACE) == Http::METHOD_SEARCH));
164 }
165 
166 /*
167  * an HttpRequestMethod should testable for inequality without fail maatches
168  */
169 void
171 {
173  CPPUNIT_ASSERT(not (HttpRequestMethod(Http::METHOD_POST) != Http::METHOD_POST));
176 }
177 
178 /*
179  * we should be able to send it to a stream and get the normalised version
180  */
181 void
183 {
184  // relaxed RFC-compliance parse HTTP methods are upgraded to correct case
186  std::ostringstream buffer;
187  buffer << HttpRequestMethod(SBuf("get"));
188  CPPUNIT_ASSERT_EQUAL(String("GET"), String(buffer.str().c_str()));
189 
190  // strict RFC-compliance parse HTTP methods are case sensitive
192  std::ostringstream buffer2;
193  buffer2 << HttpRequestMethod(SBuf("get"));
194  CPPUNIT_ASSERT_EQUAL(String("get"), String(buffer2.str().c_str()));
195 }
196 
197 // This test uses main() from ./testHttpRequest.cc.
198 
@ METHOD_OTHER
Definition: MethodType.h:93
int relaxed_header_parser
Definition: SquidConfig.h:315
Definition: SBuf.h:93
struct SquidConfig::@97 onoff
CPPUNIT_TEST_SUITE(TestHttpRequestMethod)
const SBuf & image() const
CPPUNIT_TEST(testAssignFrommethod_t)
@ METHOD_POST
Definition: MethodType.h:26
@ METHOD_TRACE
Definition: MethodType.h:30
void HttpRequestMethodXXX(char const *)
@ METHOD_SEARCH
Definition: MethodType.h:76
@ METHOD_NONE
Definition: MethodType.h:22
@ METHOD_GET
Definition: MethodType.h:25
CPPUNIT_TEST_SUITE_REGISTRATION(TestHttpRequestMethod)
class SquidConfig Config
Definition: SquidConfig.cc:12

 

Introduction

Documentation

Support

Miscellaneous