...

Source file src/oras.land/oras-go/pkg/registry/remote/utils_test.go

Documentation: oras.land/oras-go/pkg/registry/remote

     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  package remote
    16  
    17  import (
    18  	"net/http"
    19  	"net/url"
    20  	"testing"
    21  )
    22  
    23  func Test_parseLink(t *testing.T) {
    24  	tests := []struct {
    25  		name    string
    26  		url     string
    27  		header  string
    28  		want    string
    29  		wantErr bool
    30  	}{
    31  		{
    32  			name:   "catalog",
    33  			url:    "https://localhost:5000/v2/_catalog",
    34  			header: `</v2/_catalog?last=alpine&n=1>; rel="next"`,
    35  			want:   "https://localhost:5000/v2/_catalog?last=alpine&n=1",
    36  		},
    37  		{
    38  			name:   "list tag",
    39  			url:    "https://localhost:5000/v2/hello-world/tags/list",
    40  			header: `</v2/hello-world/tags/list?last=latest&n=1>; rel="next"`,
    41  			want:   "https://localhost:5000/v2/hello-world/tags/list?last=latest&n=1",
    42  		},
    43  		{
    44  			name:   "other domain",
    45  			url:    "https://localhost:5000/v2/_catalog",
    46  			header: `<https://localhost:5001/v2/_catalog?last=alpine&n=1>; rel="next"`,
    47  			want:   "https://localhost:5001/v2/_catalog?last=alpine&n=1",
    48  		},
    49  		{
    50  			name:    "invalid header",
    51  			url:     "https://localhost:5000/v2/_catalog",
    52  			header:  `</v2/_catalog`,
    53  			wantErr: true,
    54  		},
    55  	}
    56  	for _, tt := range tests {
    57  		t.Run(tt.name, func(t *testing.T) {
    58  			url, err := url.Parse(tt.url)
    59  			if err != nil {
    60  				t.Errorf("fail to parse url in the test case: %v", err)
    61  			}
    62  			resp := &http.Response{
    63  				Request: &http.Request{
    64  					URL: url,
    65  				},
    66  				Header: http.Header{
    67  					"Link": []string{tt.header},
    68  				},
    69  			}
    70  			got, err := parseLink(resp)
    71  			if (err != nil) != tt.wantErr {
    72  				t.Errorf("parseLink() error = %v, wantErr %v", err, tt.wantErr)
    73  				return
    74  			}
    75  			if got != tt.want {
    76  				t.Errorf("parseLink() = %v, want %v", got, tt.want)
    77  			}
    78  		})
    79  	}
    80  }
    81  

View as plain text