2
// HTTPStreamFactoryTest.cpp
4
// $Id: //poco/1.2/Net/testsuite/src/HTTPStreamFactoryTest.cpp#1 $
6
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
9
// Permission is hereby granted, free of charge, to any person or organization
10
// obtaining a copy of the software and accompanying documentation covered by
11
// this license (the "Software") to use, reproduce, display, distribute,
12
// execute, and transmit the Software, and to prepare derivative works of the
13
// Software, and to permit third-parties to whom the Software is furnished to
14
// do so, all subject to the following:
16
// The copyright notices in the Software and this entire statement, including
17
// the above license grant, this restriction and the following disclaimer,
18
// must be included in all copies of the Software, in whole or in part, and
19
// all derivative works of the Software, unless such copies or derivative
20
// works are solely in the form of machine-executable object code generated by
21
// a source language processor.
23
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
26
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
27
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
28
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29
// DEALINGS IN THE SOFTWARE.
33
#include "HTTPStreamFactoryTest.h"
34
#include "CppUnit/TestCaller.h"
35
#include "CppUnit/TestSuite.h"
36
#include "Poco/Net/HTTPStreamFactory.h"
37
#include "Poco/Net/NetException.h"
39
#include "Poco/StreamCopier.h"
40
#include "HTTPTestServer.h"
45
using Poco::Net::HTTPStreamFactory;
46
using Poco::Net::NetException;
47
using Poco::Net::HTTPException;
49
using Poco::StreamCopier;
52
HTTPStreamFactoryTest::HTTPStreamFactoryTest(const std::string& name): CppUnit::TestCase(name)
57
HTTPStreamFactoryTest::~HTTPStreamFactoryTest()
62
void HTTPStreamFactoryTest::testNoRedirect()
64
HTTPTestServer server;
65
HTTPStreamFactory factory;
66
URI uri("http://localhost/large");
67
uri.setPort(server.port());
68
std::auto_ptr<std::istream> pStr(factory.open(uri));
69
std::ostringstream ostr;
70
StreamCopier::copyStream(*pStr.get(), ostr);
71
assert (ostr.str() == HTTPTestServer::LARGE_BODY);
75
void HTTPStreamFactoryTest::testEmptyPath()
77
HTTPTestServer server;
78
HTTPStreamFactory factory;
79
URI uri("http://localhost");
80
uri.setPort(server.port());
81
std::auto_ptr<std::istream> pStr(factory.open(uri));
82
std::ostringstream ostr;
83
StreamCopier::copyStream(*pStr.get(), ostr);
84
assert (ostr.str() == HTTPTestServer::SMALL_BODY);
88
void HTTPStreamFactoryTest::testRedirect()
90
HTTPTestServer server;
91
HTTPStreamFactory factory;
92
URI uri("http://localhost/redirect");
93
uri.setPort(server.port());
94
std::auto_ptr<std::istream> pStr(factory.open(uri));
95
std::ostringstream ostr;
96
StreamCopier::copyStream(*pStr.get(), ostr);
97
assert (ostr.str() == HTTPTestServer::LARGE_BODY);
101
void HTTPStreamFactoryTest::testProxy()
103
HTTPTestServer server;
104
HTTPStreamFactory factory("localhost", server.port());
105
URI uri("http://www.somehost.com/large");
106
std::auto_ptr<std::istream> pStr(factory.open(uri));
107
std::ostringstream ostr;
108
StreamCopier::copyStream(*pStr.get(), ostr);
109
assert (ostr.str() == HTTPTestServer::LARGE_BODY);
113
void HTTPStreamFactoryTest::testError()
115
HTTPTestServer server;
116
HTTPStreamFactory factory;
117
URI uri("http://localhost/notfound");
118
uri.setPort(server.port());
121
std::istream* pStr = factory.open(uri);
122
fail("not found - must throw");
124
catch (HTTPException& exc)
126
std::string m = exc.displayText();
131
void HTTPStreamFactoryTest::setUp()
136
void HTTPStreamFactoryTest::tearDown()
141
CppUnit::Test* HTTPStreamFactoryTest::suite()
143
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("HTTPStreamFactoryTest");
145
CppUnit_addTest(pSuite, HTTPStreamFactoryTest, testNoRedirect);
146
CppUnit_addTest(pSuite, HTTPStreamFactoryTest, testEmptyPath);
147
CppUnit_addTest(pSuite, HTTPStreamFactoryTest, testRedirect);
148
CppUnit_addTest(pSuite, HTTPStreamFactoryTest, testProxy);
149
CppUnit_addTest(pSuite, HTTPStreamFactoryTest, testError);