Repository navigation
Expand file tree
/
Copy pathimage_path_test.go
More file actions
72 lines (66 loc) · 1.69 KB
/
Copy pathimage_path_test.go
File metadata and controls
72 lines (66 loc) · 1.69 KB
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
package hypeman_test
import (
"context"
"io"
"net/http"
"strings"
"testing"
"github.com/kernel/hypeman-go"
"github.com/kernel/hypeman-go/option"
)
func TestImagePathParameterEscapesRepositorySlashes(t *testing.T) {
const imageName = "docker.io/library/alpine:latest"
const wantPath = "/images/docker.io%2Flibrary%2Falpine:latest"
tests := []struct {
name string
method string
call func(hypeman.Client) error
}{
{
name: "get",
method: http.MethodGet,
call: func(client hypeman.Client) error {
_, err := client.Images.Get(context.Background(), imageName)
return err
},
},
{
name: "delete",
method: http.MethodDelete,
call: func(client hypeman.Client) error {
return client.Images.Delete(context.Background(), imageName)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var gotPath string
client := hypeman.NewClient(
option.WithAPIKey("test-key"),
option.WithHTTPClient(&http.Client{
Transport: &closureTransport{
fn: func(req *http.Request) (*http.Response, error) {
if req.Method != tt.method {
t.Errorf("method = %s, want %s", req.Method, tt.method)
}
gotPath = req.URL.EscapedPath()
status := http.StatusNoContent
if tt.method == http.MethodGet {
status = http.StatusNotFound
}
return &http.Response{
StatusCode: status,
Header: make(http.Header),
Body: io.NopCloser(strings.NewReader(`{"code":"not_found","message":"not found"}`)),
}, nil
},
},
}),
)
_ = tt.call(client)
if gotPath != wantPath {
t.Fatalf("escaped path = %q, want %q", gotPath, wantPath)
}
})
}
}