Debian Codegraph @

internal/openurl/openurl_test.go

raw
1 // Copyright 2019 The Go Cloud Development Kit Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 package openurl_test
15
16 import (
17 "testing"
18
19 "github.com/google/go-cmp/cmp"
20 "gocloud.dev/internal/openurl"
21 )
22
23 func TestSchemeMap(t *testing.T) {
24 const foo, bar = "foo value", "bar value"
25
26 tests := []struct {
27 url string
28 wantErr bool
29 want any
30 }{
31 {"invalid url", true, nil},
32 {"foo://a/b/c", false, foo},
33 {"api+foo://a/b/c", false, foo},
34 {"api+type+foo://a/b/c", false, foo},
35 {"bar://a?p=v", false, bar},
36 {"api+bar://a", false, bar},
37 {"api+type+bar://a", false, bar},
38 {"typ+bar://a", true, nil},
39 {"api+typ+bar://a", true, nil},
40 }
41
42 var emptyM, m openurl.SchemeMap
43 m.Register("api", "Type", "foo", foo)
44 m.Register("api", "Type", "bar", bar)
45
46 if diff := cmp.Diff(m.Schemes(), []string{"bar", "foo"}); diff != "" {
47 t.Errorf("Schemes: %s", diff)
48 }
49 if !m.ValidScheme("foo") || !m.ValidScheme("bar") {
50 t.Errorf("ValidScheme didn't return true for valid scheme")
51 }
52 if m.ValidScheme("foo2") || m.ValidScheme("http") {
53 t.Errorf("ValidScheme didn't return false for invalid scheme")
54 }
55
56 for _, test := range tests {
57 // Empty SchemeMap should always return an error.
58 if _, _, err := emptyM.FromString("type", test.url); err == nil {
59 t.Errorf("%s: empty SchemeMap got nil error, wanted non-nil error", test.url)
60 }
61
62 got, gotURL, gotErr := m.FromString("type", test.url)
63 if (gotErr != nil) != test.wantErr {
64 t.Errorf("%s: got error %v, want error: %v", test.url, gotErr, test.wantErr)
65 }
66 if gotErr != nil {
67 continue
68 }
69 if got := gotURL.String(); got != test.url {
70 t.Errorf("%s: got URL %q want %v", test.url, got, test.url)
71 }
72 if got != test.want {
73 t.Errorf("%s: got %v want %v", test.url, got, test.want)
74 }
75 }
76 }