]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/testHttpRequest.cc
2 #include <cppunit/TestAssert.h>
5 #include "testHttpRequest.h"
6 #include "HttpRequest.h"
9 CPPUNIT_TEST_SUITE_REGISTRATION( testHttpRequest
);
11 /* stub functions to link successfully */
18 /* init memory pools */
21 testHttpRequest::setUp()
27 * Test creating an HttpRequest object from a Url and method
30 testHttpRequest::testCreateFromUrlAndMethod()
34 char * url
= xstrdup("http://foo:90/bar");
35 HttpRequest
*aRequest
= HttpRequest::CreateFromUrlAndMethod(url
, METHOD_GET
);
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
));
46 /* vanilla url, different method */
47 url
= xstrdup("http://foo/bar");
48 aRequest
= HttpRequest::CreateFromUrlAndMethod(url
, METHOD_PUT
);
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
));
57 /* a connect url with non-CONNECT data */
58 url
= xstrdup(":foo/bar");
59 aRequest
= HttpRequest::CreateFromUrlAndMethod(url
, METHOD_CONNECT
);
61 CPPUNIT_ASSERT_EQUAL(nullRequest
, aRequest
);
63 /* a CONNECT url with CONNECT data */
64 url
= xstrdup("foo:45");
65 aRequest
= HttpRequest::CreateFromUrlAndMethod(url
, METHOD_CONNECT
);
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
));
77 * Test creating an HttpRequest object from a Url alone.
80 testHttpRequest::testCreateFromUrl()
84 char * url
= xstrdup("http://foo:90/bar");
85 HttpRequest
*aRequest
= HttpRequest::CreateFromUrl(url
);
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
));
97 * Test BUG: URL '2000:800:45' opens host 2000 port 800 !!
100 testHttpRequest::testIPv6HostColonBug()
102 ushort expected_port
;
104 HttpRequest
*aRequest
= NULL
;
106 /* valid IPv6 address without port */
107 url
= xstrdup("http://[2000:800::45]/foo");
108 aRequest
= HttpRequest::CreateFromUrlAndMethod(url
, METHOD_GET
);
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
));
118 /* valid IPv6 address with port */
119 url
= xstrdup("http://[2000:800::45]:90/foo");
120 aRequest
= HttpRequest::CreateFromUrlAndMethod(url
, METHOD_GET
);
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
));
130 /* IPv6 address as invalid (bug trigger) */
131 url
= xstrdup("http://2000:800::45/foo");
132 aRequest
= HttpRequest::CreateFromUrlAndMethod(url
, METHOD_GET
);
134 CPPUNIT_ASSERT_EQUAL(expected_port
, aRequest
->port
);
135 CPPUNIT_ASSERT_EQUAL(METHOD_GET
, aRequest
->method
);
137 /* We hasve fixed this in IPv6 build. */
138 CPPUNIT_ASSERT_EQUAL(String("[2000:800::45]"), String(aRequest
->GetHost()));
140 /* NO fix is possible in IPv4-pure build. */
141 CPPUNIT_ASSERT_EQUAL(String("2000:800::45"), String(aRequest
->GetHost()));
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
));