testUriScheme.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 
11 #include "anyp/UriScheme.h"
12 #include "compat/cppunit.h"
13 
14 #include <cppunit/TestAssert.h>
15 #include <sstream>
16 
17 /*
18  * test UriScheme
19  */
20 
21 class TestUriScheme : public CPPUNIT_NS::TestFixture
22 {
33 
34 protected:
36  void testCastToprotocol_t();
38  void testC_str();
40  void testEqualprotocol_t();
42  void testStream();
43 };
45 
46 /*
47  * we should be able to assign a protocol_t to a AnyP::UriScheme for ease
48  * of code conversion
49  */
50 void
52 {
53  AnyP::UriScheme empty_scheme;
54  AnyP::UriScheme scheme;
55  scheme = AnyP::PROTO_NONE;
56  CPPUNIT_ASSERT_EQUAL(empty_scheme, scheme);
57 
58  AnyP::UriScheme https_scheme(AnyP::PROTO_HTTPS);
59  scheme = AnyP::PROTO_HTTPS;
60  CPPUNIT_ASSERT_EQUAL(https_scheme, scheme);
61 }
62 
63 /*
64  * We should be able to get a protocol_t from a AnyP::UriScheme for ease
65  * of migration
66  */
67 void
69 {
70  /* explicit cast */
71  AnyP::ProtocolType protocol = static_cast<AnyP::ProtocolType>(AnyP::UriScheme());
72  CPPUNIT_ASSERT_EQUAL(AnyP::PROTO_NONE, protocol);
73  /* and implicit */
75  CPPUNIT_ASSERT_EQUAL(AnyP::PROTO_HTTP, protocol);
76 }
77 
78 /*
79  * a default constructed AnyP::UriScheme is == AnyP::PROTO_NONE
80  */
81 void
83 {
84  AnyP::UriScheme lhs;
86  CPPUNIT_ASSERT_EQUAL(lhs, rhs);
87 }
88 
89 /*
90  * we should be able to construct a AnyP::UriScheme from the old 'protocol_t' enum.
91  */
92 void
94 {
96  CPPUNIT_ASSERT_EQUAL(lhs_none, rhs_none);
97 
98  AnyP::UriScheme lhs_cacheobj(AnyP::PROTO_HTTP), rhs_cacheobj(AnyP::PROTO_HTTP);
99  CPPUNIT_ASSERT_EQUAL(lhs_cacheobj, rhs_cacheobj);
100  CPPUNIT_ASSERT(lhs_none != rhs_cacheobj);
101 }
102 
103 /*
104  * we should be able to get a char const * version of the method.
105  */
106 void
108 {
109  SBuf lhs("wais");
111  SBuf rhs(wais.image());
112  CPPUNIT_ASSERT_EQUAL(lhs, rhs);
113 }
114 
115 /*
116  * a AnyP::UriScheme replaces protocol_t, so we should be able to test for equality on
117  * either the left or right hand side seamlessly.
118  */
119 void
121 {
122  CPPUNIT_ASSERT(AnyP::UriScheme() == AnyP::PROTO_NONE);
123  CPPUNIT_ASSERT(not (AnyP::UriScheme(AnyP::PROTO_WAIS) == AnyP::PROTO_HTTP));
125  CPPUNIT_ASSERT(not (AnyP::PROTO_HTTPS == AnyP::UriScheme(AnyP::PROTO_HTTP)));
126 }
127 
128 /*
129  * a AnyP::UriScheme should testable for inequality with a protocol_t.
130  */
131 void
133 {
135  CPPUNIT_ASSERT(not (AnyP::UriScheme(AnyP::PROTO_HTTP) != AnyP::PROTO_HTTP));
137  CPPUNIT_ASSERT(not (AnyP::PROTO_WAIS != AnyP::UriScheme(AnyP::PROTO_WAIS)));
138 }
139 
140 /*
141  * we should be able to send it to a stream and get the normalised version
142  */
143 void
145 {
146  std::ostringstream buffer;
148  SBuf http_str("http");
149  SBuf from_buf(buffer.str());
150  CPPUNIT_ASSERT_EQUAL(http_str, from_buf);
151 }
152 
153 // This test uses main() from ./testURL.cc.
154 
CPPUNIT_TEST_SUITE(TestUriScheme)
@ PROTO_NONE
Definition: ProtocolType.h:24
void testNotEqualprotocol_t()
void testAssignFromprotocol_t()
Definition: SBuf.h:93
void testDefaultConstructor()
ProtocolType
Definition: ProtocolType.h:23
void testCastToprotocol_t()
SBuf image() const
Definition: UriScheme.h:57
void testConstructprotocol_t()
@ PROTO_HTTPS
Definition: ProtocolType.h:27
@ PROTO_HTTP
Definition: ProtocolType.h:25
@ PROTO_WAIS
Definition: ProtocolType.h:30
CPPUNIT_TEST(testAssignFromprotocol_t)
CPPUNIT_TEST_SUITE_REGISTRATION(TestUriScheme)
void testEqualprotocol_t()

 

Introduction

Documentation

Support

Miscellaneous