poco/Net/testsuite/src/HTTPResponseTest.cpp
Aleksandar Fabijanic c4e676d36d
Feature net udp (#2347)
* add PMTU discovery #2329

* add socket gather/scatter capabilities #2330 (win, udp)

* enable WSAPoll

* add FastMemoryPool

* add receiveFrom() with native args

* allow copying of StringTokenizer

* add AtomicFlag and SpinlockMutex

* update .gitignore

* UDPServer and client #2343 (windows)

* fix warnings

* fix warnings

* regenerate Net VS solutions

* regenerate CppUnit projects/solutions

* clang fixes

* gcc fixes

* try to fix travis

* more travis fixes

* more travis fixes

* handle UDPClient exception

* fix makefiles and init order warnings

* add UNIX gather/scatter sendto/recvfrom implementations and tests

* run travis tests as sudo

* try to run tests as sudo, 2nd attempt

* fix warning

* use mutex in reactor

* lock-order-inversion in SocketReactor #2346

* add PMTU discovery #2329 (linux)

* ICMPSocket does not check reply address #1921

* remove some ignored tests

* add PMTU discovery #2329 (reconcile logic with #1921)

* fix native receiveFrome()

* reinstate ignoring of proxy errors

* add testMTU to ignore list

* add include atomic

* NTPClient not checking reply address #2348

* some ICMP/MTU fixes

* UDPSocketReader cleanup

* resolve some socket inheritance warnings

* add NTP time sync to ignored tests

* SocketNotifier not thread-safe #2345

* prevent x64 samples build attempt for win32

* build TestApp and Library

* fix ICMP tests

* regen VS projects

* regen VS projects and add missing 2012 files

* remove debug prints
2018-06-02 14:02:33 -05:00

230 lines
6.1 KiB
C++

//
// HTTPResponseTest.cpp
//
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "HTTPResponseTest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Poco/Net/HTTPResponse.h"
#include "Poco/Net/HTTPCookie.h"
#include "Poco/Net/NetException.h"
#include <sstream>
using Poco::Net::HTTPResponse;
using Poco::Net::HTTPMessage;
using Poco::Net::HTTPCookie;
using Poco::Net::MessageException;
HTTPResponseTest::HTTPResponseTest(const std::string& name): CppUnit::TestCase(name)
{
}
HTTPResponseTest::~HTTPResponseTest()
{
}
void HTTPResponseTest::testWrite1()
{
HTTPResponse response;
std::ostringstream ostr;
response.write(ostr);
std::string s = ostr.str();
assertTrue (s == "HTTP/1.0 200 OK\r\n\r\n");
}
void HTTPResponseTest::testWrite2()
{
HTTPResponse response(HTTPMessage::HTTP_1_1, HTTPResponse::HTTP_MOVED_PERMANENTLY);
response.set("Location", "http://www.appinf.com/index.html");
response.set("Server", "Poco/1.0");
std::ostringstream ostr;
response.write(ostr);
std::string s = ostr.str();
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");
}
void HTTPResponseTest::testRead1()
{
std::string s("HTTP/1.1 500 Internal Server Error\r\n\r\n");
std::istringstream istr(s);
HTTPResponse response;
response.read(istr);
assertTrue (response.getStatus() == HTTPResponse::HTTP_INTERNAL_SERVER_ERROR);
assertTrue (response.getReason() == "Internal Server Error");
assertTrue (response.getVersion() == HTTPMessage::HTTP_1_1);
assertTrue (response.empty());
assertTrue (istr.get() == -1);
}
void HTTPResponseTest::testRead2()
{
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");
std::istringstream istr(s);
HTTPResponse response;
response.read(istr);
assertTrue (response.getStatus() == HTTPResponse::HTTP_MOVED_PERMANENTLY);
assertTrue (response.getReason() == "Moved Permanently");
assertTrue (response.getVersion() == HTTPMessage::HTTP_1_0);
assertTrue (response.size() == 2);
assertTrue (response["Location"] == "http://www.appinf.com/index.html");
assertTrue (response["Server"] == "Poco/1.0");
assertTrue (istr.get() == -1);
}
void HTTPResponseTest::testRead3()
{
std::string s("HTTP/1.1 200 \r\nContent-Length: 0\r\n\r\n");
std::istringstream istr(s);
HTTPResponse response;
response.read(istr);
assertTrue (response.getVersion() == HTTPMessage::HTTP_1_1);
assertTrue (response.getStatus() == HTTPResponse::HTTP_OK);
assertTrue (response.getReason() == "");
assertTrue (response.size() == 1);
assertTrue (response.getContentLength() == 0);
assertTrue (istr.get() == -1);
}
void HTTPResponseTest::testInvalid1()
{
std::string s(256, 'x');
std::istringstream istr(s);
HTTPResponse response;
try
{
response.read(istr);
fail("inavalid response - must throw");
}
catch (MessageException&)
{
}
}
void HTTPResponseTest::testInvalid2()
{
std::string s("HTTP/1.1 200 ");
s.append(1000, 'x');
s.append("\r\n\r\n");
std::istringstream istr(s);
HTTPResponse response;
try
{
response.read(istr);
fail("inavalid response - must throw");
}
catch (MessageException&)
{
}
}
void HTTPResponseTest::testInvalid3()
{
std::string s("HTTP/1.0 ");
s.append(8000, 'x');
s.append("\r\n\r\n");
std::istringstream istr(s);
HTTPResponse response;
try
{
response.read(istr);
fail("inavalid response - must throw");
}
catch (MessageException&)
{
}
}
void HTTPResponseTest::testCookies()
{
HTTPResponse response;
HTTPCookie cookie1("cookie1", "value1");
response.addCookie(cookie1);
std::vector<HTTPCookie> cookies;
response.getCookies(cookies);
assertTrue (cookies.size() == 1);
assertTrue (cookie1.getVersion() == cookies[0].getVersion());
assertTrue (cookie1.getName() == cookies[0].getName());
assertTrue (cookie1.getValue() == cookies[0].getValue());
assertTrue (cookie1.getComment() == cookies[0].getComment());
assertTrue (cookie1.getDomain() == cookies[0].getDomain());
assertTrue (cookie1.getPath() == cookies[0].getPath());
assertTrue (cookie1.getSecure() == cookies[0].getSecure());
assertTrue (cookie1.getMaxAge() == cookies[0].getMaxAge());
HTTPCookie cookie2("cookie2", "value2");
cookie2.setVersion(1);
cookie2.setMaxAge(42);
cookie2.setSecure(true);
response.addCookie(cookie2);
response.getCookies(cookies);
assertTrue (cookies.size() == 2);
HTTPCookie cookie2a;
if (cookies[0].getName() == cookie2.getName())
cookie2a = cookies[0];
else
cookie2a = cookies[1];
assertTrue (cookie2.getVersion() == cookie2a.getVersion());
assertTrue (cookie2.getName() == cookie2a.getName());
assertTrue (cookie2.getValue() == cookie2a.getValue());
assertTrue (cookie2.getComment() == cookie2a.getComment());
assertTrue (cookie2.getDomain() == cookie2a.getDomain());
assertTrue (cookie2.getPath() == cookie2a.getPath());
assertTrue (cookie2.getSecure() == cookie2a.getSecure());
assertTrue (cookie2.getMaxAge() == cookie2a.getMaxAge());
HTTPResponse response2;
response2.add("Set-Cookie", "name1=value1");
response2.add("Set-cookie", "name2=value2");
cookies.clear();
response2.getCookies(cookies);
assertTrue (cookies.size() == 2);
assertTrue (((cookies[0].getName() == "name1") && (cookies[1].getName() == "name2"))
|| ((cookies[0].getName() == "name2") && (cookies[1].getName() == "name1")));
}
void HTTPResponseTest::setUp()
{
}
void HTTPResponseTest::tearDown()
{
}
CppUnit::Test* HTTPResponseTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("HTTPResponseTest");
CppUnit_addTest(pSuite, HTTPResponseTest, testWrite1);
CppUnit_addTest(pSuite, HTTPResponseTest, testWrite2);
CppUnit_addTest(pSuite, HTTPResponseTest, testRead1);
CppUnit_addTest(pSuite, HTTPResponseTest, testRead2);
CppUnit_addTest(pSuite, HTTPResponseTest, testRead3);
CppUnit_addTest(pSuite, HTTPResponseTest, testInvalid1);
CppUnit_addTest(pSuite, HTTPResponseTest, testInvalid2);
CppUnit_addTest(pSuite, HTTPResponseTest, testInvalid3);
CppUnit_addTest(pSuite, HTTPResponseTest, testCookies);
return pSuite;
}