| 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 |
|
| 15 |
package docstore |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"errors" |
| 20 |
"net/url" |
| 21 |
"testing" |
| 22 |
|
| 23 |
"github.com/google/go-cmp/cmp" |
| 24 |
) |
| 25 |
|
| 26 |
func TestURLMux(t *testing.T) { |
| 27 |
ctx := context.Background() |
| 28 |
|
| 29 |
mux := new(URLMux) |
| 30 |
fake := &fakeOpener{} |
| 31 |
mux.RegisterCollection("foo", fake) |
| 32 |
mux.RegisterCollection("err", fake) |
| 33 |
|
| 34 |
if diff := cmp.Diff(mux.CollectionSchemes(), []string{"err", "foo"}); diff != "" { |
| 35 |
t.Errorf("Schemes: %s", diff) |
| 36 |
} |
| 37 |
if !mux.ValidCollectionScheme("foo") || !mux.ValidCollectionScheme("err") { |
| 38 |
t.Errorf("ValidCollectionScheme didn't return true for valid scheme") |
| 39 |
} |
| 40 |
if mux.ValidCollectionScheme("foo2") || mux.ValidCollectionScheme("http") { |
| 41 |
t.Errorf("ValidCollectionScheme didn't return false for invalid scheme") |
| 42 |
} |
| 43 |
|
| 44 |
for _, tc := range []struct { |
| 45 |
name string |
| 46 |
url string |
| 47 |
wantErr bool |
| 48 |
}{ |
| 49 |
{ |
| 50 |
name: "empty URL", |
| 51 |
wantErr: true, |
| 52 |
}, |
| 53 |
{ |
| 54 |
name: "invalid URL", |
| 55 |
url: ":foo", |
| 56 |
wantErr: true, |
| 57 |
}, |
| 58 |
{ |
| 59 |
name: "invalid URL no scheme", |
| 60 |
url: "foo", |
| 61 |
wantErr: true, |
| 62 |
}, |
| 63 |
{ |
| 64 |
name: "unregistered scheme", |
| 65 |
url: "bar://mycollection", |
| 66 |
wantErr: true, |
| 67 |
}, |
| 68 |
{ |
| 69 |
name: "func returns error", |
| 70 |
url: "err://mycollection", |
| 71 |
wantErr: true, |
| 72 |
}, |
| 73 |
{ |
| 74 |
name: "no query options", |
| 75 |
url: "foo://mycollection", |
| 76 |
}, |
| 77 |
{ |
| 78 |
name: "empty query options", |
| 79 |
url: "foo://mycollection?", |
| 80 |
}, |
| 81 |
{ |
| 82 |
name: "using api scheme prefix", |
| 83 |
url: "docstore+foo://bar", |
| 84 |
}, |
| 85 |
{ |
| 86 |
name: "using api+type scheme prefix", |
| 87 |
url: "docstore+collection+foo://bar", |
| 88 |
}, |
| 89 |
} { |
| 90 |
t.Run(tc.name, func(t *testing.T) { |
| 91 |
_, gotErr := mux.OpenCollection(ctx, tc.url) |
| 92 |
if (gotErr != nil) != tc.wantErr { |
| 93 |
t.Fatalf("got err %v, want error %v", gotErr, tc.wantErr) |
| 94 |
} |
| 95 |
if gotErr != nil { |
| 96 |
return |
| 97 |
} |
| 98 |
if got := fake.u.String(); got != tc.url { |
| 99 |
t.Errorf("got %q want %q", got, tc.url) |
| 100 |
} |
| 101 |
// Repeat with OpenCollectionURL. |
| 102 |
parsed, err := url.Parse(tc.url) |
| 103 |
if err != nil { |
| 104 |
t.Fatal(err) |
| 105 |
} |
| 106 |
_, gotErr = mux.OpenCollectionURL(ctx, parsed) |
| 107 |
if gotErr != nil { |
| 108 |
t.Fatalf("got err %v want nil", gotErr) |
| 109 |
} |
| 110 |
if got := fake.u.String(); got != tc.url { |
| 111 |
t.Errorf("got %q want %q", got, tc.url) |
| 112 |
} |
| 113 |
}) |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
type fakeOpener struct { |
| 118 |
u *url.URL // last url passed to OpenCollectionURL |
| 119 |
} |
| 120 |
|
| 121 |
func (o *fakeOpener) OpenCollectionURL(ctx context.Context, u *url.URL) (*Collection, error) { |
| 122 |
if u.Scheme == "err" { |
| 123 |
return nil, errors.New("fail") |
| 124 |
} |
| 125 |
o.u = u |
| 126 |
return nil, nil |
| 127 |
} |