~ubuntu-branches/ubuntu/gutsy/poco/gutsy

« back to all changes in this revision

Viewing changes to Net/testsuite/src/HTTPStreamFactoryTest.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2007-04-27 18:33:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070427183348-xgnpct0qd6a2ip34
Tags: upstream-1.2.9
ImportĀ upstreamĀ versionĀ 1.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// HTTPStreamFactoryTest.cpp
 
3
//
 
4
// $Id: //poco/1.2/Net/testsuite/src/HTTPStreamFactoryTest.cpp#1 $
 
5
//
 
6
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
 
7
// and Contributors.
 
8
//
 
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:
 
15
// 
 
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.
 
22
// 
 
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.
 
30
//
 
31
 
 
32
 
 
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"
 
38
#include "Poco/URI.h"
 
39
#include "Poco/StreamCopier.h"
 
40
#include "HTTPTestServer.h"
 
41
#include <sstream>
 
42
#include <memory>
 
43
 
 
44
 
 
45
using Poco::Net::HTTPStreamFactory;
 
46
using Poco::Net::NetException;
 
47
using Poco::Net::HTTPException;
 
48
using Poco::URI;
 
49
using Poco::StreamCopier;
 
50
 
 
51
 
 
52
HTTPStreamFactoryTest::HTTPStreamFactoryTest(const std::string& name): CppUnit::TestCase(name)
 
53
{
 
54
}
 
55
 
 
56
 
 
57
HTTPStreamFactoryTest::~HTTPStreamFactoryTest()
 
58
{
 
59
}
 
60
 
 
61
 
 
62
void HTTPStreamFactoryTest::testNoRedirect()
 
63
{
 
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);
 
72
}
 
73
 
 
74
 
 
75
void HTTPStreamFactoryTest::testEmptyPath()
 
76
{
 
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);
 
85
}
 
86
 
 
87
 
 
88
void HTTPStreamFactoryTest::testRedirect()
 
89
{
 
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);
 
98
}
 
99
 
 
100
 
 
101
void HTTPStreamFactoryTest::testProxy()
 
102
{
 
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);
 
110
}
 
111
 
 
112
 
 
113
void HTTPStreamFactoryTest::testError()
 
114
{
 
115
        HTTPTestServer server;
 
116
        HTTPStreamFactory factory;
 
117
        URI uri("http://localhost/notfound");
 
118
        uri.setPort(server.port());
 
119
        try
 
120
        {
 
121
                std::istream* pStr = factory.open(uri);
 
122
                fail("not found - must throw");
 
123
        }
 
124
        catch (HTTPException& exc)
 
125
        {
 
126
                std::string m = exc.displayText();
 
127
        }
 
128
}
 
129
 
 
130
 
 
131
void HTTPStreamFactoryTest::setUp()
 
132
{
 
133
}
 
134
 
 
135
 
 
136
void HTTPStreamFactoryTest::tearDown()
 
137
{
 
138
}
 
139
 
 
140
 
 
141
CppUnit::Test* HTTPStreamFactoryTest::suite()
 
142
{
 
143
        CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("HTTPStreamFactoryTest");
 
144
 
 
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);
 
150
 
 
151
        return pSuite;
 
152
}