1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
/*
Copyright The ORAS Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package remote
import (
"net/url"
"reflect"
"testing"
"oras.land/oras-go/v2/registry"
)
func Test_buildReferrersURL(t *testing.T) {
ref := registry.Reference{
Registry: "localhost",
Repository: "hello-world",
Reference: "sha256:b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9",
}
params := []struct {
name string
plainHttp bool
artifactType string
want string
}{
{
name: "plain http, no filter",
plainHttp: true,
artifactType: "",
want: "http://localhost/v2/hello-world/referrers/sha256:b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9",
},
{
name: "https, no filter",
plainHttp: false,
artifactType: "",
want: "https://localhost/v2/hello-world/referrers/sha256:b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9",
},
{
name: "plain http, filter",
plainHttp: true,
artifactType: "signature/example",
want: "http://localhost/v2/hello-world/referrers/sha256:b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9?artifactType=signature%2Fexample",
},
{
name: "https, filter",
plainHttp: false,
artifactType: "signature/example",
want: "https://localhost/v2/hello-world/referrers/sha256:b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9?artifactType=signature%2Fexample",
},
}
for _, tt := range params {
t.Run(tt.name, func(t *testing.T) {
got := buildReferrersURL(tt.plainHttp, ref, tt.artifactType)
if !compareUrl(got, tt.want) {
t.Errorf("buildReferrersURL() = %s, want %s", got, tt.want)
}
})
}
}
// compareUrl compares two urls, regardless of query order and encoding
func compareUrl(s1, s2 string) bool {
u1, err := url.Parse(s1)
if err != nil {
return false
}
u2, err := url.Parse(s2)
if err != nil {
return false
}
q1, err := url.ParseQuery(u1.RawQuery)
if err != nil {
return false
}
q2, err := url.ParseQuery(u2.RawQuery)
if err != nil {
return false
}
return u1.Scheme == u2.Scheme &&
reflect.DeepEqual(u1.User, u1.User) &&
u1.Host == u2.Host &&
u1.Path == u2.Path &&
reflect.DeepEqual(q1, q2)
}
|