]> git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/testHttpRequest.cc
Cleanups: Replace a series of URN handler gotos with a simple sub-function.
[thirdparty/squid.git] / src / tests / testHttpRequest.cc
1 #if 0
2 #include "squid.h"
3 #endif
4
5 #include <cppunit/TestAssert.h>
6
7 #include "testHttpRequest.h"
8 #include "HttpRequest.h"
9 #include "Mem.h"
10
11
12 CPPUNIT_TEST_SUITE_REGISTRATION( testHttpRequest );
13
14 /* stub functions to link successfully */
15 void
16 shut_down(int)
17 {}
18
19 void
20 reconfigure(int)
21 {}
22
23 /* end stubs */
24
25 /* init memory pools */
26
27 void
28 testHttpRequest::setUp()
29 {
30 Mem::Init();
31 }
32
33 /*
34 * Test creating an HttpRequest object from a Url and method
35 */
36 void
37 testHttpRequest::testCreateFromUrlAndMethod()
38 {
39 /* vanilla url */
40 ushort expected_port;
41 char * url = xstrdup("http://foo:90/bar");
42 HttpRequest *aRequest = HttpRequest::CreateFromUrlAndMethod(url, METHOD_GET);
43 expected_port = 90;
44 HttpRequest *nullRequest = NULL;
45 CPPUNIT_ASSERT_EQUAL(expected_port, aRequest->port);
46 CPPUNIT_ASSERT(aRequest->method == METHOD_GET);
47 CPPUNIT_ASSERT_EQUAL(String("foo"), String(aRequest->GetHost()));
48 CPPUNIT_ASSERT_EQUAL(String("/bar"), aRequest->urlpath);
49 CPPUNIT_ASSERT_EQUAL(PROTO_HTTP, aRequest->protocol);
50 CPPUNIT_ASSERT_EQUAL(String("http://foo:90/bar"), String(url));
51 xfree(url);
52
53 /* vanilla url, different method */
54 url = xstrdup("http://foo/bar");
55 aRequest = HttpRequest::CreateFromUrlAndMethod(url, METHOD_PUT);
56 expected_port = 80;
57 CPPUNIT_ASSERT_EQUAL(expected_port, aRequest->port);
58 CPPUNIT_ASSERT(aRequest->method == METHOD_PUT);
59 CPPUNIT_ASSERT_EQUAL(String("foo"), String(aRequest->GetHost()));
60 CPPUNIT_ASSERT_EQUAL(String("/bar"), aRequest->urlpath);
61 CPPUNIT_ASSERT_EQUAL(PROTO_HTTP, aRequest->protocol);
62 CPPUNIT_ASSERT_EQUAL(String("http://foo/bar"), String(url));
63
64 /* a connect url with non-CONNECT data */
65 url = xstrdup(":foo/bar");
66 aRequest = HttpRequest::CreateFromUrlAndMethod(url, METHOD_CONNECT);
67 xfree(url);
68 CPPUNIT_ASSERT_EQUAL(nullRequest, aRequest);
69
70 /* a CONNECT url with CONNECT data */
71 url = xstrdup("foo:45");
72 aRequest = HttpRequest::CreateFromUrlAndMethod(url, METHOD_CONNECT);
73 expected_port = 45;
74 CPPUNIT_ASSERT_EQUAL(expected_port, aRequest->port);
75 CPPUNIT_ASSERT(aRequest->method == METHOD_CONNECT);
76 CPPUNIT_ASSERT_EQUAL(String("foo"), String(aRequest->GetHost()));
77 CPPUNIT_ASSERT_EQUAL(String(""), aRequest->urlpath);
78 CPPUNIT_ASSERT_EQUAL(PROTO_NONE, aRequest->protocol);
79 CPPUNIT_ASSERT_EQUAL(String("foo:45"), String(url));
80 xfree(url);
81 }
82
83 /*
84 * Test creating an HttpRequest object from a Url alone.
85 */
86 void
87 testHttpRequest::testCreateFromUrl()
88 {
89 /* vanilla url */
90 ushort expected_port;
91 char * url = xstrdup("http://foo:90/bar");
92 HttpRequest *aRequest = HttpRequest::CreateFromUrl(url);
93 expected_port = 90;
94 CPPUNIT_ASSERT_EQUAL(expected_port, aRequest->port);
95 CPPUNIT_ASSERT(aRequest->method == METHOD_GET);
96 CPPUNIT_ASSERT_EQUAL(String("foo"), String(aRequest->GetHost()));
97 CPPUNIT_ASSERT_EQUAL(String("/bar"), aRequest->urlpath);
98 CPPUNIT_ASSERT_EQUAL(PROTO_HTTP, aRequest->protocol);
99 CPPUNIT_ASSERT_EQUAL(String("http://foo:90/bar"), String(url));
100 xfree(url);
101 }
102
103 /*
104 * Test BUG: URL '2000:800:45' opens host 2000 port 800 !!
105 */
106 void
107 testHttpRequest::testIPv6HostColonBug()
108 {
109 ushort expected_port;
110 char * url = NULL;
111 HttpRequest *aRequest = NULL;
112
113 /* valid IPv6 address without port */
114 url = xstrdup("http://[2000:800::45]/foo");
115 aRequest = HttpRequest::CreateFromUrlAndMethod(url, METHOD_GET);
116 expected_port = 80;
117 CPPUNIT_ASSERT_EQUAL(expected_port, aRequest->port);
118 CPPUNIT_ASSERT(aRequest->method == METHOD_GET);
119 CPPUNIT_ASSERT_EQUAL(String("[2000:800::45]"), String(aRequest->GetHost()));
120 CPPUNIT_ASSERT_EQUAL(String("/foo"), aRequest->urlpath);
121 CPPUNIT_ASSERT_EQUAL(PROTO_HTTP, aRequest->protocol);
122 CPPUNIT_ASSERT_EQUAL(String("http://[2000:800::45]/foo"), String(url));
123 xfree(url);
124
125 /* valid IPv6 address with port */
126 url = xstrdup("http://[2000:800::45]:90/foo");
127 aRequest = HttpRequest::CreateFromUrlAndMethod(url, METHOD_GET);
128 expected_port = 90;
129 CPPUNIT_ASSERT_EQUAL(expected_port, aRequest->port);
130 CPPUNIT_ASSERT(aRequest->method == METHOD_GET);
131 CPPUNIT_ASSERT_EQUAL(String("[2000:800::45]"), String(aRequest->GetHost()));
132 CPPUNIT_ASSERT_EQUAL(String("/foo"), aRequest->urlpath);
133 CPPUNIT_ASSERT_EQUAL(PROTO_HTTP, aRequest->protocol);
134 CPPUNIT_ASSERT_EQUAL(String("http://[2000:800::45]:90/foo"), String(url));
135 xfree(url);
136
137 /* IPv6 address as invalid (bug trigger) */
138 url = xstrdup("http://2000:800::45/foo");
139 aRequest = HttpRequest::CreateFromUrlAndMethod(url, METHOD_GET);
140 expected_port = 80;
141 CPPUNIT_ASSERT_EQUAL(expected_port, aRequest->port);
142 CPPUNIT_ASSERT(aRequest->method == METHOD_GET);
143 #if USE_IPV6
144 /* We hasve fixed this in IPv6 build. */
145 CPPUNIT_ASSERT_EQUAL(String("[2000:800::45]"), String(aRequest->GetHost()));
146 #else
147 /* NO fix is possible in IPv4-pure build. */
148 CPPUNIT_ASSERT_EQUAL(String("2000:800::45"), String(aRequest->GetHost()));
149 #endif
150 CPPUNIT_ASSERT_EQUAL(String("/foo"), aRequest->urlpath);
151 CPPUNIT_ASSERT_EQUAL(PROTO_HTTP, aRequest->protocol);
152 CPPUNIT_ASSERT_EQUAL(String("http://2000:800::45/foo"), String(url));
153 xfree(url);
154 }