]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/testHttpRequest.cc
5 #include <cppunit/TestAssert.h>
7 #include "testHttpRequest.h"
8 #include "HttpRequest.h"
12 CPPUNIT_TEST_SUITE_REGISTRATION( testHttpRequest
);
14 /* stub functions to link successfully */
25 /* init memory pools */
28 testHttpRequest::setUp()
34 * Test creating an HttpRequest object from a Url and method
37 testHttpRequest::testCreateFromUrlAndMethod()
41 char * url
= xstrdup("http://foo:90/bar");
42 HttpRequest
*aRequest
= HttpRequest::CreateFromUrlAndMethod(url
, METHOD_GET
);
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
));
53 /* vanilla url, different method */
54 url
= xstrdup("http://foo/bar");
55 aRequest
= HttpRequest::CreateFromUrlAndMethod(url
, METHOD_PUT
);
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
));
64 /* a connect url with non-CONNECT data */
65 url
= xstrdup(":foo/bar");
66 aRequest
= HttpRequest::CreateFromUrlAndMethod(url
, METHOD_CONNECT
);
68 CPPUNIT_ASSERT_EQUAL(nullRequest
, aRequest
);
70 /* a CONNECT url with CONNECT data */
71 url
= xstrdup("foo:45");
72 aRequest
= HttpRequest::CreateFromUrlAndMethod(url
, METHOD_CONNECT
);
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
));
84 * Test creating an HttpRequest object from a Url alone.
87 testHttpRequest::testCreateFromUrl()
91 char * url
= xstrdup("http://foo:90/bar");
92 HttpRequest
*aRequest
= HttpRequest::CreateFromUrl(url
);
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
));
104 * Test BUG: URL '2000:800:45' opens host 2000 port 800 !!
107 testHttpRequest::testIPv6HostColonBug()
109 ushort expected_port
;
111 HttpRequest
*aRequest
= NULL
;
113 /* valid IPv6 address without port */
114 url
= xstrdup("http://[2000:800::45]/foo");
115 aRequest
= HttpRequest::CreateFromUrlAndMethod(url
, METHOD_GET
);
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
));
125 /* valid IPv6 address with port */
126 url
= xstrdup("http://[2000:800::45]:90/foo");
127 aRequest
= HttpRequest::CreateFromUrlAndMethod(url
, METHOD_GET
);
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
));
137 /* IPv6 address as invalid (bug trigger) */
138 url
= xstrdup("http://2000:800::45/foo");
139 aRequest
= HttpRequest::CreateFromUrlAndMethod(url
, METHOD_GET
);
141 CPPUNIT_ASSERT_EQUAL(expected_port
, aRequest
->port
);
142 CPPUNIT_ASSERT(aRequest
->method
== METHOD_GET
);
144 /* We hasve fixed this in IPv6 build. */
145 CPPUNIT_ASSERT_EQUAL(String("[2000:800::45]"), String(aRequest
->GetHost()));
147 /* NO fix is possible in IPv4-pure build. */
148 CPPUNIT_ASSERT_EQUAL(String("2000:800::45"), String(aRequest
->GetHost()));
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
));