MCPcopy Create free account
hub / github.com/coder/coder / TestOAuth2BearerTokenMalformedHeaders

Function TestOAuth2BearerTokenMalformedHeaders

coderd/httpmw/rfc6750_extended_test.go:151–230  ·  view source on GitHub ↗

TestOAuth2BearerTokenMalformedHeaders tests handling of malformed Bearer headers per RFC 6750 nolint:tparallel,paralleltest // Subtests share a DB; run sequentially to avoid Windows DB cleanup flake.

(t *testing.T)

Source from the content-addressed store, hash-verified

149//
150//nolint:tparallel,paralleltest // Subtests share a DB; run sequentially to avoid Windows DB cleanup flake.
151func TestOAuth2BearerTokenMalformedHeaders(t *testing.T) {
152 t.Parallel()
153
154 db, _ := dbtestutil.NewDB(t)
155
156 middleware := httpmw.ExtractAPIKeyMW(httpmw.ExtractAPIKeyConfig{
157 DB: db,
158 })
159
160 handler := middleware(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
161 rw.WriteHeader(http.StatusOK)
162 }))
163
164 tests := []struct {
165 name string
166 authHeader string
167 expectedStatus int
168 shouldHaveWWW bool
169 }{
170 {
171 name: "MissingBearer",
172 authHeader: "invalid-token",
173 expectedStatus: http.StatusUnauthorized,
174 shouldHaveWWW: true,
175 },
176 {
177 name: "CaseSensitive",
178 authHeader: "bearer token", // lowercase should still work
179 expectedStatus: http.StatusUnauthorized,
180 shouldHaveWWW: true,
181 },
182 {
183 name: "ExtraSpaces",
184 authHeader: "Bearer token-with-extra-spaces",
185 expectedStatus: http.StatusUnauthorized,
186 shouldHaveWWW: true,
187 },
188 {
189 name: "EmptyToken",
190 authHeader: "Bearer ",
191 expectedStatus: http.StatusUnauthorized,
192 shouldHaveWWW: true,
193 },
194 {
195 name: "OnlyBearer",
196 authHeader: "Bearer",
197 expectedStatus: http.StatusUnauthorized,
198 shouldHaveWWW: true,
199 },
200 {
201 name: "MultipleBearer",
202 authHeader: "Bearer token1 Bearer token2",
203 expectedStatus: http.StatusUnauthorized,
204 shouldHaveWWW: true,
205 },
206 {
207 name: "InvalidBase64",
208 authHeader: "Bearer !!!invalid-base64!!!",

Callers

nothing calls this directly

Calls 10

NewDBFunction · 0.92
ExtractAPIKeyMWFunction · 0.92
RunMethod · 0.65
SetMethod · 0.65
GetMethod · 0.65
WriteHeaderMethod · 0.45
ServeHTTPMethod · 0.45
EqualMethod · 0.45
HeaderMethod · 0.45
ContainsMethod · 0.45

Tested by

no test coverage detected