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