| 1 | // |
| 2 | // HTTPResponseTest.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 "HTTPResponseTest.h" |
| 12 | #include "Poco/CppUnit/TestCaller.h" |
| 13 | #include "Poco/CppUnit/TestSuite.h" |
| 14 | #include "Poco/Net/HTTPResponse.h" |
| 15 | #include "Poco/Net/HTTPCookie.h" |
| 16 | #include "Poco/Net/NetException.h" |
| 17 | #include <sstream> |
| 18 | |
| 19 | |
| 20 | using Poco::Net::HTTPResponse; |
| 21 | using Poco::Net::HTTPMessage; |
| 22 | using Poco::Net::HTTPCookie; |
| 23 | using Poco::Net::MessageException; |
| 24 | |
| 25 | |
| 26 | HTTPResponseTest::HTTPResponseTest(const std::string& name): CppUnit::TestCase(name) |
| 27 | { |
| 28 | } |
| 29 | |
| 30 | |
| 31 | HTTPResponseTest::~HTTPResponseTest() |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | |
| 36 | void HTTPResponseTest::testWrite1() |
| 37 | { |
| 38 | HTTPResponse response; |
| 39 | std::ostringstream ostr; |
| 40 | response.write(ostr); |
| 41 | std::string s = ostr.str(); |
| 42 | assertTrue (s == "HTTP/1.0 200 OK\r\n\r\n" ); |
| 43 | } |
| 44 | |
| 45 | |
| 46 | void HTTPResponseTest::testWrite2() |
| 47 | { |
| 48 | HTTPResponse response(HTTPMessage::HTTP_1_1, HTTPResponse::HTTP_MOVED_PERMANENTLY); |
| 49 | response.set("Location" , "http://www.appinf.com/index.html" ); |
| 50 | response.set("Server" , "Poco/1.0" ); |
| 51 | std::ostringstream ostr; |
| 52 | response.write(ostr); |
| 53 | std::string s = ostr.str(); |
| 54 | assertTrue (s == "HTTP/1.1 301 Moved Permanently\r\nLocation: http://www.appinf.com/index.html\r\nServer: Poco/1.0\r\n\r\n" ); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | void HTTPResponseTest::testRead1() |
| 59 | { |
| 60 | std::string s("HTTP/1.1 500 Internal Server Error\r\n\r\n" ); |
| 61 | std::istringstream istr(s); |
| 62 | HTTPResponse response; |
| 63 | response.read(istr); |
| 64 | assertTrue (response.getStatus() == HTTPResponse::HTTP_INTERNAL_SERVER_ERROR); |
| 65 | assertTrue (response.getReason() == "Internal Server Error" ); |
| 66 | assertTrue (response.getVersion() == HTTPMessage::HTTP_1_1); |
| 67 | assertTrue (response.empty()); |
| 68 | assertTrue (istr.get() == -1); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | void HTTPResponseTest::testRead2() |
| 73 | { |
| 74 | std::string s("HTTP/1.0 301 Moved Permanently\r\nLocation: http://www.appinf.com/index.html\r\nServer: Poco/1.0\r\n\r\n" ); |
| 75 | std::istringstream istr(s); |
| 76 | HTTPResponse response; |
| 77 | response.read(istr); |
| 78 | assertTrue (response.getStatus() == HTTPResponse::HTTP_MOVED_PERMANENTLY); |
| 79 | assertTrue (response.getReason() == "Moved Permanently" ); |
| 80 | assertTrue (response.getVersion() == HTTPMessage::HTTP_1_0); |
| 81 | assertTrue (response.size() == 2); |
| 82 | assertTrue (response["Location" ] == "http://www.appinf.com/index.html" ); |
| 83 | assertTrue (response["Server" ] == "Poco/1.0" ); |
| 84 | assertTrue (istr.get() == -1); |
| 85 | } |
| 86 | |
| 87 | |
| 88 | void HTTPResponseTest::testRead3() |
| 89 | { |
| 90 | std::string s("HTTP/1.1 200 \r\nContent-Length: 0\r\n\r\n" ); |
| 91 | std::istringstream istr(s); |
| 92 | HTTPResponse response; |
| 93 | response.read(istr); |
| 94 | assertTrue (response.getVersion() == HTTPMessage::HTTP_1_1); |
| 95 | assertTrue (response.getStatus() == HTTPResponse::HTTP_OK); |
| 96 | assertTrue (response.getReason() == "" ); |
| 97 | assertTrue (response.size() == 1); |
| 98 | assertTrue (response.getContentLength() == 0); |
| 99 | assertTrue (istr.get() == -1); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | void HTTPResponseTest::testInvalid1() |
| 104 | { |
| 105 | std::string s(256, 'x'); |
| 106 | std::istringstream istr(s); |
| 107 | HTTPResponse response; |
| 108 | try |
| 109 | { |
| 110 | response.read(istr); |
| 111 | fail("inavalid response - must throw" ); |
| 112 | } |
| 113 | catch (MessageException&) |
| 114 | { |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | |
| 119 | void HTTPResponseTest::testInvalid2() |
| 120 | { |
| 121 | std::string s("HTTP/1.1 200 " ); |
| 122 | s.append(1000, 'x'); |
| 123 | s.append("\r\n\r\n" ); |
| 124 | std::istringstream istr(s); |
| 125 | HTTPResponse response; |
| 126 | try |
| 127 | { |
| 128 | response.read(istr); |
| 129 | fail("inavalid response - must throw" ); |
| 130 | } |
| 131 | catch (MessageException&) |
| 132 | { |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | |
| 137 | void HTTPResponseTest::testInvalid3() |
| 138 | { |
| 139 | std::string s("HTTP/1.0 " ); |
| 140 | s.append(8000, 'x'); |
| 141 | s.append("\r\n\r\n" ); |
| 142 | std::istringstream istr(s); |
| 143 | HTTPResponse response; |
| 144 | try |
| 145 | { |
| 146 | response.read(istr); |
| 147 | fail("inavalid response - must throw" ); |
| 148 | } |
| 149 | catch (MessageException&) |
| 150 | { |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | |
| 155 | void HTTPResponseTest::testCookies() |
| 156 | { |
| 157 | HTTPResponse response; |
| 158 | HTTPCookie cookie1("cookie1" , "value1" ); |
| 159 | response.addCookie(cookie1); |
| 160 | std::vector<HTTPCookie> cookies; |
| 161 | response.getCookies(cookies); |
| 162 | assertTrue (cookies.size() == 1); |
| 163 | assertTrue (cookie1.getVersion() == cookies[0].getVersion()); |
| 164 | assertTrue (cookie1.getName() == cookies[0].getName()); |
| 165 | assertTrue (cookie1.getValue() == cookies[0].getValue()); |
| 166 | assertTrue (cookie1.getComment() == cookies[0].getComment()); |
| 167 | assertTrue (cookie1.getDomain() == cookies[0].getDomain()); |
| 168 | assertTrue (cookie1.getPath() == cookies[0].getPath()); |
| 169 | assertTrue (cookie1.getSecure() == cookies[0].getSecure()); |
| 170 | assertTrue (cookie1.getMaxAge() == cookies[0].getMaxAge()); |
| 171 | |
| 172 | HTTPCookie cookie2("cookie2" , "value2" ); |
| 173 | cookie2.setVersion(1); |
| 174 | cookie2.setMaxAge(42); |
| 175 | cookie2.setSecure(true); |
| 176 | response.addCookie(cookie2); |
| 177 | response.getCookies(cookies); |
| 178 | assertTrue (cookies.size() == 2); |
| 179 | HTTPCookie cookie2a; |
| 180 | if (cookies[0].getName() == cookie2.getName()) |
| 181 | cookie2a = cookies[0]; |
| 182 | else |
| 183 | cookie2a = cookies[1]; |
| 184 | assertTrue (cookie2.getVersion() == cookie2a.getVersion()); |
| 185 | assertTrue (cookie2.getName() == cookie2a.getName()); |
| 186 | assertTrue (cookie2.getValue() == cookie2a.getValue()); |
| 187 | assertTrue (cookie2.getComment() == cookie2a.getComment()); |
| 188 | assertTrue (cookie2.getDomain() == cookie2a.getDomain()); |
| 189 | assertTrue (cookie2.getPath() == cookie2a.getPath()); |
| 190 | assertTrue (cookie2.getSecure() == cookie2a.getSecure()); |
| 191 | assertTrue (cookie2.getMaxAge() == cookie2a.getMaxAge()); |
| 192 | |
| 193 | HTTPResponse response2; |
| 194 | response2.add("Set-Cookie" , "name1=value1" ); |
| 195 | response2.add("Set-cookie" , "name2=value2" ); |
| 196 | cookies.clear(); |
| 197 | response2.getCookies(cookies); |
| 198 | assertTrue (cookies.size() == 2); |
| 199 | assertTrue (((cookies[0].getName() == "name1" ) && (cookies[1].getName() == "name2" )) |
| 200 | || ((cookies[0].getName() == "name2" ) && (cookies[1].getName() == "name1" ))); |
| 201 | } |
| 202 | |
| 203 | |
| 204 | void HTTPResponseTest::setUp() |
| 205 | { |
| 206 | } |
| 207 | |
| 208 | |
| 209 | void HTTPResponseTest::tearDown() |
| 210 | { |
| 211 | } |
| 212 | |
| 213 | |
| 214 | CppUnit::Test* HTTPResponseTest::suite() |
| 215 | { |
| 216 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("HTTPResponseTest" ); |
| 217 | |
| 218 | CppUnit_addTest(pSuite, HTTPResponseTest, testWrite1); |
| 219 | CppUnit_addTest(pSuite, HTTPResponseTest, testWrite2); |
| 220 | CppUnit_addTest(pSuite, HTTPResponseTest, testRead1); |
| 221 | CppUnit_addTest(pSuite, HTTPResponseTest, testRead2); |
| 222 | CppUnit_addTest(pSuite, HTTPResponseTest, testRead3); |
| 223 | CppUnit_addTest(pSuite, HTTPResponseTest, testInvalid1); |
| 224 | CppUnit_addTest(pSuite, HTTPResponseTest, testInvalid2); |
| 225 | CppUnit_addTest(pSuite, HTTPResponseTest, testInvalid3); |
| 226 | CppUnit_addTest(pSuite, HTTPResponseTest, testCookies); |
| 227 | |
| 228 | return pSuite; |
| 229 | } |
| 230 | |