1//
2// HTTPStreamFactoryTest.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 "HTTPStreamFactoryTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/Net/HTTPStreamFactory.h"
15#include "Poco/Net/NetException.h"
16#include "Poco/URI.h"
17#include "Poco/URIStreamOpener.h"
18#include "Poco/StreamCopier.h"
19#include "HTTPTestServer.h"
20#include <sstream>
21#include <memory>
22
23
24using Poco::Net::HTTPStreamFactory;
25using Poco::Net::NetException;
26using Poco::Net::HTTPException;
27using Poco::URI;
28using Poco::StreamCopier;
29
30
31HTTPStreamFactoryTest::HTTPStreamFactoryTest(const std::string& name): CppUnit::TestCase(name)
32{
33}
34
35
36HTTPStreamFactoryTest::~HTTPStreamFactoryTest()
37{
38}
39
40
41void HTTPStreamFactoryTest::testNoRedirect()
42{
43 HTTPTestServer server;
44 HTTPStreamFactory factory;
45 URI uri("http://127.0.0.1/large");
46 uri.setPort(server.port());
47 std::unique_ptr<std::istream> pStr(factory.open(uri));
48 std::ostringstream ostr;
49 StreamCopier::copyStream(*pStr.get(), ostr);
50 assertTrue (ostr.str() == HTTPTestServer::LARGE_BODY);
51}
52
53
54void HTTPStreamFactoryTest::testEmptyPath()
55{
56 HTTPTestServer server;
57 HTTPStreamFactory factory;
58 URI uri("http://127.0.0.1");
59 uri.setPort(server.port());
60 std::unique_ptr<std::istream> pStr(factory.open(uri));
61 std::ostringstream ostr;
62 StreamCopier::copyStream(*pStr.get(), ostr);
63 assertTrue (ostr.str() == HTTPTestServer::SMALL_BODY);
64}
65
66
67void HTTPStreamFactoryTest::testRedirect()
68{
69 HTTPTestServer server;
70 Poco::URIStreamOpener opener;
71 opener.registerStreamFactory("http", new HTTPStreamFactory);
72 URI uri("http://127.0.0.1/redirect");
73 uri.setPort(server.port());
74 std::unique_ptr<std::istream> pStr(opener.open(uri));
75 std::ostringstream ostr;
76 StreamCopier::copyStream(*pStr.get(), ostr);
77 assertTrue (ostr.str() == HTTPTestServer::LARGE_BODY);
78}
79
80
81void HTTPStreamFactoryTest::testProxy()
82{
83 HTTPTestServer server;
84 HTTPStreamFactory factory("127.0.0.1", server.port());
85 URI uri("http://www.somehost.com/large");
86 std::unique_ptr<std::istream> pStr(factory.open(uri));
87 std::ostringstream ostr;
88 StreamCopier::copyStream(*pStr.get(), ostr);
89 assertTrue (ostr.str() == HTTPTestServer::LARGE_BODY);
90}
91
92
93void HTTPStreamFactoryTest::testError()
94{
95 HTTPTestServer server;
96 HTTPStreamFactory factory;
97 URI uri("http://127.0.0.1/notfound");
98 uri.setPort(server.port());
99 try
100 {
101 std::istream* pStr = factory.open(uri);
102 fail("not found - must throw");
103 pStr = pStr + 0; // to silence gcc
104 }
105 catch (HTTPException& exc)
106 {
107 std::string m = exc.displayText();
108 }
109}
110
111
112void HTTPStreamFactoryTest::setUp()
113{
114}
115
116
117void HTTPStreamFactoryTest::tearDown()
118{
119}
120
121
122CppUnit::Test* HTTPStreamFactoryTest::suite()
123{
124 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("HTTPStreamFactoryTest");
125
126 CppUnit_addTest(pSuite, HTTPStreamFactoryTest, testNoRedirect);
127 CppUnit_addTest(pSuite, HTTPStreamFactoryTest, testEmptyPath);
128 CppUnit_addTest(pSuite, HTTPStreamFactoryTest, testRedirect);
129 CppUnit_addTest(pSuite, HTTPStreamFactoryTest, testProxy);
130 CppUnit_addTest(pSuite, HTTPStreamFactoryTest, testError);
131
132 return pSuite;
133}
134