]> git.ipfire.org Git - thirdparty/squid.git/blame - src/tests/testUriScheme.cc
Source Format Enforcement (#763)
[thirdparty/squid.git] / src / tests / testUriScheme.cc
CommitLineData
4e0938ef 1/*
f70aedc4 2 * Copyright (C) 1996-2021 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 */
8
582c2af2 9#include "squid.h"
27e059d4 10
985c86bc 11#include <cppunit/TestAssert.h>
12
1ca54a54 13#include "anyp/UriScheme.h"
1ca54a54 14#include "tests/testUriScheme.h"
985c86bc 15
27e059d4 16#include <sstream>
985c86bc 17
1ca54a54 18CPPUNIT_TEST_SUITE_REGISTRATION( testUriScheme );
985c86bc 19
985c86bc 20#if 0
21/*
22 * We should be able to make an HttpRequestMethod straight from a string.
23 */
24void
25testHttpRequestMethod::testConstructCharStart()
26{
27 /* parse an empty string -> METHOD_NONE */
28 CPPUNIT_ASSERT(METHOD_NONE == HttpRequestMethod(NULL));
29 /* parsing a literal should work */
30 CPPUNIT_ASSERT(METHOD_GET == HttpRequestMethod("GET", NULL));
31}
32
33/*
26ac0430 34 * We can also parse precise ranges of characters
985c86bc 35 */
36void
37testHttpRequestMethod::testConstructCharStartEnd()
38{
39 char const * buffer;
40 /* parse an empty string -> METHOD_NONE */
41 CPPUNIT_ASSERT(METHOD_NONE == HttpRequestMethod(NULL, NULL));
42 /* parsing a literal should work */
43 CPPUNIT_ASSERT(METHOD_GET == HttpRequestMethod("GET", NULL));
44 /* parsing with an explicit end should work */
45 buffer = "POSTPLUS";
46 CPPUNIT_ASSERT(METHOD_POST == HttpRequestMethod(buffer, buffer + 4));
47}
48
49#endif
50
51/*
1ca54a54 52 * we should be able to assign a protocol_t to a AnyP::UriScheme for ease
985c86bc 53 * of code conversion
54 */
55void
1ca54a54 56testUriScheme::testAssignFromprotocol_t()
985c86bc 57{
1ca54a54
AJ
58 AnyP::UriScheme empty_scheme;
59 AnyP::UriScheme scheme;
0c3d3f65 60 scheme = AnyP::PROTO_NONE;
41030a36 61 CPPUNIT_ASSERT_EQUAL(empty_scheme, scheme);
62
1ca54a54 63 AnyP::UriScheme https_scheme(AnyP::PROTO_HTTPS);
0c3d3f65 64 scheme = AnyP::PROTO_HTTPS;
41030a36 65 CPPUNIT_ASSERT_EQUAL(https_scheme, scheme);
985c86bc 66}
67
68/*
1ca54a54 69 * We should be able to get a protocol_t from a AnyP::UriScheme for ease
985c86bc 70 * of migration
71 */
72void
1ca54a54 73testUriScheme::testCastToprotocol_t()
985c86bc 74{
75 /* explicit cast */
1ca54a54 76 AnyP::ProtocolType protocol = static_cast<AnyP::ProtocolType>(AnyP::UriScheme());
0c3d3f65 77 CPPUNIT_ASSERT_EQUAL(AnyP::PROTO_NONE, protocol);
985c86bc 78 /* and implicit */
1ca54a54 79 protocol = AnyP::UriScheme(AnyP::PROTO_HTTP);
0c3d3f65 80 CPPUNIT_ASSERT_EQUAL(AnyP::PROTO_HTTP, protocol);
985c86bc 81}
82
83/*
1ca54a54 84 * a default constructed AnyP::UriScheme is == AnyP::PROTO_NONE
985c86bc 85 */
86void
1ca54a54 87testUriScheme::testDefaultConstructor()
985c86bc 88{
1ca54a54
AJ
89 AnyP::UriScheme lhs;
90 AnyP::UriScheme rhs(AnyP::PROTO_NONE);
41030a36 91 CPPUNIT_ASSERT_EQUAL(lhs, rhs);
985c86bc 92}
93
94/*
1ca54a54 95 * we should be able to construct a AnyP::UriScheme from the old 'protocol_t' enum.
985c86bc 96 */
97void
1ca54a54 98testUriScheme::testConstructprotocol_t()
985c86bc 99{
1ca54a54 100 AnyP::UriScheme lhs_none(AnyP::PROTO_NONE), rhs_none(AnyP::PROTO_NONE);
41030a36 101 CPPUNIT_ASSERT_EQUAL(lhs_none, rhs_none);
102
1ca54a54 103 AnyP::UriScheme lhs_cacheobj(AnyP::PROTO_CACHE_OBJECT), rhs_cacheobj(AnyP::PROTO_CACHE_OBJECT);
41030a36 104 CPPUNIT_ASSERT_EQUAL(lhs_cacheobj, rhs_cacheobj);
105 CPPUNIT_ASSERT(lhs_none != rhs_cacheobj);
985c86bc 106}
107
108/*
109 * we should be able to get a char const * version of the method.
110 */
111void
1ca54a54 112testUriScheme::testC_str()
985c86bc 113{
d31d59d8 114 SBuf lhs("wais");
1ca54a54 115 AnyP::UriScheme wais(AnyP::PROTO_WAIS);
d31d59d8 116 SBuf rhs(wais.image());
41030a36 117 CPPUNIT_ASSERT_EQUAL(lhs, rhs);
985c86bc 118}
119
120/*
1ca54a54 121 * a AnyP::UriScheme replaces protocol_t, so we should be able to test for equality on
985c86bc 122 * either the left or right hand side seamlessly.
123 */
124void
1ca54a54 125testUriScheme::testEqualprotocol_t()
985c86bc 126{
1ca54a54
AJ
127 CPPUNIT_ASSERT(AnyP::UriScheme() == AnyP::PROTO_NONE);
128 CPPUNIT_ASSERT(not (AnyP::UriScheme(AnyP::PROTO_WAIS) == AnyP::PROTO_HTTP));
129 CPPUNIT_ASSERT(AnyP::PROTO_HTTP == AnyP::UriScheme(AnyP::PROTO_HTTP));
130 CPPUNIT_ASSERT(not (AnyP::PROTO_CACHE_OBJECT == AnyP::UriScheme(AnyP::PROTO_HTTP)));
985c86bc 131}
132
133/*
1ca54a54 134 * a AnyP::UriScheme should testable for inequality with a protocol_t.
985c86bc 135 */
136void
1ca54a54 137testUriScheme::testNotEqualprotocol_t()
985c86bc 138{
1ca54a54
AJ
139 CPPUNIT_ASSERT(AnyP::UriScheme(AnyP::PROTO_NONE) != AnyP::PROTO_HTTP);
140 CPPUNIT_ASSERT(not (AnyP::UriScheme(AnyP::PROTO_HTTP) != AnyP::PROTO_HTTP));
141 CPPUNIT_ASSERT(AnyP::PROTO_NONE != AnyP::UriScheme(AnyP::PROTO_HTTP));
142 CPPUNIT_ASSERT(not (AnyP::PROTO_WAIS != AnyP::UriScheme(AnyP::PROTO_WAIS)));
985c86bc 143}
144
145/*
146 * we should be able to send it to a stream and get the normalised version
147 */
148void
1ca54a54 149testUriScheme::testStream()
985c86bc 150{
151 std::ostringstream buffer;
1ca54a54 152 buffer << AnyP::UriScheme(AnyP::PROTO_HTTP);
dc334f20
AJ
153 SBuf http_str("http");
154 SBuf from_buf(buffer.str());
41030a36 155 CPPUNIT_ASSERT_EQUAL(http_str, from_buf);
985c86bc 156}
f53969cc 157
e58d6701
FC
158void
159testUriScheme::setUp()
160{
161 Mem::Init();
162 AnyP::UriScheme::Init();
163}
70ac5b29 164