Logo Search packages:      
Sourcecode: poco version File versions  Download package

HTTPResponseTest.cpp

//
// HTTPResponseTest.cpp
//
// $Id: //poco/1.2/Net/testsuite/src/HTTPResponseTest.cpp#1 $
//
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
// 
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//


#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();
      assert (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();
      assert (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);
      assert (response.getStatus() == HTTPResponse::HTTP_INTERNAL_SERVER_ERROR);
      assert (response.getReason() == "Internal Server Error");
      assert (response.getVersion() == HTTPMessage::HTTP_1_1);
      assert (response.empty());
      assert (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);
      assert (response.getStatus() == HTTPResponse::HTTP_MOVED_PERMANENTLY);
      assert (response.getReason() == "Moved Permanently");
      assert (response.getVersion() == HTTPMessage::HTTP_1_0);
      assert (response.size() == 2);
      assert (response["Location"] == "http://www.appinf.com/index.html");
      assert (response["Server"] == "Poco/1.0");
      assert (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);
      assert (cookies.size() == 1);
      assert (cookie1.getVersion() == cookies[0].getVersion());
      assert (cookie1.getName() == cookies[0].getName());
      assert (cookie1.getValue() == cookies[0].getValue());
      assert (cookie1.getComment() == cookies[0].getComment());
      assert (cookie1.getDomain() == cookies[0].getDomain());
      assert (cookie1.getPath() == cookies[0].getPath());
      assert (cookie1.getSecure() == cookies[0].getSecure());
      assert (cookie1.getMaxAge() == cookies[0].getMaxAge());
      
      HTTPCookie cookie2("cookie2", "value2");
      cookie2.setVersion(1);
      cookie2.setMaxAge(42);
      cookie2.setSecure(true);
      response.addCookie(cookie2);
      response.getCookies(cookies);
      assert (cookies.size() == 2);
      HTTPCookie cookie2a;
      if (cookies[0].getName() == cookie2.getName())
            cookie2a = cookies[0];
      else
            cookie2a = cookies[1];
      assert (cookie2.getVersion() == cookie2a.getVersion());
      assert (cookie2.getName() == cookie2a.getName());
      assert (cookie2.getValue() == cookie2a.getValue());
      assert (cookie2.getComment() == cookie2a.getComment());
      assert (cookie2.getDomain() == cookie2a.getDomain());
      assert (cookie2.getPath() == cookie2a.getPath());
      assert (cookie2.getSecure() == cookie2a.getSecure());
      assert (cookie2.getMaxAge() == cookie2a.getMaxAge());
}


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, testInvalid1);
      CppUnit_addTest(pSuite, HTTPResponseTest, testInvalid2);
      CppUnit_addTest(pSuite, HTTPResponseTest, testInvalid3);
      CppUnit_addTest(pSuite, HTTPResponseTest, testCookies);

      return pSuite;
}

Generated by  Doxygen 1.6.0   Back to index