detached d69dfa0

test: harden HTTP onboarding coverage

commit
d69dfa04911b47fd60c99e70385db313358d1ec0
author
brice · · 2026-04-02 03:49 UTC
parent
0b996ab
signature
GPG VerifiedCD88F9E564E0102646FB6077A1A07C7C7C3E349D · arch-gpg Verified for the committer email
2 files · +13 -4
M internal/adapters/in/http/middleware/cidr_test.go +1 -1
@@ -175,7 +175,7 @@ func TestHTTPSRedirect_InvalidExplicitPort_IsPreserved(t *testing.T) {
175175
176176 handler := HTTPSRedirect(nil, 8088, 8443, true, log)(ok)
177177
178- req := httptest.NewRequest(http.MethodGet, "http://example.com/", nil)
178+ req := httptest.NewRequest(http.MethodGet, "http://o2.bnema.dev/", nil)
179179 req.Host = "o2.bnema.dev:abcd"
180180 req.RemoteAddr = "203.0.113.10:12345"
181181 rr := httptest.NewRecorder()
@@ -175,7 +175,7 @@ func TestHTTPSRedirect_InvalidExplicitPort_IsPreserved(t *testing.T) {
175 175
176 handler := HTTPSRedirect(nil, 8088, 8443, true, log)(ok)176 handler := HTTPSRedirect(nil, 8088, 8443, true, log)(ok)
177 177
178- req := httptest.NewRequest(http.MethodGet, "http://example.com/", nil)178+ req := httptest.NewRequest(http.MethodGet, "http://o2.bnema.dev/", nil)
179 req.Host = "o2.bnema.dev:abcd"179 req.Host = "o2.bnema.dev:abcd"
180 req.RemoteAddr = "203.0.113.10:12345"180 req.RemoteAddr = "203.0.113.10:12345"
181 rr := httptest.NewRecorder()181 rr := httptest.NewRecorder()
M internal/app/run_http_handlers_test.go +12 -3
@@ -5,6 +5,8 @@ import (
55 "io"
66 "net/http"
77 "net/http/httptest"
8+ "os"
9+ "path/filepath"
810 "testing"
911
1012 "github.com/stretchr/testify/assert"
@@ -244,9 +246,13 @@ func TestCreateHTTPHandlers_HTTPSOnboarding_HEADRoutesRemainAvailable(t *testing
244246 for _, path := range []string{"/ca", "/ca.crt", "/ca.mobileconfig"} {
245247 resp, err := ts.Client().Head(ts.URL + path)
246248 require.NoError(t, err)
247- resp.Body.Close()
249+
250+ body, err := io.ReadAll(resp.Body)
251+ require.NoError(t, err)
252+ require.NoError(t, resp.Body.Close())
248253
249254 assert.Equal(t, http.StatusOK, resp.StatusCode, "HEAD %s should return 200", path)
255+ assert.Empty(t, body, "HEAD %s should have empty body", path)
250256 }
251257 }
252258
@@ -350,8 +356,11 @@ func TestCreateHTTPHandlers_TLSConfiguredWithoutCA_FailsStartup(t *testing.T) {
350356 svc: &services{},
351357 }
352358 si.cfg.Server.TLSPort = 8443
353- // Point data dir to a non-existent path under /proc to guarantee failure.
354- si.cfg.Server.DataDir = "/proc/nonexistent/path"
359+ dataDir := t.TempDir()
360+ blockingPath := filepath.Join(dataDir, "not-a-directory")
361+ require.NoError(t, os.WriteFile(blockingPath, []byte("blocking file"), 0600))
362+ // Point data dir to a file path so PKI directory creation fails portably.
363+ si.cfg.Server.DataDir = blockingPath
355364
356365 err := si.initPKI()
357366 assert.Error(t, err, "initPKI should fail when CA cannot be initialized")
@@ -5,6 +5,8 @@ import (
5 "io"5 "io"
6 "net/http"6 "net/http"
7 "net/http/httptest"7 "net/http/httptest"
8+ "os"
9+ "path/filepath"
8 "testing"10 "testing"
9 11
10 "github.com/stretchr/testify/assert"12 "github.com/stretchr/testify/assert"
@@ -244,9 +246,13 @@ func TestCreateHTTPHandlers_HTTPSOnboarding_HEADRoutesRemainAvailable(t *testing
244 for _, path := range []string{"/ca", "/ca.crt", "/ca.mobileconfig"} {246 for _, path := range []string{"/ca", "/ca.crt", "/ca.mobileconfig"} {
245 resp, err := ts.Client().Head(ts.URL + path)247 resp, err := ts.Client().Head(ts.URL + path)
246 require.NoError(t, err)248 require.NoError(t, err)
247- resp.Body.Close()249+
250+ body, err := io.ReadAll(resp.Body)
251+ require.NoError(t, err)
252+ require.NoError(t, resp.Body.Close())
248 253
249 assert.Equal(t, http.StatusOK, resp.StatusCode, "HEAD %s should return 200", path)254 assert.Equal(t, http.StatusOK, resp.StatusCode, "HEAD %s should return 200", path)
255+ assert.Empty(t, body, "HEAD %s should have empty body", path)
250 }256 }
251 }257 }
252 258
@@ -350,8 +356,11 @@ func TestCreateHTTPHandlers_TLSConfiguredWithoutCA_FailsStartup(t *testing.T) {
350 svc: &services{},356 svc: &services{},
351 }357 }
352 si.cfg.Server.TLSPort = 8443358 si.cfg.Server.TLSPort = 8443
353- // Point data dir to a non-existent path under /proc to guarantee failure.359+ dataDir := t.TempDir()
354- si.cfg.Server.DataDir = "/proc/nonexistent/path"360+ blockingPath := filepath.Join(dataDir, "not-a-directory")
361+ require.NoError(t, os.WriteFile(blockingPath, []byte("blocking file"), 0600))
362+ // Point data dir to a file path so PKI directory creation fails portably.
363+ si.cfg.Server.DataDir = blockingPath
355 364
356 err := si.initPKI()365 err := si.initPKI()
357 assert.Error(t, err, "initPKI should fail when CA cannot be initialized")366 assert.Error(t, err, "initPKI should fail when CA cannot be initialized")

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 7

Tags 106

↑↓ moveenter openesc close