File: client_test.go

package info (click to toggle)
golang-github-apptainer-container-key-client 0.7.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 160 kB
  • sloc: makefile: 2
file content (265 lines) | stat: -rw-r--r-- 8,751 bytes parent folder | download | duplicates (2)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Copyright (c) 2019-2020, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the LICENSE.md file
// distributed with the sources of this project regarding your rights to use or distribute this
// software.

package client

import (
	"context"
	"errors"
	"io"
	"net/http"
	"net/url"
	"reflect"
	"strings"
	"testing"
)

func TestNormalizeURL(t *testing.T) {
	tests := []struct {
		name    string
		url     string
		wantErr bool
		wantURL *url.URL
	}{
		{"BadURL", ":", true, nil},
		{"BadScheme", "bad:", true, nil},
		{"HTTPBaseURL", "http://p80.pool.sks-keyservers.net", false, &url.URL{
			Scheme: "http",
			Host:   "p80.pool.sks-keyservers.net",
			Path:   "/",
		}},
		{"HTTPSBaseURL", "https://hkps.pool.sks-keyservers.net", false, &url.URL{
			Scheme: "https",
			Host:   "hkps.pool.sks-keyservers.net",
			Path:   "/",
		}},
		{"HKPBaseURL", "hkp://pool.sks-keyservers.net", false, &url.URL{
			Scheme: "http",
			Host:   "pool.sks-keyservers.net:11371",
			Path:   "/",
		}},
		{"HKPSBaseURL", "hkps://hkps.pool.sks-keyservers.net", false, &url.URL{
			Scheme: "https",
			Host:   "hkps.pool.sks-keyservers.net",
			Path:   "/",
		}},
		{"BaseURLSlash", "hkps://hkps.pool.sks-keyservers.net/", false, &url.URL{
			Scheme: "https",
			Host:   "hkps.pool.sks-keyservers.net",
			Path:   "/",
		}},
		{"BaseURLPath", "hkps://hkps.pool.sks-keyservers.net/path", false, &url.URL{
			Scheme: "https",
			Host:   "hkps.pool.sks-keyservers.net",
			Path:   "/path/",
		}},
		{"BaseURLPathSlash", "hkps://hkps.pool.sks-keyservers.net/path/", false, &url.URL{
			Scheme: "https",
			Host:   "hkps.pool.sks-keyservers.net",
			Path:   "/path/",
		}},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			u, err := normalizeURL(tt.url)
			if (err != nil) != tt.wantErr {
				t.Fatalf("got err %v, want %v", err, tt.wantErr)
			}

			if err == nil {
				if got, want := u, tt.wantURL; !reflect.DeepEqual(got, want) {
					t.Errorf("got url %v, want %v", got, want)
				}
			}
		})
	}
}

func TestNewClient(t *testing.T) {
	httpClient := &http.Client{}

	tests := []struct {
		name           string
		opts           []Option
		wantErr        error
		wantURL        string
		bearerToken    string
		wantUserAgent  string
		wantHTTPClient *http.Client
	}{
		{"UnsupportedProtocolScheme", []Option{
			OptBaseURL("bad:"),
		}, errUnsupportedProtocolScheme, "", "", "", nil},
		{"TLSRequiredHTTP", []Option{
			OptBaseURL("http://p80.pool.sks-keyservers.net"),
			OptBearerToken("blah"),
		}, ErrTLSRequired, "", "", "", nil},
		{"TLSRequiredHKP", []Option{
			OptBaseURL("hkp://pool.sks-keyservers.net"),
			OptBearerToken("blah"),
		}, ErrTLSRequired, "", "", "", nil},
		{"Defaults", nil, nil, defaultBaseURL, "", "", http.DefaultClient},
		{"BaseURL", []Option{
			OptBaseURL("hkps://hkps.pool.sks-keyservers.net"),
		}, nil, "https://hkps.pool.sks-keyservers.net/", "", "", http.DefaultClient},
		{"BaseURLSlash", []Option{
			OptBaseURL("hkps://hkps.pool.sks-keyservers.net/"),
		}, nil, "https://hkps.pool.sks-keyservers.net/", "", "", http.DefaultClient},
		{"BaseURLWithPath", []Option{
			OptBaseURL("hkps://hkps.pool.sks-keyservers.net/path"),
		}, nil, "https://hkps.pool.sks-keyservers.net/path/", "", "", http.DefaultClient},
		{"BaseURLWithPathSlash", []Option{
			OptBaseURL("hkps://hkps.pool.sks-keyservers.net/path/"),
		}, nil, "https://hkps.pool.sks-keyservers.net/path/", "", "", http.DefaultClient},
		{"BearerToken", []Option{
			OptBearerToken("blah"),
		}, nil, defaultBaseURL, "blah", "", http.DefaultClient},
		{"UserAgent", []Option{
			OptUserAgent("Secret Agent Man"),
		}, nil, defaultBaseURL, "", "Secret Agent Man", http.DefaultClient},
		{"HTTPClient", []Option{
			OptHTTPClient(httpClient),
		}, nil, defaultBaseURL, "", "", httpClient},
		{"LocalhostBearerTokenHTTP", []Option{
			OptBaseURL("http://localhost"),
			OptBearerToken("blah"),
		}, nil, "http://localhost/", "blah", "", http.DefaultClient},
		{"LocalhostBearerTokenHTTP8080", []Option{
			OptBaseURL("http://localhost:8080"),
			OptBearerToken("blah"),
		}, nil, "http://localhost:8080/", "blah", "", http.DefaultClient},
		{"LocalhostBearerTokenHKP", []Option{
			OptBaseURL("hkp://localhost"),
			OptBearerToken("blah"),
		}, nil, "http://localhost:11371/", "blah", "", http.DefaultClient},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			c, err := NewClient(tt.opts...)
			if got, want := err, tt.wantErr; !errors.Is(got, want) {
				t.Fatalf("got error %v, want %v", got, want)
			}

			if err == nil {
				if got, want := c.baseURL.String(), tt.wantURL; got != want {
					t.Errorf("got host %v, want %v", got, want)
				}

				if got, want := c.bearerToken, tt.bearerToken; got != want {
					t.Errorf("got auth token %v, want %v", got, want)
				}

				if got, want := c.userAgent, tt.wantUserAgent; got != want {
					t.Errorf("got user agent %v, want %v", got, want)
				}

				if got, want := c.httpClient, tt.wantHTTPClient; got != want {
					t.Errorf("got HTTP client %v, want %v", got, want)
				}
			}
		})
	}
}

func TestNewRequest(t *testing.T) {
	tests := []struct {
		name           string
		opts           []Option
		method         string
		path           string
		rawQuery       string
		body           string
		wantErr        bool
		wantURL        string
		wantAuthBearer string
		wantUserAgent  string
	}{
		{"BadMethod", nil, "b@d	", "", "", "", true, "", "", ""},
		{"Get", nil, http.MethodGet, "/path", "", "", false, "https://keys.openpgp.org/path", "", ""},
		{"Post", nil, http.MethodPost, "/path", "", "", false, "https://keys.openpgp.org/path", "", ""},
		{"PostRawQuery", nil, http.MethodPost, "/path", "a=b", "", false, "https://keys.openpgp.org/path?a=b", "", ""},
		{"PostBody", nil, http.MethodPost, "/path", "", "body", false, "https://keys.openpgp.org/path", "", ""},
		{"BaseURLAbsolute", []Option{
			OptBaseURL("hkps://hkps.pool.sks-keyservers.net"),
		}, http.MethodGet, "/path", "", "", false, "https://hkps.pool.sks-keyservers.net/path", "", ""},
		{"BaseURLRelative", []Option{
			OptBaseURL("hkps://hkps.pool.sks-keyservers.net"),
		}, http.MethodGet, "path", "", "", false, "https://hkps.pool.sks-keyservers.net/path", "", ""},
		{"BaseURLPathAbsolute", []Option{
			OptBaseURL("hkps://hkps.pool.sks-keyservers.net/a/b"),
		}, http.MethodGet, "/path", "", "", false, "https://hkps.pool.sks-keyservers.net/path", "", ""},
		{"BaseURLPathRelative", []Option{
			OptBaseURL("hkps://hkps.pool.sks-keyservers.net/a/b"),
		}, http.MethodGet, "path", "", "", false, "https://hkps.pool.sks-keyservers.net/a/b/path", "", ""},
		{"BearerToken", []Option{
			OptBearerToken("blah"),
		}, http.MethodGet, "/path", "", "", false, "https://keys.openpgp.org/path", "BEARER blah", ""},
		{"UserAgent", []Option{
			OptUserAgent("Secret Agent Man"),
		}, http.MethodGet, "/path", "", "", false, "https://keys.openpgp.org/path", "", "Secret Agent Man"},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			c, err := NewClient(tt.opts...)
			if err != nil {
				t.Fatal(err)
			}

			ref := &url.URL{Path: tt.path, RawQuery: tt.rawQuery}

			r, err := c.NewRequest(context.Background(), tt.method, ref, strings.NewReader(tt.body))
			if (err != nil) != tt.wantErr {
				t.Fatalf("got err %v, wantErr %v", err, tt.wantErr)
			}

			if err == nil {
				if got, want := r.Method, tt.method; got != want {
					t.Errorf("got method %v, want %v", got, want)
				}

				if got, want := r.URL.String(), tt.wantURL; got != want {
					t.Errorf("got URL %v, want %v", got, want)
				}

				b, err := io.ReadAll(r.Body)
				if err != nil {
					t.Errorf("failed to read body: %v", err)
				}
				if got, want := string(b), tt.body; got != want {
					t.Errorf("got body %v, want %v", got, want)
				}

				authBearer, ok := r.Header["Authorization"]
				if got, want := ok, (tt.wantAuthBearer != ""); got != want {
					t.Fatalf("presence of auth bearer %v, want %v", got, want)
				}
				if ok {
					if got, want := len(authBearer), 1; got != want {
						t.Fatalf("got %v auth bearer(s), want %v", got, want)
					}
					if got, want := authBearer[0], tt.wantAuthBearer; got != want {
						t.Errorf("got auth bearer %v, want %v", got, want)
					}
				}

				userAgent, ok := r.Header["User-Agent"]
				if got, want := ok, (tt.wantUserAgent != ""); got != want {
					t.Fatalf("presence of user agent %v, want %v", got, want)
				}
				if ok {
					if got, want := len(userAgent), 1; got != want {
						t.Fatalf("got %v user agent(s), want %v", got, want)
					}
					if got, want := userAgent[0], tt.wantUserAgent; got != want {
						t.Errorf("got user agent %v, want %v", got, want)
					}
				}
			}
		})
	}
}