1//
2// HTTPRequestTest.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 "HTTPRequestTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/Net/HTTPRequest.h"
15#include "Poco/Net/NetException.h"
16#include <sstream>
17
18
19using Poco::Net::HTTPRequest;
20using Poco::Net::HTTPMessage;
21using Poco::Net::MessageException;
22using Poco::Net::NameValueCollection;
23
24
25HTTPRequestTest::HTTPRequestTest(const std::string& name): CppUnit::TestCase(name)
26{
27}
28
29
30HTTPRequestTest::~HTTPRequestTest()
31{
32}
33
34
35void HTTPRequestTest::testWrite1()
36{
37 HTTPRequest request;
38 std::ostringstream ostr;
39 request.write(ostr);
40 std::string s = ostr.str();
41 assertTrue (s == "GET / HTTP/1.0\r\n\r\n");
42}
43
44
45void HTTPRequestTest::testWrite2()
46{
47 HTTPRequest request(HTTPRequest::HTTP_HEAD, "/index.html", HTTPMessage::HTTP_1_1);
48 request.setHost("localhost", 80);
49 request.setKeepAlive(true);
50 request.set("User-Agent", "Poco");
51 std::ostringstream ostr;
52 request.write(ostr);
53 std::string s = ostr.str();
54 assertTrue (s == "HEAD /index.html HTTP/1.1\r\nHost: localhost\r\nConnection: Keep-Alive\r\nUser-Agent: Poco\r\n\r\n");
55}
56
57
58void HTTPRequestTest::testWrite3()
59{
60 HTTPRequest request(HTTPRequest::HTTP_POST, "/test.cgi", HTTPMessage::HTTP_1_1);
61 request.setHost("localhost", 8000);
62 request.setKeepAlive(false);
63 request.set("User-Agent", "Poco");
64 request.setContentLength(100);
65 request.setContentType("text/plain");
66 std::ostringstream ostr;
67 request.write(ostr);
68 std::string s = ostr.str();
69 assertTrue (s == "POST /test.cgi HTTP/1.1\r\nHost: localhost:8000\r\nConnection: Close\r\nUser-Agent: Poco\r\nContent-Length: 100\r\nContent-Type: text/plain\r\n\r\n");
70}
71
72
73void HTTPRequestTest::testWrite4()
74{
75 HTTPRequest request(HTTPRequest::HTTP_HEAD, "/index.html", HTTPMessage::HTTP_1_1);
76 request.setHost("fe80::1", 88);
77 request.setKeepAlive(true);
78 request.set("User-Agent", "Poco");
79 std::ostringstream ostr;
80 request.write(ostr);
81 std::string s = ostr.str();
82 assertTrue (s == "HEAD /index.html HTTP/1.1\r\nHost: [fe80::1]:88\r\nConnection: Keep-Alive\r\nUser-Agent: Poco\r\n\r\n");
83}
84
85
86void HTTPRequestTest::testRead1()
87{
88 std::string s("GET / HTTP/1.0\r\n\r\n");
89 std::istringstream istr(s);
90 HTTPRequest request;
91 request.read(istr);
92 assertTrue (request.getMethod() == HTTPRequest::HTTP_GET);
93 assertTrue (request.getURI() == "/");
94 assertTrue (request.getVersion() == HTTPMessage::HTTP_1_0);
95 assertTrue (request.empty());
96 assertTrue (istr.get() == -1);
97}
98
99
100void HTTPRequestTest::testRead2()
101{
102 std::string s("HEAD /index.html HTTP/1.1\r\nConnection: Keep-Alive\r\nHost: localhost\r\nUser-Agent: Poco\r\n\r\n");
103 std::istringstream istr(s);
104 HTTPRequest request;
105 request.read(istr);
106 assertTrue (request.getMethod() == HTTPRequest::HTTP_HEAD);
107 assertTrue (request.getURI() == "/index.html");
108 assertTrue (request.getVersion() == HTTPMessage::HTTP_1_1);
109 assertTrue (request.size() == 3);
110 assertTrue (request["Connection"] == "Keep-Alive");
111 assertTrue (request["Host"] == "localhost");
112 assertTrue (request["User-Agent"] == "Poco");
113 assertTrue (istr.get() == -1);
114}
115
116
117void HTTPRequestTest::testRead3()
118{
119 std::string s("POST /test.cgi HTTP/1.1\r\nConnection: Close\r\nContent-Length: 100\r\nContent-Type: text/plain\r\nHost: localhost:8000\r\nUser-Agent: Poco\r\n\r\n");
120 std::istringstream istr(s);
121 HTTPRequest request;
122 request.read(istr);
123 assertTrue (request.getMethod() == HTTPRequest::HTTP_POST);
124 assertTrue (request.getURI() == "/test.cgi");
125 assertTrue (request.getVersion() == HTTPMessage::HTTP_1_1);
126 assertTrue (request.size() == 5);
127 assertTrue (request["Connection"] == "Close");
128 assertTrue (request["Host"] == "localhost:8000");
129 assertTrue (request["User-Agent"] == "Poco");
130 assertTrue (request.getContentType() == "text/plain");
131 assertTrue (request.getContentLength() == 100);
132 assertTrue (istr.get() == -1);
133}
134
135
136void HTTPRequestTest::testRead4()
137{
138 std::string s("POST /test.cgi HTTP/1.1\r\nConnection: Close\r\nContent-Length: 100 \r\nContent-Type: text/plain\r\nHost: localhost:8000\r\nUser-Agent: Poco\r\n\r\n");
139 std::istringstream istr(s);
140 HTTPRequest request;
141 request.read(istr);
142 assertTrue (request.getMethod() == HTTPRequest::HTTP_POST);
143 assertTrue (request.getURI() == "/test.cgi");
144 assertTrue (request.getVersion() == HTTPMessage::HTTP_1_1);
145 assertTrue (request.size() == 5);
146 assertTrue (request["Connection"] == "Close");
147 assertTrue (request["Host"] == "localhost:8000");
148 assertTrue (request["User-Agent"] == "Poco");
149 assertTrue (request.getContentType() == "text/plain");
150 assertTrue (request.getContentLength() == 100);
151 assertTrue (istr.get() == -1);
152}
153
154
155void HTTPRequestTest::testInvalid1()
156{
157 std::string s(256, 'x');
158 std::istringstream istr(s);
159 HTTPRequest request;
160 try
161 {
162 request.read(istr);
163 fail("inavalid request - must throw");
164 }
165 catch (MessageException&)
166 {
167 }
168}
169
170
171void HTTPRequestTest::testInvalid2()
172{
173 std::string s("GET ");
174 s.append(8000, 'x');
175 s.append("HTTP/1.0");
176 std::istringstream istr(s);
177 HTTPRequest request;
178 try
179 {
180 request.read(istr);
181 fail("inavalid request - must throw");
182 }
183 catch (MessageException&)
184 {
185 }
186}
187
188
189void HTTPRequestTest::testInvalid3()
190{
191 std::string s("GET / HTTP/1.10");
192 std::istringstream istr(s);
193 HTTPRequest request;
194 try
195 {
196 request.read(istr);
197 fail("inavalid request - must throw");
198 }
199 catch (MessageException&)
200 {
201 }
202}
203
204
205void HTTPRequestTest::testCookies()
206{
207 HTTPRequest request1;
208 NameValueCollection cookies1;
209 cookies1.add("cookie1", "value1");
210 request1.setCookies(cookies1);
211 assertTrue (request1["Cookie"] == "cookie1=value1");
212
213 HTTPRequest request2;
214 NameValueCollection cookies2;
215 cookies2.add("cookie2", "value2");
216 cookies2.add("cookie3", "value3");
217 request2.setCookies(cookies2);
218 assertTrue (request2["Cookie"] == "cookie2=value2; cookie3=value3");
219
220 request1.setCookies(cookies2);
221 NameValueCollection cookies3;
222 request1.getCookies(cookies3);
223 assertTrue (cookies3.size() == 3);
224 assertTrue (cookies3["cookie1"] == "value1");
225 assertTrue (cookies3["cookie2"] == "value2");
226 assertTrue (cookies3["cookie3"] == "value3");
227
228 HTTPRequest request3;
229 request3.add("Cookie", "cookie1=value1");
230 request3.add("cookie", "cookie2=value2");
231 NameValueCollection cookies4;
232 request3.getCookies(cookies4);
233 assertTrue (cookies4.size() == 2);
234 assertTrue (cookies4["cookie1"] == "value1");
235 assertTrue (cookies4["cookie2"] == "value2");
236}
237
238
239void HTTPRequestTest::setUp()
240{
241}
242
243
244void HTTPRequestTest::tearDown()
245{
246}
247
248
249CppUnit::Test* HTTPRequestTest::suite()
250{
251 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("HTTPRequestTest");
252
253 CppUnit_addTest(pSuite, HTTPRequestTest, testWrite1);
254 CppUnit_addTest(pSuite, HTTPRequestTest, testWrite2);
255 CppUnit_addTest(pSuite, HTTPRequestTest, testWrite3);
256 CppUnit_addTest(pSuite, HTTPRequestTest, testWrite4);
257 CppUnit_addTest(pSuite, HTTPRequestTest, testRead1);
258 CppUnit_addTest(pSuite, HTTPRequestTest, testRead2);
259 CppUnit_addTest(pSuite, HTTPRequestTest, testRead3);
260 CppUnit_addTest(pSuite, HTTPRequestTest, testRead4);
261 CppUnit_addTest(pSuite, HTTPRequestTest, testInvalid1);
262 CppUnit_addTest(pSuite, HTTPRequestTest, testInvalid2);
263 CppUnit_addTest(pSuite, HTTPRequestTest, testInvalid3);
264 CppUnit_addTest(pSuite, HTTPRequestTest, testCookies);
265
266 return pSuite;
267}
268