File: http_test.go

package info (click to toggle)
golang-k8s-sigs-release-utils 0.8.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 456 kB
  • sloc: sh: 21; makefile: 5
file content (464 lines) | stat: -rw-r--r-- 12,550 bytes parent folder | download
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
Copyright 2020 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package http_test

import (
	"bytes"
	"errors"
	"fmt"
	"io"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"

	"github.com/stretchr/testify/require"

	khttp "sigs.k8s.io/release-utils/http"
	"sigs.k8s.io/release-utils/http/httpfakes"
)

func TestGetURLResponseSuccess(t *testing.T) {
	// Given
	server := httptest.NewServer(http.HandlerFunc(
		func(w http.ResponseWriter, _ *http.Request) {
			_, err := io.WriteString(w, "")
			if err != nil {
				t.Fail()
			}
		}))
	defer server.Close()

	// When
	actual, err := khttp.GetURLResponse(server.URL, false)

	// Then
	require.NoError(t, err)
	require.Empty(t, actual)
}

func TestGetURLResponseSuccessTrimmed(t *testing.T) {
	// Given
	const expected = "     some test     "
	server := httptest.NewServer(http.HandlerFunc(
		func(w http.ResponseWriter, _ *http.Request) {
			_, err := io.WriteString(w, expected)
			if err != nil {
				t.Fail()
			}
		}))
	defer server.Close()

	// When
	actual, err := khttp.GetURLResponse(server.URL, true)

	// Then
	require.NoError(t, err)
	require.Equal(t, strings.TrimSpace(expected), actual)
}

func TestGetURLResponseFailedStatus(t *testing.T) {
	// Given
	server := httptest.NewServer(http.HandlerFunc(
		func(w http.ResponseWriter, _ *http.Request) {
			w.WriteHeader(http.StatusBadRequest)
		}))
	defer server.Close()

	// When
	_, err := khttp.GetURLResponse(server.URL, true)

	// Then
	require.Error(t, err)
}

func NewTestAgent() *khttp.Agent {
	agent := khttp.NewAgent()
	agent.SetImplementation(&httpfakes.FakeAgentImplementation{})
	return agent
}

func TestAgentPost(t *testing.T) {
	t.Parallel()
	agent := NewTestAgent().WithRetries(0)
	resp := getTestResponse()
	defer resp.Body.Close()

	// First simulate a successful request
	fake := &httpfakes.FakeAgentImplementation{}
	fake.SendPostRequestReturns(resp, nil)

	agent.SetImplementation(fake)
	body, err := agent.Post("http://www.example.com/", []byte("Test string"))
	require.NoError(t, err)
	require.Equal(t, body, []byte("hello sig-release!"))

	// Now check error is handled
	fake.SendPostRequestReturns(resp, errors.New("HTTP Post error"))
	agent.SetImplementation(fake)
	_, err = agent.Post("http://www.example.com/", []byte("Test string"))
	require.Error(t, err)
}

func TestAgentGet(t *testing.T) {
	t.Parallel()
	agent := NewTestAgent().WithRetries(0)

	for _, tc := range []struct {
		name     string
		mustErr  bool
		expected []byte
		prepare  func(*httpfakes.FakeAgentImplementation)
	}{
		{
			"no-error",
			false,
			[]byte("hello sig-release!"),
			func(fai *httpfakes.FakeAgentImplementation) {
				t.Helper()
				resp := getTestResponse()
				defer resp.Body.Close()
				fai.SendGetRequestReturns(resp, nil)
			},
		}, {
			"error",
			true,
			nil,
			func(fai *httpfakes.FakeAgentImplementation) {
				t.Helper()
				fai.SendGetRequestReturns(nil, errors.New("HTTP Post error"))
			},
		},
	} {
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()
			fake := &httpfakes.FakeAgentImplementation{}
			tc.prepare(fake)
			agent.SetImplementation(fake)
			b, err := agent.Get("http://www.example.com/")
			if tc.mustErr {
				require.Error(t, err)
				return
			}
			require.NoError(t, err)
			require.Equal(t, tc.expected, b)
		})
	}
}

func TestAgentGetToWriter(t *testing.T) {
	agent := NewTestAgent()
	for _, tc := range []struct {
		n       string
		prepare func(*httpfakes.FakeAgentImplementation, *http.Response)
		mustErr bool
	}{
		{
			n: "success",
			prepare: func(fake *httpfakes.FakeAgentImplementation, resp *http.Response) {
				fake.SendGetRequestReturns(resp, nil)
			},
		},
		{
			n: "fail",
			prepare: func(fake *httpfakes.FakeAgentImplementation, resp *http.Response) {
				fake.SendGetRequestReturns(resp, errors.New("HTTP Post error"))
			},
			mustErr: true,
		},
	} {
		t.Run(tc.n, func(t *testing.T) {
			// First simulate a successful request
			fake := &httpfakes.FakeAgentImplementation{}
			resp := getTestResponse()
			defer resp.Body.Close()
			tc.prepare(fake, resp)
			var buf bytes.Buffer

			agent.SetImplementation(fake)
			err := agent.GetToWriter(&buf, "http://www.example.com/")
			if tc.mustErr {
				require.Error(t, err)
				return
			}
			require.NoError(t, err)
			require.Equal(t, buf.Bytes(), []byte("hello sig-release!"))
		})
	}
}

func TestAgentHead(t *testing.T) {
	t.Parallel()
	agent := NewTestAgent().WithRetries(0)

	resp := getTestResponse()
	defer resp.Body.Close()

	// First simulate a successful request
	fake := &httpfakes.FakeAgentImplementation{}
	fake.SendHeadRequestReturns(resp, nil)

	agent.SetImplementation(fake)
	b, err := agent.Head("http://www.example.com/")
	require.NoError(t, err)
	require.Equal(t, b, []byte("hello sig-release!"))

	// Now check error is handled
	fake.SendHeadRequestReturns(resp, errors.New("HTTP Head error"))
	agent.SetImplementation(fake)
	_, err = agent.Head("http://www.example.com/")
	require.Error(t, err)
}

func getTestResponse() *http.Response {
	return &http.Response{
		Status:        "200 OK",
		StatusCode:    http.StatusOK,
		Body:          io.NopCloser(bytes.NewReader([]byte("hello sig-release!"))),
		ContentLength: 18,
		Close:         true,
		Request:       &http.Request{},
	}
}

func TestAgentPostToWriter(t *testing.T) {
	for _, tc := range []struct {
		n       string
		prepare func(*httpfakes.FakeAgentImplementation, *http.Response)
		mustErr bool
	}{
		{
			n: "success",
			prepare: func(fake *httpfakes.FakeAgentImplementation, resp *http.Response) {
				fake.SendPostRequestReturns(resp, nil)
			},
		},
		{
			n: "fail",
			prepare: func(fake *httpfakes.FakeAgentImplementation, resp *http.Response) {
				fake.SendPostRequestReturns(resp, errors.New("HTTP Post error"))
			},
			mustErr: true,
		},
	} {
		t.Run(tc.n, func(t *testing.T) {
			agent := NewTestAgent()
			// First simulate a successful request
			fake := &httpfakes.FakeAgentImplementation{}
			resp := getTestResponse()
			defer resp.Body.Close()
			tc.prepare(fake, resp)
			var buf bytes.Buffer
			agent.SetImplementation(fake)
			err := agent.PostToWriter(&buf, "http://www.example.com/", []byte{})
			if tc.mustErr {
				require.Error(t, err)
				return
			}
			require.NoError(t, err)
			require.Equal(t, buf.Bytes(), []byte("hello sig-release!"))
		})
	}
}

func TestAgentOptions(t *testing.T) {
	agent := NewTestAgent()
	fake := &httpfakes.FakeAgentImplementation{}
	resp := &http.Response{
		Status:        "Fake not found",
		StatusCode:    http.StatusNotFound,
		Body:          io.NopCloser(bytes.NewReader([]byte("hello sig-release!"))),
		ContentLength: 18,
		Close:         true,
		Request:       &http.Request{},
	}
	defer resp.Body.Close()

	fake.SendGetRequestReturns(resp, nil)
	agent.SetImplementation(fake)

	// Test FailOnHTTPError
	// First we fail on server errors
	_, err := agent.WithFailOnHTTPError(true).Get("http://example.com/")
	require.Error(t, err)

	// Then we just note them and do not fail
	_, err = agent.WithFailOnHTTPError(false).Get("http://example.com/")
	require.NoError(t, err)
}

// closeHTTPResponseGroup is an internal func that closes the response bodies.
func closeHTTPResponseGroup(resps []*http.Response) {
	for i := range resps {
		if resps[i] == nil {
			continue
		}
		resps[i].Body.Close()
	}
}

func TestAgentGroupGetRequest(t *testing.T) {
	fake := &httpfakes.FakeAgentImplementation{}
	fakeUrls := []string{"http://www/1", "http://www/2", "http://www/3"}
	fake.SendGetRequestCalls(func(_ *http.Client, s string) (*http.Response, error) {
		switch s {
		case fakeUrls[0]:
			return &http.Response{
				Status:        "Fake OK",
				StatusCode:    http.StatusOK,
				Body:          io.NopCloser(bytes.NewReader([]byte("hello sig-release!"))),
				ContentLength: 18,
				Close:         true,
				Request:       &http.Request{},
			}, nil
		case fakeUrls[1]:
			return &http.Response{
				Status:        "Fake not found",
				StatusCode:    http.StatusNotFound,
				Body:          io.NopCloser(bytes.NewReader([]byte("hello sig-release!"))),
				ContentLength: 18,
				Close:         true,
				Request:       &http.Request{},
			}, nil
		case fakeUrls[2]:
			return nil, errors.New("malformed url")
		}
		return nil, nil
	})

	for _, tc := range []struct {
		name    string
		workers int
	}{
		{"no-parallelism", 1}, {"one-per-request", 3}, {"spare-workers", 5},
	} {
		t.Run(tc.name, func(t *testing.T) {
			// No retries as the errors are synthetic
			agent := NewTestAgent().WithRetries(0).WithFailOnHTTPError(false).WithMaxParallel(tc.workers)
			agent.SetImplementation(fake)

			//nolint: bodyclose // The next line closes them
			resps, errs := agent.GetRequestGroup(fakeUrls)
			defer closeHTTPResponseGroup(resps)

			require.Len(t, resps, 3)
			require.Len(t, errs, 3)

			require.NoError(t, errs[0])
			require.NoError(t, errs[1])
			require.Error(t, errs[2])

			require.Equal(t, http.StatusOK, resps[0].StatusCode)
			require.Equal(t, http.StatusNotFound, resps[1].StatusCode)
			require.Nil(t, resps[2])
		})
	}
}

func TestAgentPostRequestGroup(t *testing.T) {
	t.Parallel()
	fake := &httpfakes.FakeAgentImplementation{}
	errorURL := "fake:error"
	httpErrorURL := "fake:httpError"
	noErrorURL := "fake:ok"

	fake.SendPostRequestCalls(func(_ *http.Client, s string, _ []byte, _ string) (*http.Response, error) {
		switch s {
		case noErrorURL:
			return &http.Response{
				Status:        "Fake OK",
				StatusCode:    http.StatusOK,
				Body:          io.NopCloser(bytes.NewReader([]byte("hello sig-release!"))),
				ContentLength: 18,
				Close:         true,
				Request:       &http.Request{},
			}, nil
		case httpErrorURL:
			return &http.Response{
				Status:        "Fake not found",
				StatusCode:    http.StatusNotFound,
				Body:          io.NopCloser(bytes.NewReader([]byte("hello sig-release!"))),
				ContentLength: 18,
				Close:         true,
				Request:       &http.Request{},
			}, fmt.Errorf("HTTP error %d for %s", http.StatusNotFound, s)
		case errorURL:
			return nil, errors.New("malformed url")
		}
		return nil, nil
	})

	for _, tc := range []struct {
		name     string
		workers  int
		mustErr  bool
		urls     []string
		postData [][]byte
	}{
		{"no-parallelism", 1, false, []string{noErrorURL, noErrorURL, noErrorURL}, make([][]byte, 3)},
		{"one-per-request", 3, false, []string{noErrorURL, noErrorURL, noErrorURL}, make([][]byte, 3)},
		{"spare-workers", 5, false, []string{noErrorURL, noErrorURL, noErrorURL}, make([][]byte, 3)},
		{"uneven-postdata", 5, true, []string{noErrorURL, noErrorURL, noErrorURL}, make([][]byte, 2)},
		{"uneven-postdata2", 5, true, []string{noErrorURL, noErrorURL, noErrorURL}, make([][]byte, 4)},
		{"http-error", 5, true, []string{noErrorURL, httpErrorURL, noErrorURL}, make([][]byte, 3)},
		{"software-error", 5, true, []string{noErrorURL, errorURL, noErrorURL}, make([][]byte, 3)},
	} {
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()
			// No retries as the errors are synthetic
			agent := NewTestAgent().WithRetries(0).WithFailOnHTTPError(false).WithMaxParallel(tc.workers)
			agent.SetImplementation(fake)

			//nolint: bodyclose
			resps, errs := agent.PostRequestGroup(tc.urls, tc.postData)
			closeHTTPResponseGroup(resps)

			// If urls and postdata don't all errors should be errors
			if len(tc.urls) != len(tc.postData) {
				for i := range errs {
					require.Error(t, errs[i])
				}
				return
			}

			// Check for at least on error
			if tc.mustErr {
				require.Error(t, errors.Join(errs...))
			} else {
				require.NoError(t, errors.Join(errs...))
			}

			require.Len(t, resps, len(tc.urls))
			require.Len(t, errs, len(tc.urls))

			for i := range tc.urls {
				switch tc.urls[i] {
				case noErrorURL:
					require.NoError(t, errs[i])
					require.NotNil(t, resps[i])
					require.Equal(t, http.StatusOK, resps[i].StatusCode)
				case httpErrorURL:
					require.Error(t, errs[i])
					require.NotNil(t, resps[i])
					require.Equal(t, http.StatusNotFound, resps[i].StatusCode)
				case errorURL:
					require.Error(t, errs[i])
				}
			}
		})
	}
}