| 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_testscip-go gomod gocloud.dev . `gocloud.dev/internal/openurl_test`/ |
| 15 | |
| 16 | import ( |
| 17 | "testingscip-go gomod github.com/golang/go/src go1.24.0 testing/" |
| 18 | |
| 19 | "github.com/google/go-cmp/cmpscip-go gomod github.com/google/go-cmp v0.7.0 `github.com/google/go-cmp/cmp`/" |
| 20 | "gocloud.dev/internal/openurlscip-go gomod gocloud.dev . `gocloud.dev/internal/openurl`/" |
| 21 | ) |
| 22 | |
| 23 | func TestSchemeMapscip-go gomod gocloud.dev . `gocloud.dev/internal/openurl_test`/TestSchemeMap().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 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 openurlscip-go gomod gocloud.dev . `gocloud.dev/internal/openurl`/.SchemeMapscip-go gomod gocloud.dev . `gocloud.dev/internal/openurl`/SchemeMap# |
| 43 | m.Registerscip-go gomod gocloud.dev . `gocloud.dev/internal/openurl`/SchemeMap#Register().("api", "Type", "foo", foo) |
| 44 | m.Registerscip-go gomod gocloud.dev . `gocloud.dev/internal/openurl`/SchemeMap#Register().("api", "Type", "bar", bar) |
| 45 | |
| 46 | if diff := cmpscip-go gomod github.com/google/go-cmp v0.7.0 `github.com/google/go-cmp/cmp`/.Diffscip-go gomod github.com/google/go-cmp v0.7.0 `github.com/google/go-cmp/cmp`/Diff().(m.Schemesscip-go gomod gocloud.dev . `gocloud.dev/internal/openurl`/SchemeMap#Schemes().(), []string{"bar", "foo"}); diff != "" { |
| 47 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("Schemes: %s", diff) |
| 48 | } |
| 49 | if !m.ValidSchemescip-go gomod gocloud.dev . `gocloud.dev/internal/openurl`/SchemeMap#ValidScheme().("foo") || !m.ValidSchemescip-go gomod gocloud.dev . `gocloud.dev/internal/openurl`/SchemeMap#ValidScheme().("bar") { |
| 50 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("ValidScheme didn't return true for valid scheme") |
| 51 | } |
| 52 | if m.ValidSchemescip-go gomod gocloud.dev . `gocloud.dev/internal/openurl`/SchemeMap#ValidScheme().("foo2") || m.ValidSchemescip-go gomod gocloud.dev . `gocloud.dev/internal/openurl`/SchemeMap#ValidScheme().("http") { |
| 53 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#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.FromStringscip-go gomod gocloud.dev . `gocloud.dev/internal/openurl`/SchemeMap#FromString().("type", test.url); err == nil { |
| 59 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("%s: empty SchemeMap got nil error, wanted non-nil error", test.url) |
| 60 | } |
| 61 | |
| 62 | got, gotURL, gotErr := m.FromStringscip-go gomod gocloud.dev . `gocloud.dev/internal/openurl`/SchemeMap#FromString().("type", test.url) |
| 63 | if (gotErr != nil) != test.wantErr { |
| 64 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#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.Stringscip-go gomod github.com/golang/go/src go1.24.0 `net/url`/URL#String().(); got != test.url { |
| 70 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("%s: got URL %q want %v", test.url, got, test.url) |
| 71 | } |
| 72 | if got != test.want { |
| 73 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("%s: got %v want %v", test.url, got, test.want) |
| 74 | } |
| 75 | } |
| 76 | } |