Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c6534f6

Browse files
committed
Fix tests
1 parent d6c404c commit c6534f6

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

coderd/httpmw/hsts.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ const (
2020
// This header only makes sense if the app is using tls.
2121
// Full header example
2222
// Strict-Transport-Security: max-age=63072000; includeSubDomains; preload
23-
func HSTS(hsts bool) func(next http.Handler) http.Handler {
23+
// nolint:revive
24+
func HSTS(enable bool) func(next http.Handler) http.Handler {
2425
return func(next http.Handler) http.Handler {
2526
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
26-
if hsts {
27+
if enable {
2728
w.Header().Set(HSTSHeader, fmt.Sprintf("max-age=%d", int64(HSTSMaxAge)))
2829
}
2930

coderd/httpmw/hsts_test.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,40 @@ import (
66
"net/http/httptest"
77
"testing"
88

9-
"github.com/coder/coder/coderd/httpmw"
109
"github.com/go-chi/chi/v5"
1110
"github.com/stretchr/testify/require"
11+
12+
"github.com/coder/coder/coderd/httpmw"
1213
)
1314

1415
func TestHSTS(t *testing.T) {
1516
t.Parallel()
16-
setup := func(hsts bool) *http.Response {
17+
18+
setup := func(enable bool) *http.Response {
1719
rw := httptest.NewRecorder()
1820
r := httptest.NewRequest("GET", "/", nil)
1921

2022
rtr := chi.NewRouter()
21-
rtr.Use(httpmw.HSTS(hsts))
23+
rtr.Use(httpmw.HSTS(enable))
2224
rtr.Get("/", func(w http.ResponseWriter, r *http.Request) {
2325
_, _ = w.Write([]byte("hello!"))
2426
})
2527
rtr.ServeHTTP(rw, r)
26-
res := rw.Result()
27-
defer res.Body.Close()
28-
return res
28+
return rw.Result()
2929
}
3030

3131
t.Run("True", func(t *testing.T) {
3232
t.Parallel()
3333

3434
res := setup(true)
35+
defer res.Body.Close()
3536
require.Contains(t, res.Header.Get(httpmw.HSTSHeader), fmt.Sprintf("max-age=%d", int64(httpmw.HSTSMaxAge)))
3637
})
3738
t.Run("False", func(t *testing.T) {
3839
t.Parallel()
3940

4041
res := setup(false)
42+
defer res.Body.Close()
4143
require.NotContains(t, res.Header.Get(httpmw.HSTSHeader), fmt.Sprintf("max-age=%d", int64(httpmw.HSTSMaxAge)))
4244
})
4345
}

0 commit comments

Comments
 (0)