File: handlers_test.go

package info (click to toggle)
golang-github-hashicorp-go-cleanhttp 0.5.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, buster-proposed-updates, sid
  • size: 132 kB
  • sloc: makefile: 2
file content (72 lines) | stat: -rw-r--r-- 1,480 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
package cleanhttp

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"testing"
)

func TestPrintablePathCheckHandler(t *testing.T) {
	getTestHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintln(w, "Hello, client")
	})

	cases := map[string]struct {
		path       string
		expectCode int
		input      *HandlerInput
	}{
		"valid nil input": {
			path:       "/valid",
			expectCode: http.StatusOK,
			input:      nil,
		},

		"valid empty error status": {
			path:       "/valid",
			expectCode: http.StatusOK,
			input:      &HandlerInput{},
		},

		"invalid newline": {
			path:       "/invalid%0A",
			expectCode: http.StatusBadRequest,
		},

		"invalid carriage return": {
			path:       "/invalid%0D",
			expectCode: http.StatusBadRequest,
		},

		"invalid null": {
			path:       "/invalid%00",
			expectCode: http.StatusBadRequest,
		},

		"invalid alternate status": {
			path:       "/invalid%0A",
			expectCode: http.StatusInternalServerError,
			input: &HandlerInput{
				ErrStatus: http.StatusInternalServerError,
			},
		},
	}

	for name, tc := range cases {
		t.Run(name, func(t *testing.T) {
			// Create test HTTP server
			ts := httptest.NewServer(PrintablePathCheckHandler(getTestHandler, tc.input))
			defer ts.Close()

			res, err := http.Get(ts.URL + tc.path)
			if err != nil {
				t.Fatal(err)
			}

			if tc.expectCode != res.StatusCode {
				t.Fatalf("expected %d, got :%d", tc.expectCode, res.StatusCode)
			}
		})
	}
}