| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <catch.hpp> |
| 21 |
#include <proton/url.hpp> |
| 22 |
#include <sstream> |
| 23 |
|
| 24 |
namespace { |
| 25 |
using proton::url; |
| 26 |
using proton::url_error; |
| 27 |
|
| 28 |
#define CHECK_URL(U, SCHEME, USER, PWD, HOST, PORT, PATH, S) do { \ |
| 29 |
url u(U); \ |
| 30 |
CHECK((SCHEME) == u.scheme()); \ |
| 31 |
CHECK((USER) == u.user()); \ |
| 32 |
CHECK((PWD) == u.password()); \ |
| 33 |
CHECK((HOST) == u.host()); \ |
| 34 |
CHECK((PORT) == u.port()); \ |
| 35 |
CHECK((PATH) == u.path()); \ |
| 36 |
CHECK(std::string(S) == std::string(u)); \ |
| 37 |
} while(0) |
| 38 |
|
| 39 |
TEST_CASE("parse URL","[url]") { |
| 40 |
SECTION("full and defaulted") { |
| 41 |
CHECK_URL(url("amqp://foo:xyz/path"), |
| 42 |
"amqp", "", "", "foo", "xyz", "path", |
| 43 |
"amqp://foo:xyz/path"); |
| 44 |
CHECK_URL(url("amqp://username:password@host:1234/path"), |
| 45 |
"amqp", "username", "password", "host", "1234", "path", |
| 46 |
"amqp://username:password@host:1234/path"); |
| 47 |
CHECK_URL(url("host:1234"), |
| 48 |
"amqp", "", "", "host", "1234", "", |
| 49 |
"amqp://host:1234"); |
| 50 |
CHECK_URL(url("host"), |
| 51 |
"amqp", "", "", "host", "amqp", "", |
| 52 |
"amqp://host:amqp"); |
| 53 |
CHECK_URL(url("host/path"), |
| 54 |
"amqp", "", "", "host", "amqp", "path", |
| 55 |
"amqp://host:amqp/path"); |
| 56 |
CHECK_URL(url("amqps://host"), |
| 57 |
"amqps", "", "", "host", "amqps", "", |
| 58 |
"amqps://host:amqps"); |
| 59 |
CHECK_URL(url("/path"), |
| 60 |
"amqp", "", "", "localhost", "amqp", "path", |
| 61 |
"amqp://localhost:amqp/path"); |
| 62 |
CHECK_URL(url(""), |
| 63 |
"amqp", "", "", "localhost", "amqp", "", |
| 64 |
"amqp://localhost:amqp"); |
| 65 |
CHECK_URL(url(":1234"), |
| 66 |
"amqp", "", "", "localhost", "1234", "", |
| 67 |
"amqp://localhost:1234"); |
| 68 |
} |
| 69 |
SECTION("starting with //") { |
| 70 |
CHECK_URL(url("//username:password@host:1234/path"), |
| 71 |
"amqp", "username", "password", "host", "1234", "path", |
| 72 |
"amqp://username:password@host:1234/path"); |
| 73 |
CHECK_URL(url("//host:port/path"), |
| 74 |
"amqp", "", "", "host", "port", "path", |
| 75 |
"amqp://host:port/path"); |
| 76 |
CHECK_URL(url("//host"), |
| 77 |
"amqp", "", "", "host", "amqp", "", |
| 78 |
"amqp://host:amqp"); |
| 79 |
CHECK_URL(url("//:port"), |
| 80 |
"amqp", "", "", "localhost", "port", "", |
| 81 |
"amqp://localhost:port"); |
| 82 |
CHECK_URL(url("//:0"), |
| 83 |
"amqp", "", "", "localhost", "0", "", |
| 84 |
"amqp://localhost:0"); |
| 85 |
} |
| 86 |
SECTION("no defaults") { |
| 87 |
CHECK_URL(url("", false), |
| 88 |
"", "", "", "", "", "", |
| 89 |
""); |
| 90 |
CHECK_URL(url("//:", false), |
| 91 |
"", "", "", "", "", "", |
| 92 |
":"); |
| 93 |
CHECK_URL(url("//:0", false), |
| 94 |
"", "", "", "", "0", "", |
| 95 |
":0"); |
| 96 |
CHECK_URL(url("//h:", false), |
| 97 |
"", "", "", "h", "", "", |
| 98 |
"h:"); |
| 99 |
} |
| 100 |
SECTION("urlencoding") { |
| 101 |
CHECK_URL(url("amqps://%40user%2F%3A:%40pass%2F%[email protected]/some_topic"), |
| 102 |
"amqps", "@user/:", "@pass/:", "example.net", "amqps", "some_topic", |
| 103 |
"amqps://%40user%2F%3A:%40pass%2F%[email protected]:amqps/some_topic"); |
| 104 |
CHECK_URL(url("amqps://user%2F%3A=:pass%2F%[email protected]/some_topic"), |
| 105 |
"amqps", "user/:=", "pass/:=", "example.net", "amqps", "some_topic", |
| 106 |
"amqps://user%2F%3A=:pass%2F%[email protected]:amqps/some_topic"); |
| 107 |
// Unquoted % at the end of a percent encoded string |
| 108 |
CHECK_URL(url("amqp://username%:password@host:1234/path"), |
| 109 |
"amqp", "username%", "password", "host", "1234", "path", |
| 110 |
"amqp://username%25:password@host:1234/path"); |
| 111 |
} |
| 112 |
SECTION("ipv6") { |
| 113 |
CHECK_URL(url("[fe80::c662:ab36:1ef1:1596]:5672/path"), |
| 114 |
"amqp", "", "", "fe80::c662:ab36:1ef1:1596", "5672", "path", |
| 115 |
"amqp://[fe80::c662:ab36:1ef1:1596]:5672/path"); |
| 116 |
CHECK_URL(url("amqp://user:pass@[::1]:1234/path"), |
| 117 |
"amqp", "user", "pass", "::1", "1234", "path", |
| 118 |
"amqp://user:pass@[::1]:1234/path"); |
| 119 |
} |
| 120 |
SECTION("port") { |
| 121 |
CHECK("host:1234" == url("amqp://host:1234/path").host_port()); |
| 122 |
CHECK(5672 == url("amqp://foo/path").port_int()); |
| 123 |
CHECK(5671 == url("amqps://foo/path").port_int()); |
| 124 |
CHECK(1234 == url("amqps://foo:1234/path").port_int()); |
| 125 |
|
| 126 |
url u("amqps://foo:xyz/path"); |
| 127 |
CHECK_THROWS_AS(u.port_int(), url_error); |
| 128 |
CHECK_THROWS_WITH(u.port_int(), "invalid port 'xyz'"); |
| 129 |
} |
| 130 |
SECTION("constructors") { |
| 131 |
url u1("amqp://foo:xyz/path"); |
| 132 |
url u2 = u1; |
| 133 |
CHECK("amqp://foo:xyz/path" == std::string(u2)); |
| 134 |
} |
| 135 |
SECTION("methods") { |
| 136 |
SECTION("check empty") { |
| 137 |
url u("amqp://foo:1234/path"); |
| 138 |
CHECK(u.empty()); |
| 139 |
CHECK("amqp://foo:1234/path" == std::string(u)); |
| 140 |
CHECK(!u.empty()); |
| 141 |
} |
| 142 |
} |
| 143 |
SECTION("operators") { |
| 144 |
SECTION("assignment") { |
| 145 |
url u1("amqp://foo:xyz/path"); |
| 146 |
url u3("amqp://foo:xyz/pathdontcare"); |
| 147 |
u3 = u1; |
| 148 |
CHECK("amqp://foo:xyz/path" == std::string(u3)); |
| 149 |
} |
| 150 |
SECTION("output stream") { |
| 151 |
url u("amqp://foo:xyz/path"); |
| 152 |
std::ostringstream o; |
| 153 |
o << u; |
| 154 |
CHECK("amqp://foo:xyz/path" == std::string(o.str())); |
| 155 |
CHECK("amqp://foo:xyz/path" == to_string(u)); |
| 156 |
} |
| 157 |
SECTION("input stream") { |
| 158 |
url u("amqp://foo:xyz/pathdontcare"); |
| 159 |
std::istringstream i("amqp://foo:xyz/path"); |
| 160 |
i >> u; |
| 161 |
CHECK("amqp://foo:xyz/path" == std::string(u)); |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
} // namespace |