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