Menu

[r103]: / branches / uri / libs / network / test / uri_test.cpp  Maximize  Restore  History

Download this file

159 lines (103 with data), 3.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright Kim Grasman 2007.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#define BOOST_TEST_MODULE uri test
#include <boost/test/unit_test.hpp>
#include <boost/network/uri.hpp> // todo: this should be available from network.hpp
namespace std {
// Implement ostream::operator<< for uri class, to allow seralization to string
ostream& operator<<(ostream& stream, const boost::network::uri& u) {
stream << u.scheme() << "://";
stream << u.host();
return stream;
}
}
BOOST_AUTO_TEST_CASE(default_constructed_is_invalid) {
using namespace boost::network;
uri u;
BOOST_REQUIRE(!u.valid());
}
BOOST_AUTO_TEST_CASE(parse_valid_uri) {
using namespace boost::network;
uri u("http://www.boost.org:8080/path#fragment");
BOOST_REQUIRE(u.valid());
}
BOOST_AUTO_TEST_CASE(parse_invalid_uri) {
using namespace boost::network;
using namespace std;
uri u("abrakadabra");
BOOST_REQUIRE(!u.valid());
}
BOOST_AUTO_TEST_CASE(parse_partial_uri) {
using namespace boost::network;
// scheme + host should constitute enough to be valid
uri u("http://www.boost.org");
BOOST_REQUIRE(u.valid());
}
BOOST_AUTO_TEST_CASE(read_attributes) {
using namespace boost::network;
uri u("http://www.boost.org:8080/path?key=value#fragment");
BOOST_CHECK(u.valid());
BOOST_CHECK_EQUAL("http", u.scheme());
BOOST_CHECK_EQUAL("www.boost.org", u.host());
BOOST_CHECK_EQUAL(8080, u.port());
BOOST_CHECK_EQUAL("/path", u.path());
BOOST_CHECK_EQUAL("key=value", u.query());
BOOST_CHECK_EQUAL("fragment", u.fragment());
}
BOOST_AUTO_TEST_CASE(write_attributes) {
using namespace boost::network;
uri u("http://www.boost.org");
u.scheme() = "ftp";
u.host() = "ftp.boost.org";
u.port() = 2525;
u.path() = "/temp";
u.query() = "k=v";
u.fragment() = "anchor";
BOOST_CHECK_EQUAL("ftp://ftp.boost.org:2525/temp?k=v#anchor", u.str());
}
BOOST_AUTO_TEST_CASE(multiple_querystring_args) {
using namespace boost::network;
uri u("http://www.boost.org/path?a=b&c=d&e=f");
BOOST_CHECK(u.valid());
BOOST_CHECK_EQUAL("a=b&c=d&e=f", u.query());
}
BOOST_AUTO_TEST_CASE(custom_scheme) {
using namespace boost::network;
uri u("custom://www.boost.org");
BOOST_CHECK(u.valid());
BOOST_CHECK_EQUAL("custom", u.scheme());
}
BOOST_AUTO_TEST_CASE(path_defaults_to_slash) {
using namespace boost::network;
uri u("http://www.boost.org");
BOOST_CHECK_EQUAL("/", u.path());
}
BOOST_AUTO_TEST_CASE(invalid_converts_to_empty_string) {
using namespace boost::network;
uri u;
BOOST_CHECK_EQUAL(std::string(), u.str());
}
BOOST_AUTO_TEST_CASE(to_string_conversion) {
using namespace boost::network;
const std::string url = "http://www.boost.org:8080/temp?k=v#anchor";
uri u(url);
BOOST_CHECK_EQUAL(url, u.str());
}
BOOST_AUTO_TEST_CASE(empty_path_str) {
using namespace boost::network;
uri u("http://www.boost.org");
BOOST_CHECK_EQUAL("http://www.boost.org/", u.str());
}
BOOST_AUTO_TEST_CASE(port_default) {
using namespace boost::network;
uri u("http://www.boost.org");
BOOST_CHECK_EQUAL(0u, u.port());
}
BOOST_AUTO_TEST_CASE(url_builder) {
using namespace boost::network;
uri u, boost_org("http://www.boost.org");
// u << boost_org << path("path") << query_param("a", "b");
// BOOST_CHECK_EQUAL("http://www.boost.org/path?a=b", u.str());
}