test direct HTTP onboarding HEAD routes
- commit
- 0b996ab2137889a98b945458f8295fd47dd6d3c5
- author
- brice · · 2026-04-01 14:01 UTC
- parent
- 7fff706
- signature
- GPG Verified
CD88F9E564E0102646FB6077A1A07C7C7C3E349D· arch-gpg Verified for the committer email
1 file · +36 -0
M internal/app/run_http_handlers_test.go +36 -0
| @@ -2,6 +2,7 @@ package app | |||
| 2 | 2 | ||
| 3 | 3 | import ( | |
| 4 | 4 | "context" | |
| 5 | + | "io" | |
| 5 | 6 | "net/http" | |
| 6 | 7 | "net/http/httptest" | |
| 7 | 8 | "testing" | |
| @@ -249,6 +250,41 @@ func TestCreateHTTPHandlers_HTTPSOnboarding_HEADRoutesRemainAvailable(t *testing | |||
| 249 | 250 | } | |
| 250 | 251 | } | |
| 251 | 252 | ||
| 253 | + | func TestCreateHTTPHandlers_DirectHTTPOnboarding_HEADRoutesRemainAvailable(t *testing.T) { | |
| 254 | + | t.Parallel() | |
| 255 | + | ca := newTestCA(t) | |
| 256 | + | cfg := newOnboardingConfig() | |
| 257 | + | svc := &services{caAdapter: ca} | |
| 258 | + | ||
| 259 | + | _, httpHandler, _ := createHTTPHandlers(svc, cfg, zerowrap.Default()) | |
| 260 | + | ||
| 261 | + | ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
| 262 | + | r.RemoteAddr = directAddr | |
| 263 | + | r.Host = "o2.bnema.dev" | |
| 264 | + | httpHandler.ServeHTTP(w, r) | |
| 265 | + | })) | |
| 266 | + | defer ts.Close() | |
| 267 | + | ||
| 268 | + | for _, tc := range []struct { | |
| 269 | + | path string | |
| 270 | + | contentType string | |
| 271 | + | }{ | |
| 272 | + | {path: "/ca", contentType: "text/html"}, | |
| 273 | + | {path: "/ca.crt", contentType: "application/x-x509-ca-cert"}, | |
| 274 | + | } { | |
| 275 | + | resp, err := ts.Client().Head(ts.URL + tc.path) | |
| 276 | + | require.NoError(t, err) | |
| 277 | + | ||
| 278 | + | body, err := io.ReadAll(resp.Body) | |
| 279 | + | require.NoError(t, err) | |
| 280 | + | require.NoError(t, resp.Body.Close()) | |
| 281 | + | ||
| 282 | + | assert.Equal(t, http.StatusOK, resp.StatusCode, "HEAD %s should return 200", tc.path) | |
| 283 | + | assert.Contains(t, resp.Header.Get("Content-Type"), tc.contentType) | |
| 284 | + | assert.Empty(t, body, "HEAD %s should have empty body", tc.path) | |
| 285 | + | } | |
| 286 | + | } | |
| 287 | + | ||
| 252 | 288 | func TestCreateHTTPHandlers_DirectHTTPForbiddenHEAD_ReturnsForbidden(t *testing.T) { | |
| 253 | 289 | t.Parallel() | |
| 254 | 290 | ca := newTestCA(t) | |
| @@ -2,6 +2,7 @@ package app | |||||
| 2 | 2 | ||||
| 3 | import ( | 3 | import ( | ||
| 4 | "context" | 4 | "context" | ||
| 5 | + | "io" | |||
| 5 | "net/http" | 6 | "net/http" | ||
| 6 | "net/http/httptest" | 7 | "net/http/httptest" | ||
| 7 | "testing" | 8 | "testing" | ||
| @@ -249,6 +250,41 @@ func TestCreateHTTPHandlers_HTTPSOnboarding_HEADRoutesRemainAvailable(t *testing | |||||
| 249 | } | 250 | } | ||
| 250 | } | 251 | } | ||
| 251 | 252 | ||||
| 253 | + | func TestCreateHTTPHandlers_DirectHTTPOnboarding_HEADRoutesRemainAvailable(t *testing.T) { | |||
| 254 | + | t.Parallel() | |||
| 255 | + | ca := newTestCA(t) | |||
| 256 | + | cfg := newOnboardingConfig() | |||
| 257 | + | svc := &services{caAdapter: ca} | |||
| 258 | + | ||||
| 259 | + | _, httpHandler, _ := createHTTPHandlers(svc, cfg, zerowrap.Default()) | |||
| 260 | + | ||||
| 261 | + | ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |||
| 262 | + | r.RemoteAddr = directAddr | |||
| 263 | + | r.Host = "o2.bnema.dev" | |||
| 264 | + | httpHandler.ServeHTTP(w, r) | |||
| 265 | + | })) | |||
| 266 | + | defer ts.Close() | |||
| 267 | + | ||||
| 268 | + | for _, tc := range []struct { | |||
| 269 | + | path string | |||
| 270 | + | contentType string | |||
| 271 | + | }{ | |||
| 272 | + | {path: "/ca", contentType: "text/html"}, | |||
| 273 | + | {path: "/ca.crt", contentType: "application/x-x509-ca-cert"}, | |||
| 274 | + | } { | |||
| 275 | + | resp, err := ts.Client().Head(ts.URL + tc.path) | |||
| 276 | + | require.NoError(t, err) | |||
| 277 | + | ||||
| 278 | + | body, err := io.ReadAll(resp.Body) | |||
| 279 | + | require.NoError(t, err) | |||
| 280 | + | require.NoError(t, resp.Body.Close()) | |||
| 281 | + | ||||
| 282 | + | assert.Equal(t, http.StatusOK, resp.StatusCode, "HEAD %s should return 200", tc.path) | |||
| 283 | + | assert.Contains(t, resp.Header.Get("Content-Type"), tc.contentType) | |||
| 284 | + | assert.Empty(t, body, "HEAD %s should have empty body", tc.path) | |||
| 285 | + | } | |||
| 286 | + | } | |||
| 287 | + | ||||
| 252 | func TestCreateHTTPHandlers_DirectHTTPForbiddenHEAD_ReturnsForbidden(t *testing.T) { | 288 | func TestCreateHTTPHandlers_DirectHTTPForbiddenHEAD_ReturnsForbidden(t *testing.T) { | ||
| 253 | t.Parallel() | 289 | t.Parallel() | ||
| 254 | ca := newTestCA(t) | 290 | ca := newTestCA(t) | ||