github.com/opcr-io/oras-go/v2@v2.0.0-20231122155130-eb4260d8a0ae/registry/remote/url_test.go (about)

     1  /*
     2  Copyright The ORAS Authors.
     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  http://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  
    16  package remote
    17  
    18  import (
    19  	"net/url"
    20  	"reflect"
    21  	"testing"
    22  
    23  	"github.com/opcr-io/oras-go/v2/registry"
    24  )
    25  
    26  func Test_buildReferrersURL(t *testing.T) {
    27  	ref := registry.Reference{
    28  		Registry:   "localhost",
    29  		Repository: "hello-world",
    30  		Reference:  "sha256:b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9",
    31  	}
    32  
    33  	params := []struct {
    34  		name         string
    35  		plainHttp    bool
    36  		artifactType string
    37  		want         string
    38  	}{
    39  		{
    40  			name:         "plain http, no filter",
    41  			plainHttp:    true,
    42  			artifactType: "",
    43  			want:         "http://localhost/v2/hello-world/referrers/sha256:b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9",
    44  		},
    45  		{
    46  			name:         "https, no filter",
    47  			plainHttp:    false,
    48  			artifactType: "",
    49  			want:         "https://localhost/v2/hello-world/referrers/sha256:b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9",
    50  		},
    51  		{
    52  			name:         "plain http, filter",
    53  			plainHttp:    true,
    54  			artifactType: "signature/example",
    55  			want:         "http://localhost/v2/hello-world/referrers/sha256:b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9?artifactType=signature%2Fexample",
    56  		},
    57  		{
    58  			name:         "https, filter",
    59  			plainHttp:    false,
    60  			artifactType: "signature/example",
    61  			want:         "https://localhost/v2/hello-world/referrers/sha256:b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9?artifactType=signature%2Fexample",
    62  		},
    63  	}
    64  	for _, tt := range params {
    65  		t.Run(tt.name, func(t *testing.T) {
    66  			got := buildReferrersURL(tt.plainHttp, ref, tt.artifactType)
    67  			if !compareUrl(got, tt.want) {
    68  				t.Errorf("buildReferrersURL() = %s, want %s", got, tt.want)
    69  			}
    70  		})
    71  	}
    72  }
    73  
    74  // compareUrl compares two urls, regardless of query order and encoding
    75  func compareUrl(s1, s2 string) bool {
    76  	u1, err := url.Parse(s1)
    77  	if err != nil {
    78  		return false
    79  	}
    80  	u2, err := url.Parse(s2)
    81  	if err != nil {
    82  		return false
    83  	}
    84  	q1, err := url.ParseQuery(u1.RawQuery)
    85  	if err != nil {
    86  		return false
    87  	}
    88  	q2, err := url.ParseQuery(u2.RawQuery)
    89  	if err != nil {
    90  		return false
    91  	}
    92  	return u1.Scheme == u2.Scheme &&
    93  		reflect.DeepEqual(u1.User, u1.User) &&
    94  		u1.Host == u2.Host &&
    95  		u1.Path == u2.Path &&
    96  		reflect.DeepEqual(q1, q2)
    97  }