1//
2// HTTPClientSessionTest.cpp
3//
4// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
5// and Contributors.
6//
7// SPDX-License-Identifier: BSL-1.0
8//
9
10
11#include "HTTPClientSessionTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/Net/HTTPClientSession.h"
15#include "Poco/Net/HTTPRequest.h"
16#include "Poco/Net/HTTPResponse.h"
17#include "Poco/StreamCopier.h"
18#include "HTTPTestServer.h"
19#include <istream>
20#include <ostream>
21#include <sstream>
22
23
24using Poco::Net::HTTPClientSession;
25using Poco::Net::HTTPRequest;
26using Poco::Net::HTTPResponse;
27using Poco::Net::HTTPMessage;
28using Poco::StreamCopier;
29
30
31HTTPClientSessionTest::HTTPClientSessionTest(const std::string& name): CppUnit::TestCase(name)
32{
33}
34
35
36HTTPClientSessionTest::~HTTPClientSessionTest()
37{
38}
39
40
41void HTTPClientSessionTest::testGetSmall()
42{
43 HTTPTestServer srv;
44 HTTPClientSession s("127.0.0.1", srv.port());
45 HTTPRequest request(HTTPRequest::HTTP_GET, "/small");
46 s.sendRequest(request);
47 HTTPResponse response;
48 std::istream& rs = s.receiveResponse(response);
49 assertTrue (response.getContentLength() == HTTPTestServer::SMALL_BODY.length());
50 assertTrue (response.getContentType() == "text/plain");
51 std::ostringstream ostr;
52 StreamCopier::copyStream(rs, ostr);
53 assertTrue (ostr.str() == HTTPTestServer::SMALL_BODY);
54}
55
56
57void HTTPClientSessionTest::testGetLarge()
58{
59 HTTPTestServer srv;
60 HTTPClientSession s("127.0.0.1", srv.port());
61 HTTPRequest request(HTTPRequest::HTTP_GET, "/large");
62 s.sendRequest(request);
63 HTTPResponse response;
64 std::istream& rs = s.receiveResponse(response);
65 assertTrue (response.getContentLength() == HTTPTestServer::LARGE_BODY.length());
66 assertTrue (response.getContentType() == "text/plain");
67 std::ostringstream ostr;
68 StreamCopier::copyStream(rs, ostr);
69 assertTrue (ostr.str() == HTTPTestServer::LARGE_BODY);
70}
71
72
73void HTTPClientSessionTest::testHead()
74{
75 HTTPTestServer srv;
76 HTTPClientSession s("127.0.0.1", srv.port());
77 HTTPRequest request(HTTPRequest::HTTP_HEAD, "/large");
78 s.sendRequest(request);
79 HTTPResponse response;
80 std::istream& rs = s.receiveResponse(response);
81 assertTrue (response.getContentLength() == HTTPTestServer::LARGE_BODY.length());
82 assertTrue (response.getContentType() == "text/plain");
83 std::ostringstream ostr;
84 assertTrue (StreamCopier::copyStream(rs, ostr) == 0);
85}
86
87
88void HTTPClientSessionTest::testPostSmallIdentity()
89{
90 HTTPTestServer srv;
91 HTTPClientSession s("127.0.0.1", srv.port());
92 HTTPRequest request(HTTPRequest::HTTP_POST, "/echo");
93 std::string body("this is a random request body\r\n0\r\n");
94 request.setContentLength((int) body.length());
95 s.sendRequest(request) << body;
96 HTTPResponse response;
97 std::istream& rs = s.receiveResponse(response);
98 assertTrue (response.getContentLength() == body.length());
99 std::ostringstream ostr;
100 StreamCopier::copyStream(rs, ostr);
101 assertTrue (ostr.str() == body);
102}
103
104
105void HTTPClientSessionTest::testPostLargeIdentity()
106{
107 HTTPTestServer srv;
108 HTTPClientSession s("127.0.0.1", srv.port());
109 HTTPRequest request(HTTPRequest::HTTP_POST, "/echo");
110 std::string body(8000, 'x');
111 body.append("\r\n0\r\n");
112 request.setContentLength((int) body.length());
113 s.sendRequest(request) << body;
114 HTTPResponse response;
115 std::istream& rs = s.receiveResponse(response);
116 assertTrue (response.getContentLength() == body.length());
117 std::ostringstream ostr;
118 StreamCopier::copyStream(rs, ostr);
119 assertTrue (ostr.str() == body);
120}
121
122
123void HTTPClientSessionTest::testPostSmallChunked()
124{
125 HTTPTestServer srv;
126 HTTPClientSession s("127.0.0.1", srv.port());
127 HTTPRequest request(HTTPRequest::HTTP_POST, "/echo");
128 std::string body("this is a random request body");
129 request.setChunkedTransferEncoding(true);
130 s.sendRequest(request) << body;
131 HTTPResponse response;
132 std::istream& rs = s.receiveResponse(response);
133 assertTrue (response.getChunkedTransferEncoding());
134 assertTrue (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
135 std::ostringstream ostr;
136 StreamCopier::copyStream(rs, ostr);
137 assertTrue (ostr.str() == body);
138}
139
140
141void HTTPClientSessionTest::testPostLargeChunked()
142{
143 HTTPTestServer srv;
144 HTTPClientSession s("127.0.0.1", srv.port());
145 HTTPRequest request(HTTPRequest::HTTP_POST, "/echo");
146 std::string body(16000, 'x');
147 request.setChunkedTransferEncoding(true);
148 std::ostream& os = s.sendRequest(request);
149 os << body;
150 os.flush();
151 HTTPResponse response;
152 std::istream& rs = s.receiveResponse(response);
153 assertTrue (response.getChunkedTransferEncoding());
154 assertTrue (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
155 std::ostringstream ostr;
156 StreamCopier::copyStream(rs, ostr, 16000);
157 assertTrue (ostr.str() == body);
158}
159
160
161void HTTPClientSessionTest::testPostSmallClose()
162{
163 HTTPTestServer srv;
164 HTTPClientSession s("127.0.0.1", srv.port());
165 HTTPRequest request(HTTPRequest::HTTP_POST, "/echo");
166 std::string body("this is a random request body");
167 s.sendRequest(request) << body;
168 HTTPResponse response;
169 std::istream& rs = s.receiveResponse(response);
170 assertTrue (!response.getChunkedTransferEncoding());
171 assertTrue (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
172 std::ostringstream ostr;
173 StreamCopier::copyStream(rs, ostr);
174 assertTrue (ostr.str() == body);
175}
176
177
178void HTTPClientSessionTest::testPostLargeClose()
179{
180 HTTPTestServer srv;
181 HTTPClientSession s("127.0.0.1", srv.port());
182 HTTPRequest request(HTTPRequest::HTTP_POST, "/echo");
183 std::string body(8000, 'x');
184 s.sendRequest(request) << body;
185 HTTPResponse response;
186 std::istream& rs = s.receiveResponse(response);
187 assertTrue (!response.getChunkedTransferEncoding());
188 assertTrue (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
189 std::ostringstream ostr;
190 StreamCopier::copyStream(rs, ostr);
191 assertTrue (ostr.str() == body);
192}
193
194
195void HTTPClientSessionTest::testKeepAlive()
196{
197 HTTPTestServer srv;
198 HTTPClientSession s("127.0.0.1", srv.port());
199 s.setKeepAlive(true);
200 HTTPRequest request(HTTPRequest::HTTP_HEAD, "/keepAlive", HTTPMessage::HTTP_1_1);
201 s.sendRequest(request);
202 HTTPResponse response;
203 std::istream& rs1 = s.receiveResponse(response);
204 assertTrue (response.getContentLength() == HTTPTestServer::SMALL_BODY.length());
205 assertTrue (response.getContentType() == "text/plain");
206 assertTrue (response.getKeepAlive());
207 std::ostringstream ostr1;
208 assertTrue (StreamCopier::copyStream(rs1, ostr1) == 0);
209
210 request.setMethod(HTTPRequest::HTTP_GET);
211 request.setURI("/small");
212 s.sendRequest(request);
213 std::istream& rs2 = s.receiveResponse(response);
214 assertTrue (response.getContentLength() == HTTPTestServer::SMALL_BODY.length());
215 assertTrue (response.getKeepAlive());
216 std::ostringstream ostr2;
217 StreamCopier::copyStream(rs2, ostr2);
218 assertTrue (ostr2.str() == HTTPTestServer::SMALL_BODY);
219
220 request.setMethod(HTTPRequest::HTTP_GET);
221 request.setURI("/large");
222 s.sendRequest(request);
223 std::istream& rs3 = s.receiveResponse(response);
224 assertTrue (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
225 assertTrue (response.getChunkedTransferEncoding());
226 assertTrue (response.getKeepAlive());
227 std::ostringstream ostr3;
228 StreamCopier::copyStream(rs3, ostr3);
229 assertTrue (ostr3.str() == HTTPTestServer::LARGE_BODY);
230
231 request.setMethod(HTTPRequest::HTTP_HEAD);
232 request.setURI("/large");
233 s.sendRequest(request);
234 std::istream& rs4= s.receiveResponse(response);
235 assertTrue (response.getContentLength() == HTTPTestServer::LARGE_BODY.length());
236 assertTrue (response.getContentType() == "text/plain");
237 assertTrue (!response.getKeepAlive());
238 std::ostringstream ostr4;
239 assertTrue (StreamCopier::copyStream(rs4, ostr4) == 0);
240}
241
242
243void HTTPClientSessionTest::testProxy()
244{
245 HTTPTestServer srv;
246 HTTPClientSession s("www.somehost.com");
247 s.setProxy("127.0.0.1", srv.port());
248 HTTPRequest request(HTTPRequest::HTTP_GET, "/large");
249 s.sendRequest(request);
250 HTTPResponse response;
251 std::istream& rs = s.receiveResponse(response);
252 assertTrue (response.getContentLength() == HTTPTestServer::LARGE_BODY.length());
253 assertTrue (response.getContentType() == "text/plain");
254 std::ostringstream ostr;
255 StreamCopier::copyStream(rs, ostr);
256 assertTrue (ostr.str() == HTTPTestServer::LARGE_BODY);
257}
258
259
260void HTTPClientSessionTest::testProxyAuth()
261{
262 HTTPTestServer srv;
263 HTTPClientSession s("www.somehost.com");
264 s.setProxy("127.0.0.1", srv.port());
265 s.setProxyCredentials("user", "pass");
266 HTTPRequest request(HTTPRequest::HTTP_GET, "/large");
267 s.sendRequest(request);
268 HTTPResponse response;
269 std::istream& rs = s.receiveResponse(response);
270 assertTrue (response.getContentLength() == HTTPTestServer::LARGE_BODY.length());
271 assertTrue (response.getContentType() == "text/plain");
272 std::ostringstream ostr;
273 StreamCopier::copyStream(rs, ostr);
274 assertTrue (ostr.str() == HTTPTestServer::LARGE_BODY);
275 std::string r = srv.lastRequest();
276 assertTrue (r.find("Proxy-Authorization: Basic dXNlcjpwYXNz\r\n") != std::string::npos);
277}
278
279
280void HTTPClientSessionTest::testBypassProxy()
281{
282 HTTPClientSession::ProxyConfig proxyConfig;
283 proxyConfig.host = "proxy.domain.com";
284 proxyConfig.port = 80;
285 proxyConfig.nonProxyHosts = "localhost|127\\.0\\.0\\.1";
286
287 HTTPClientSession s1("127.0.0.1", 80);
288 s1.setProxyConfig(proxyConfig);
289 assertTrue (s1.bypassProxy());
290
291 HTTPClientSession s2("127.0.0.1", 80);
292 s2.setProxyConfig(proxyConfig);
293 assertTrue (s2.bypassProxy());
294
295 HTTPClientSession s3("www.appinf.com", 80);
296 s3.setProxyConfig(proxyConfig);
297 assertTrue (!s3.bypassProxy());
298}
299
300
301void HTTPClientSessionTest::testExpectContinue()
302{
303 HTTPTestServer srv;
304 HTTPClientSession s("127.0.0.1", srv.port());
305 HTTPRequest request(HTTPRequest::HTTP_POST, "/expect");
306 std::string body("this is a random request body\r\n0\r\n");
307 request.setContentLength((int) body.length());
308 request.setExpectContinue(true);
309 s.sendRequest(request) << body;
310 HTTPResponse response;
311 assertTrue (s.peekResponse(response));
312 assertTrue (response.getStatus() == HTTPResponse::HTTP_CONTINUE);
313 std::istream& rs = s.receiveResponse(response);
314 assertTrue (response.getStatus() == HTTPResponse::HTTP_OK);
315 assertTrue (response.getContentLength() == body.length());
316 std::ostringstream ostr;
317 StreamCopier::copyStream(rs, ostr);
318 assertTrue (ostr.str() == body);
319}
320
321
322void HTTPClientSessionTest::testExpectContinueFail()
323{
324 HTTPTestServer srv;
325 HTTPClientSession s("127.0.0.1", srv.port());
326 HTTPRequest request(HTTPRequest::HTTP_POST, "/fail");
327 std::string body("this is a random request body\r\n0\r\n");
328 request.setContentLength((int) body.length());
329 request.setExpectContinue(true);
330 s.sendRequest(request) << body;
331 HTTPResponse response;
332 assertTrue (!s.peekResponse(response));
333 assertTrue (response.getStatus() == HTTPResponse::HTTP_BAD_REQUEST);
334 std::istream& rs = s.receiveResponse(response);
335 assertTrue (response.getStatus() == HTTPResponse::HTTP_BAD_REQUEST);
336 std::ostringstream ostr;
337 StreamCopier::copyStream(rs, ostr);
338 assertTrue (ostr.str().empty());
339}
340
341
342void HTTPClientSessionTest::setUp()
343{
344}
345
346
347void HTTPClientSessionTest::tearDown()
348{
349}
350
351
352CppUnit::Test* HTTPClientSessionTest::suite()
353{
354 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("HTTPClientSessionTest");
355
356 CppUnit_addTest(pSuite, HTTPClientSessionTest, testGetSmall);
357 CppUnit_addTest(pSuite, HTTPClientSessionTest, testGetLarge);
358 CppUnit_addTest(pSuite, HTTPClientSessionTest, testHead);
359 CppUnit_addTest(pSuite, HTTPClientSessionTest, testPostSmallIdentity);
360 CppUnit_addTest(pSuite, HTTPClientSessionTest, testPostLargeIdentity);
361 CppUnit_addTest(pSuite, HTTPClientSessionTest, testPostSmallChunked);
362 CppUnit_addTest(pSuite, HTTPClientSessionTest, testPostLargeChunked);
363 CppUnit_addTest(pSuite, HTTPClientSessionTest, testPostSmallClose);
364 CppUnit_addTest(pSuite, HTTPClientSessionTest, testPostLargeClose);
365 CppUnit_addTest(pSuite, HTTPClientSessionTest, testKeepAlive);
366 CppUnit_addTest(pSuite, HTTPClientSessionTest, testProxy);
367 CppUnit_addTest(pSuite, HTTPClientSessionTest, testProxyAuth);
368 CppUnit_addTest(pSuite, HTTPClientSessionTest, testBypassProxy);
369 CppUnit_addTest(pSuite, HTTPClientSessionTest, testExpectContinue);
370 CppUnit_addTest(pSuite, HTTPClientSessionTest, testExpectContinueFail);
371
372 return pSuite;
373}
374