MCPcopy
hub / github.com/fabiolb/fabio / TestBasic_Authorised

Function TestBasic_Authorised

auth/basic_test.go:108–169  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

106}
107
108func TestBasic_Authorised(t *testing.T) {
109 basicAuth, err := createBasicAuth("foo", "bar", t)
110 creds := []byte("foo:bar")
111
112 if err != nil {
113 t.Fatal(err)
114 }
115
116 tests := []struct {
117 name string
118 req *http.Request
119 res http.ResponseWriter
120 out bool
121 }{
122 {
123 "correct credentials should be authorized",
124 &http.Request{
125 Header: http.Header{
126 "Authorization": []string{fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString(creds))},
127 },
128 },
129 &responseWriter{},
130 true,
131 },
132 {
133 "incorrect credentials should not be authorized",
134 &http.Request{
135 Header: http.Header{
136 "Authorization": []string{fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte("baz:blarg")))},
137 },
138 },
139 &responseWriter{},
140 false,
141 },
142 {
143 "missing Authorization header should not be authorized",
144 &http.Request{
145 Header: http.Header{},
146 },
147 &responseWriter{},
148 false,
149 },
150 {
151 "malformed Authorization header should not be authorized",
152 &http.Request{
153 Header: http.Header{
154 "Authorization": []string{"malformed"},
155 },
156 },
157 &responseWriter{},
158 false,
159 },
160 }
161
162 for _, tt := range tests {
163 t.Run(tt.name, func(t *testing.T) {
164 if got, want := basicAuth.Authorized(tt.req, tt.res), tt.out; !reflect.DeepEqual(got, want) {
165 t.Errorf("got %v want %v", got, want)

Callers

nothing calls this directly

Calls 3

createBasicAuthFunction · 0.85
FatalMethod · 0.80
AuthorizedMethod · 0.65

Tested by

no test coverage detected