2017-10-18 00:25:16 +02:00
|
|
|
package disco
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
2018-12-10 11:06:05 +01:00
|
|
|
"strings"
|
2017-10-18 00:25:16 +02:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestHostServiceURL(t *testing.T) {
|
|
|
|
baseURL, _ := url.Parse("https://example.com/disco/foo.json")
|
|
|
|
host := Host{
|
|
|
|
discoURL: baseURL,
|
2018-12-10 11:06:05 +01:00
|
|
|
hostname: "test-server",
|
2017-10-18 00:25:16 +02:00
|
|
|
services: map[string]interface{}{
|
|
|
|
"absolute.v1": "http://example.net/foo/bar",
|
|
|
|
"absolutewithport.v1": "http://example.net:8080/foo/bar",
|
|
|
|
"relative.v1": "./stu/",
|
|
|
|
"rootrelative.v1": "/baz",
|
|
|
|
"protorelative.v1": "//example.net/",
|
|
|
|
"withfragment.v1": "http://example.org/#foo",
|
|
|
|
"querystring.v1": "https://example.net/baz?foo=bar",
|
|
|
|
"nothttp.v1": "ftp://127.0.0.1/pub/",
|
|
|
|
"invalid.v1": "***not A URL at all!:/<@@@@>***",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
ID string
|
2018-12-10 11:06:05 +01:00
|
|
|
want string
|
|
|
|
err string
|
2017-10-18 00:25:16 +02:00
|
|
|
}{
|
2018-12-10 11:06:05 +01:00
|
|
|
{"absolute.v1", "http://example.net/foo/bar", ""},
|
|
|
|
{"absolutewithport.v1", "http://example.net:8080/foo/bar", ""},
|
|
|
|
{"relative.v1", "https://example.com/disco/stu/", ""},
|
|
|
|
{"rootrelative.v1", "https://example.com/baz", ""},
|
|
|
|
{"protorelative.v1", "https://example.net/", ""},
|
|
|
|
{"withfragment.v1", "http://example.org/", ""},
|
|
|
|
{"querystring.v1", "https://example.net/baz?foo=bar", ""},
|
|
|
|
{"nothttp.v1", "<nil>", "unsupported scheme"},
|
|
|
|
{"invalid.v1", "<nil>", "Failed to parse service URL"},
|
2017-10-18 00:25:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.ID, func(t *testing.T) {
|
2018-12-10 11:06:05 +01:00
|
|
|
url, err := host.ServiceURL(test.ID)
|
|
|
|
if (err != nil || test.err != "") &&
|
|
|
|
(err == nil || !strings.Contains(err.Error(), test.err)) {
|
|
|
|
t.Fatalf("unexpected service URL error: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-10-18 00:25:16 +02:00
|
|
|
var got string
|
|
|
|
if url != nil {
|
|
|
|
got = url.String()
|
|
|
|
} else {
|
|
|
|
got = "<nil>"
|
|
|
|
}
|
|
|
|
|
2018-12-10 11:06:05 +01:00
|
|
|
if got != test.want {
|
|
|
|
t.Errorf("wrong result\ngot: %s\nwant: %s", got, test.want)
|
2017-10-18 00:25:16 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|