]> git.ipfire.org Git - thirdparty/squid.git/blame - src/tests/testHttpRequestMethod.cc
Add missing http/forward.h
[thirdparty/squid.git] / src / tests / testHttpRequestMethod.cc
CommitLineData
26ac0430 1#define SQUID_UNIT_TEST 1
ec94e362 2
582c2af2 3#include "squid.h"
985c86bc 4#include <cppunit/TestAssert.h>
5
602d9612 6#include "HttpRequestMethod.h"
985c86bc 7#include "Mem.h"
35efdb9b 8#include "SquidConfig.h"
985c86bc 9#include "testHttpRequestMethod.h"
985c86bc 10
27e059d4
AJ
11#if HAVE_SSTREAM
12#include <sstream>
13#endif
985c86bc 14
15CPPUNIT_TEST_SUITE_REGISTRATION( testHttpRequestMethod );
16
985c86bc 17/*
18 * We should be able to make an HttpRequestMethod straight from a string.
19 */
20void
21testHttpRequestMethod::testConstructCharStart()
22{
c2a7cefd
AJ
23 /* parse an empty string -> Http::METHOD_NONE */
24 CPPUNIT_ASSERT(HttpRequestMethod(NULL,NULL) == Http::METHOD_NONE);
985c86bc 25 /* parsing a literal should work */
c2a7cefd
AJ
26 CPPUNIT_ASSERT(HttpRequestMethod("GET", NULL) == Http::METHOD_GET);
27 CPPUNIT_ASSERT(HttpRequestMethod("QWERTY", NULL) == Http::METHOD_OTHER);
985c86bc 28}
29
30/*
26ac0430 31 * We can also parse precise ranges of characters
985c86bc 32 */
33void
34testHttpRequestMethod::testConstructCharStartEnd()
35{
36 char const * buffer;
c2a7cefd
AJ
37 /* parse an empty string -> Http::METHOD_NONE */
38 CPPUNIT_ASSERT(HttpRequestMethod(NULL, NULL) == Http::METHOD_NONE);
985c86bc 39 /* parsing a literal should work */
c2a7cefd 40 CPPUNIT_ASSERT(HttpRequestMethod("GET", NULL) == Http::METHOD_GET);
985c86bc 41 /* parsing with an explicit end should work */
42 buffer = "POSTPLUS";
c2a7cefd 43 CPPUNIT_ASSERT(HttpRequestMethod(buffer, buffer + 4) == Http::METHOD_POST);
985c86bc 44}
45
46/*
c2a7cefd 47 * we should be able to assign a Http::MethodType to a HttpRequestMethod
985c86bc 48 */
49void
50testHttpRequestMethod::testAssignFrommethod_t()
51{
52 HttpRequestMethod method;
c2a7cefd
AJ
53 method = Http::METHOD_NONE;
54 CPPUNIT_ASSERT_EQUAL(HttpRequestMethod(Http::METHOD_NONE), method);
55 method = Http::METHOD_POST;
56 CPPUNIT_ASSERT_EQUAL(HttpRequestMethod(Http::METHOD_POST), method);
985c86bc 57}
58
59/*
c2a7cefd 60 * a default constructed HttpRequestMethod is == Http::METHOD_NONE
985c86bc 61 */
62void
63testHttpRequestMethod::testDefaultConstructor()
64{
41030a36 65 HttpRequestMethod lhs;
c2a7cefd 66 HttpRequestMethod rhs(Http::METHOD_NONE);
41030a36 67 CPPUNIT_ASSERT_EQUAL(lhs, rhs);
985c86bc 68}
69
70/*
c2a7cefd 71 * we should be able to construct a HttpRequestMethod from a Http::MethodType
985c86bc 72 */
73void
74testHttpRequestMethod::testConstructmethod_t()
75{
c2a7cefd
AJ
76 CPPUNIT_ASSERT_EQUAL(HttpRequestMethod(Http::METHOD_NONE), HttpRequestMethod(Http::METHOD_NONE));
77 CPPUNIT_ASSERT_EQUAL(HttpRequestMethod(Http::METHOD_POST), HttpRequestMethod(Http::METHOD_POST));
78 CPPUNIT_ASSERT(HttpRequestMethod(Http::METHOD_NONE) != HttpRequestMethod(Http::METHOD_POST));
985c86bc 79}
80
81/*
82 * we should be able to get a char const * version of the method.
83 */
84void
914b89a2 85testHttpRequestMethod::testImage()
985c86bc 86{
35efdb9b
AJ
87 // relaxed RFC-compliance parse HTTP methods are upgraded to correct case
88 Config.onoff.relaxed_header_parser = 1;
89 CPPUNIT_ASSERT_EQUAL(String("POST"), String(HttpRequestMethod("POST",NULL).image()));
90 CPPUNIT_ASSERT_EQUAL(String("POST"), String(HttpRequestMethod("pOsT",NULL).image()));
914b89a2 91 CPPUNIT_ASSERT_EQUAL(String("POST"), String(HttpRequestMethod("post",NULL).image()));
35efdb9b
AJ
92
93 // strict RFC-compliance parse HTTP methods are case sensitive
94 Config.onoff.relaxed_header_parser = 0;
95 CPPUNIT_ASSERT_EQUAL(String("POST"), String(HttpRequestMethod("POST",NULL).image()));
96 CPPUNIT_ASSERT_EQUAL(String("pOsT"), String(HttpRequestMethod("pOsT",NULL).image()));
97 CPPUNIT_ASSERT_EQUAL(String("post"), String(HttpRequestMethod("post",NULL).image()));
985c86bc 98}
99
100/*
c2a7cefd 101 * an HttpRequestMethod should be comparable to a Http::MethodType without false
985c86bc 102 * matches
103 */
104void
105testHttpRequestMethod::testEqualmethod_t()
106{
c2a7cefd
AJ
107 CPPUNIT_ASSERT(HttpRequestMethod(Http::METHOD_NONE) == Http::METHOD_NONE);
108 CPPUNIT_ASSERT(not (HttpRequestMethod(Http::METHOD_POST) == Http::METHOD_GET));
109 CPPUNIT_ASSERT(HttpRequestMethod(Http::METHOD_GET) == Http::METHOD_GET);
110 CPPUNIT_ASSERT(not (HttpRequestMethod(Http::METHOD_TRACE) == Http::METHOD_SEARCH));
985c86bc 111}
112
113/*
114 * an HttpRequestMethod should testable for inequality without fail maatches
115 */
116void
117testHttpRequestMethod::testNotEqualmethod_t()
118{
c2a7cefd
AJ
119 CPPUNIT_ASSERT(HttpRequestMethod(Http::METHOD_NONE) != Http::METHOD_GET);
120 CPPUNIT_ASSERT(not (HttpRequestMethod(Http::METHOD_POST) != Http::METHOD_POST));
121 CPPUNIT_ASSERT(HttpRequestMethod(Http::METHOD_GET) != Http::METHOD_NONE);
122 CPPUNIT_ASSERT(not (HttpRequestMethod(Http::METHOD_SEARCH) != Http::METHOD_SEARCH));
985c86bc 123}
124
125/*
126 * we should be able to send it to a stream and get the normalised version
127 */
128void
129testHttpRequestMethod::testStream()
130{
35efdb9b
AJ
131 // relaxed RFC-compliance parse HTTP methods are upgraded to correct case
132 Config.onoff.relaxed_header_parser = 1;
985c86bc 133 std::ostringstream buffer;
35efdb9b 134 buffer << HttpRequestMethod("get", NULL);
30abd221 135 CPPUNIT_ASSERT_EQUAL(String("GET"), String(buffer.str().c_str()));
35efdb9b
AJ
136
137 // strict RFC-compliance parse HTTP methods are case sensitive
138 Config.onoff.relaxed_header_parser = 0;
139 std::ostringstream buffer2;
140 buffer2 << HttpRequestMethod("get", NULL);
141 CPPUNIT_ASSERT_EQUAL(String("get"), String(buffer2.str().c_str()));
985c86bc 142}