| 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 |
package amqp |
| 21 |
|
| 22 |
import ( |
| 23 |
"fmt" |
| 24 |
"net/url" |
| 25 |
"strings" |
| 26 |
"testing" |
| 27 |
) |
| 28 |
|
| 29 |
func ExampleParseURL() { |
| 30 |
for _, s := range []string{ |
| 31 |
"amqp://username:password@host:1234/path", |
| 32 |
"host:1234", |
| 33 |
"host", |
| 34 |
"host/path", |
| 35 |
"amqps://host", |
| 36 |
"/path", |
| 37 |
"", |
| 38 |
} { |
| 39 |
u, err := ParseURL(s) |
| 40 |
if err != nil { |
| 41 |
fmt.Println(err) |
| 42 |
} else { |
| 43 |
fmt.Println(u) |
| 44 |
} |
| 45 |
} |
| 46 |
// Output: |
| 47 |
// amqp://username:password@host:1234/path |
| 48 |
// amqp://host:1234 |
| 49 |
// amqp://host:amqp |
| 50 |
// amqp://host:amqp/path |
| 51 |
// amqps://host:amqps |
| 52 |
// amqp://localhost:amqp/path |
| 53 |
// amqp://localhost:amqp |
| 54 |
} |
| 55 |
|
| 56 |
func TestParseValidURL(t *testing.T) { |
| 57 |
for _, s := range []struct { |
| 58 |
// input |
| 59 |
url string |
| 60 |
// expected |
| 61 |
scheme string |
| 62 |
user *url.Userinfo |
| 63 |
host string |
| 64 |
port string |
| 65 |
path string |
| 66 |
}{ |
| 67 |
{"amqp://username:password@host:1234/path", |
| 68 |
"amqp", url.UserPassword("username", "password"), "host:1234", "1234", "/path"}, |
| 69 |
//{"[::1]", |
| 70 |
// "amqp", &url2.Userinfo{}, "[::]:amqp", "amqp", ""}, // this used to be valid example; does not parse in go 1.13 |
| 71 |
// // parse [::1]: first path segment in URL cannot contain colon |
| 72 |
} { |
| 73 |
u, err := ParseURL(s.url) |
| 74 |
if err != nil { |
| 75 |
t.Errorf("Unexpected parsing error on '%s' got error '%s'", s.url, err) |
| 76 |
continue |
| 77 |
} |
| 78 |
if u.Scheme != s.scheme { |
| 79 |
t.Errorf("Unexpected scheme parsing '%s', expected '%s', got '%s'", s.url, s.scheme, u.Scheme) |
| 80 |
} |
| 81 |
if *u.User != *s.user { |
| 82 |
t.Errorf("Unexpected user parsing '%s', expected '%s', got '%s'", s.url, s.user, u.User) |
| 83 |
} |
| 84 |
if u.Host != s.host { |
| 85 |
t.Errorf("Unexpected host parsing '%s', expected '%s', got '%s'", s.url, s.host, u.Host) |
| 86 |
} |
| 87 |
if u.Port() != s.port { |
| 88 |
t.Errorf("Unexpected port parsing '%s', expected '%s', got '%s'", s.url, s.port, u.Port()) |
| 89 |
} |
| 90 |
if u.Path != s.path { |
| 91 |
t.Errorf("Unexpected path parsing '%s', expected '%s', got '%s'", s.url, s.path, u.Path) |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
func TestParseInvalidURL(t *testing.T) { |
| 97 |
for _, s := range []struct { |
| 98 |
// input |
| 99 |
url string |
| 100 |
// expected |
| 101 |
inError string |
| 102 |
}{ |
| 103 |
// proton errors |
| 104 |
{"http://seznam.cz", "invalid amqp scheme"}, |
| 105 |
// go errors |
| 106 |
{":1234", "missing protocol scheme"}, |
| 107 |
{"[::1", ""}, // error message is completely different depending on Go toolchain version |
| 108 |
} { |
| 109 |
u, err := ParseURL(s.url) |
| 110 |
if err == nil { |
| 111 |
t.Errorf("Expected error on '%s', parsed out '%s'", s.url, u) |
| 112 |
continue |
| 113 |
} |
| 114 |
if !strings.Contains(err.Error(), s.inError) { |
| 115 |
t.Errorf("Expected error '%s' to contain '%s'", err, s.inError) |
| 116 |
} |
| 117 |
} |
| 118 |
} |