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)
| 149 | // |
| 150 | //nolint:tparallel,paralleltest // Subtests share a DB; run sequentially to avoid Windows DB cleanup flake. |
| 151 | func 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!!!", |
nothing calls this directly
no test coverage detected