testHttpRequestMethod.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2022 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 <cppunit/TestAssert.h>
11
12#include "http/RequestMethod.h"
13#include "SquidConfig.h"
15
16#include <sstream>
17
19
20/*
21 * We should be able to make an HttpRequestMethod straight from a string.
22 */
23void
25{
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
37 /* parse an empty string -> Http::METHOD_NONE */
39 a.HttpRequestMethodXXX(nullptr);
40 CPPUNIT_ASSERT(a == Http::METHOD_NONE);
41
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());
51
52 // parsing error should not leave stale results
53 b.HttpRequestMethodXXX(nullptr);
54 CPPUNIT_ASSERT(b == Http::METHOD_NONE);
55 CPPUNIT_ASSERT_EQUAL(SBuf("NONE"), b.image());
56}
57
58/*
59 * We can also parse precise ranges of characters with SBuf
60 */
61void
63{
64 char const * buffer;
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 */
70 buffer = "POSTPLUS";
71 CPPUNIT_ASSERT(HttpRequestMethod(SBuf(buffer, 4)) == Http::METHOD_POST);
72}
73
74/*
75 * we should be able to assign a Http::MethodType to a HttpRequestMethod
76 */
77void
79{
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);
85}
86
87/*
88 * a default constructed HttpRequestMethod is == Http::METHOD_NONE
89 */
90void
92{
95 CPPUNIT_ASSERT_EQUAL(lhs, rhs);
96}
97
98/*
99 * we should be able to construct a HttpRequestMethod from a Http::MethodType
100 */
101void
103{
107}
108
109/*
110 * we should be able to get a char const * version of the method.
111 */
112void
114{
115 // relaxed RFC-compliance parse HTTP methods are upgraded to correct case
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());
120
121 // strict RFC-compliance parse HTTP methods are case sensitive
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());
126}
127
128/*
129 * an HttpRequestMethod should be comparable to a Http::MethodType without false
130 * matches
131 */
132void
134{
136 CPPUNIT_ASSERT(not (HttpRequestMethod(Http::METHOD_POST) == Http::METHOD_GET));
139}
140
141/*
142 * an HttpRequestMethod should testable for inequality without fail maatches
143 */
144void
146{
148 CPPUNIT_ASSERT(not (HttpRequestMethod(Http::METHOD_POST) != Http::METHOD_POST));
151}
152
153/*
154 * we should be able to send it to a stream and get the normalised version
155 */
156void
158{
159 // relaxed RFC-compliance parse HTTP methods are upgraded to correct case
161 std::ostringstream buffer;
162 buffer << HttpRequestMethod(SBuf("get"));
163 CPPUNIT_ASSERT_EQUAL(String("GET"), String(buffer.str().c_str()));
164
165 // strict RFC-compliance parse HTTP methods are case sensitive
167 std::ostringstream buffer2;
168 buffer2 << HttpRequestMethod(SBuf("get"));
169 CPPUNIT_ASSERT_EQUAL(String("get"), String(buffer2.str().c_str()));
170}
171
class SquidConfig Config
Definition: SquidConfig.cc:12
void HttpRequestMethodXXX(char const *)
const SBuf & image() const
Definition: SBuf.h:94
struct SquidConfig::@111 onoff
int relaxed_header_parser
Definition: SquidConfig.h:313
@ METHOD_TRACE
Definition: MethodType.h:30
@ METHOD_NONE
Definition: MethodType.h:22
@ METHOD_OTHER
Definition: MethodType.h:93
@ METHOD_SEARCH
Definition: MethodType.h:76
@ METHOD_POST
Definition: MethodType.h:26
@ METHOD_GET
Definition: MethodType.h:25
CPPUNIT_TEST_SUITE_REGISTRATION(testHttpRequestMethod)

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors