Created
December 6, 2019 00:27
-
-
Save vinniefalco/95f24a2d13951d3ab7ec35f6746c81b4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com) | |
// | |
// 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) | |
// | |
// Official repository: https://github.com/vinniefalco/url | |
// | |
// Test that header file is self-contained. | |
#include <boost/url/value.hpp> | |
#include <boost/url/static_pool.hpp> | |
#include <boost/beast/_experimental/unit_test/suite.hpp> | |
#include <iostream> | |
namespace boost { | |
namespace url { | |
class value_test : public beast::unit_test::suite | |
{ | |
public: | |
void | |
testScheme() | |
{ | |
value u; | |
BEAST_EXPECT(u.scheme() == ""); | |
BEAST_EXPECT(u.set_scheme("ftp").scheme() == "ftp"); | |
BEAST_EXPECT(u.set_scheme("ws").scheme() == "ws"); | |
BEAST_EXPECT(u.set_scheme("http").scheme() == "http"); | |
BEAST_EXPECT(u.set_scheme("").scheme() == ""); | |
BEAST_THROWS(u.set_scheme("c@t"), system_error); | |
BEAST_THROWS(u.set_scheme("1cat"), system_error); | |
} | |
void | |
testUsername() | |
{ | |
value u; | |
BEAST_EXPECT(u.username() == ""); | |
BEAST_EXPECT(u.set_username("john").username() == "john"); | |
BEAST_EXPECT(u.set_username("john doe").username() == "john doe"); | |
BEAST_EXPECT(u.encoded_username() == "john%20doe"); | |
BEAST_EXPECT(u.set_encoded_username("john%20doe").username() == "john doe"); | |
BEAST_THROWS(u.set_encoded_username("john doe"), system_error); | |
} | |
void | |
testPassword() | |
{ | |
value u; | |
BEAST_EXPECT(u.password() == ""); | |
BEAST_EXPECT(u.set_password("password").password() == "password"); | |
BEAST_EXPECT(u.set_password("pass word").password() == "pass word"); | |
BEAST_EXPECT(u.encoded_password() == "pass%20word"); | |
BEAST_EXPECT(u.set_encoded_password("pass%20word").password() == "pass word"); | |
BEAST_THROWS(u.set_encoded_password("pass word"), system_error); | |
} | |
void | |
testHostname() | |
{ | |
value u; | |
BEAST_EXPECT(u.hostname() == ""); | |
BEAST_EXPECT(u.set_hostname("localhost").hostname() == "localhost"); | |
BEAST_EXPECT(u.set_hostname("local host").hostname() == "local host"); | |
BEAST_EXPECT(u.encoded_hostname() == "local%20host"); | |
BEAST_EXPECT(u.set_encoded_hostname("local%20host").hostname() == "local host"); | |
BEAST_THROWS(u.set_encoded_hostname("local host"), system_error); | |
} | |
void | |
testPort() | |
{ | |
value u; | |
BEAST_EXPECT(! u.port()); | |
BEAST_EXPECT(*u.set_port(80).port() == 80); | |
BEAST_EXPECT(! u.set_port({}).port().has_value()); | |
} | |
//------------------------------------------------------ | |
void | |
testHost() | |
{ | |
value u; | |
BEAST_EXPECT(u.encoded_host() == ""); | |
u.set_hostname("example.com"); | |
BEAST_EXPECT(u.encoded_host() == "example.com"); | |
u.set_port(443); | |
BEAST_EXPECT(u.encoded_host() == "example.com:443"); | |
u.set_hostname(""); | |
BEAST_EXPECT(u.encoded_host() == ":443"); | |
} | |
void | |
testUserinfo() | |
{ | |
value u; | |
BEAST_EXPECT(u.encoded_userinfo() == ""); | |
u.set_username("joe"); | |
BEAST_EXPECT(u.encoded_userinfo() == "joe"); | |
u.set_password("password"); | |
BEAST_EXPECT(u.encoded_userinfo() == "joe:password"); | |
u.set_username(""); | |
BEAST_EXPECT(u.encoded_userinfo() == ":password"); | |
} | |
void | |
testAuthority() | |
{ | |
value u; | |
BEAST_EXPECT(u.encoded_authority() == ""); | |
u.set_username("joe"); | |
BEAST_EXPECT(u.encoded_authority() == "joe@"); | |
u.set_password("password"); | |
BEAST_EXPECT(u.encoded_authority() == "joe:password@"); | |
u.set_hostname("example.com"); | |
BEAST_EXPECT(u.encoded_authority() == "joe:password@example.com"); | |
u.set_port(443); | |
BEAST_EXPECT(u.encoded_authority() == "joe:password@example.com:443"); | |
u.set_password(""); | |
BEAST_EXPECT(u.encoded_authority() == "joe@example.com:443"); | |
u.set_username(""); | |
BEAST_EXPECT(u.encoded_authority() == "example.com:443"); | |
BEAST_EXPECT(u.encoded_authority() == u.encoded_host()); | |
u.set_port({}); | |
BEAST_EXPECT(u.encoded_authority() == "example.com"); | |
BEAST_EXPECT(u.encoded_authority() == u.encoded_host()); | |
} | |
void | |
testOrigin() | |
{ | |
value u; | |
u.set_scheme("http"); | |
u.set_encoded_userinfo("joe:password"); | |
u.set_encoded_host("example.com:443"); | |
BEAST_EXPECT(u.encoded_origin() == "http://example.com:443"); | |
} | |
//------------------------------------------------------ | |
void | |
run() override | |
{ | |
testScheme(); | |
testUsername(); | |
testPassword(); | |
testHostname(); | |
testPort(); | |
testHost(); | |
testUserinfo(); | |
testAuthority(); | |
testOrigin(); | |
} | |
}; | |
BEAST_DEFINE_TESTSUITE(boost,url,value); | |
} // url | |
} // boost |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment