...
1
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