Skip to content

Commit b8cb380

Browse files
committed
test: reach 100% coverage
1 parent 30befbf commit b8cb380

File tree

4 files changed

+202
-1
lines changed

4 files changed

+202
-1
lines changed

src/app/app_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ func TestCompressFiles(t *testing.T) {
101101
os.Remove("app_internal_test.go.br")
102102
os.Remove("auth.go.br")
103103
os.Remove("auth_test.go.br")
104+
os.Remove("auth_internal_test.go.br")
104105
os.Remove("app.go.br")
105106

106107
params.Directory = "../../test/frontend/dist/vite.svg.br"

src/app/auth_internal_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package app
2+
3+
import (
4+
"go-http-server/param"
5+
"net/http"
6+
"os"
7+
"testing"
8+
)
9+
10+
func TestAuthLoggerDisabled(t *testing.T) {
11+
params := param.Params{
12+
Logger: false,
13+
}
14+
app := NewApp(&params)
15+
if logger := app.authLogger(); logger != nil {
16+
t.Fatalf("expected nil logger when disabled")
17+
}
18+
}
19+
20+
func TestAuthLoggerLevelsAndFormats(t *testing.T) {
21+
tests := []struct {
22+
name string
23+
logLevel string
24+
pretty bool
25+
}{
26+
{"debug json", "debug", false},
27+
{"warn pretty", "warn", true},
28+
{"error json", "error", false},
29+
{"default info pretty", "", true},
30+
}
31+
32+
for _, tt := range tests {
33+
t.Run(tt.name, func(t *testing.T) {
34+
orig := os.Getenv("LOG_LEVEL")
35+
if tt.logLevel == "" {
36+
_ = os.Unsetenv("LOG_LEVEL")
37+
} else {
38+
_ = os.Setenv("LOG_LEVEL", tt.logLevel)
39+
}
40+
t.Cleanup(func() {
41+
if orig == "" {
42+
_ = os.Unsetenv("LOG_LEVEL")
43+
} else {
44+
_ = os.Setenv("LOG_LEVEL", orig)
45+
}
46+
})
47+
48+
params := param.Params{
49+
Logger: true,
50+
LogPretty: tt.pretty,
51+
}
52+
app := NewApp(&params)
53+
logger := app.authLogger()
54+
if logger == nil {
55+
t.Fatalf("expected logger, got nil")
56+
}
57+
})
58+
}
59+
}
60+
61+
func TestListenWithAuthAndLoggerEnabled(t *testing.T) {
62+
params := param.Params{
63+
BasicAuthEnabled: true,
64+
BasicAuthUser: "user",
65+
BasicAuthPass: "pass",
66+
Logger: true,
67+
LogPretty: true,
68+
Address: "127.0.0.1",
69+
Port: 8085,
70+
}
71+
72+
called := false
73+
app := NewAppWithListenAndServe(&params, func(server *http.Server) error {
74+
if server == nil || server.Handler == nil {
75+
t.Fatalf("expected server and handler to be set")
76+
}
77+
called = true
78+
return nil
79+
})
80+
81+
app.Listen()
82+
if !called {
83+
t.Fatalf("expected listenAndServe to be called")
84+
}
85+
}

src/app/auth_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"go-http-server/param"
55
"net/http"
66
"net/http/httptest"
7+
"os"
78
"testing"
89
)
910

@@ -54,6 +55,41 @@ func TestBasicAuthMiddlewareMissingHeader(t *testing.T) {
5455
}
5556
}
5657

58+
func TestBasicAuthMiddlewareMissingHeaderWithLogger(t *testing.T) {
59+
orig := os.Getenv("LOG_LEVEL")
60+
_ = os.Setenv("LOG_LEVEL", "debug")
61+
t.Cleanup(func() {
62+
if orig == "" {
63+
_ = os.Unsetenv("LOG_LEVEL")
64+
} else {
65+
_ = os.Setenv("LOG_LEVEL", orig)
66+
}
67+
})
68+
69+
params := param.Params{
70+
BasicAuthEnabled: true,
71+
BasicAuthUser: "user",
72+
BasicAuthPass: "pass",
73+
Logger: true,
74+
}
75+
app := NewApp(&params)
76+
77+
handler := app.BasicAuthMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
78+
w.WriteHeader(http.StatusOK)
79+
}))
80+
81+
req := httptest.NewRequest("GET", "/", nil)
82+
rec := httptest.NewRecorder()
83+
handler.ServeHTTP(rec, req)
84+
85+
if rec.Code != http.StatusUnauthorized {
86+
t.Fatalf("expected status %d, got %d", http.StatusUnauthorized, rec.Code)
87+
}
88+
if got := rec.Header().Get("WWW-Authenticate"); got != "Basic realm=\"Restricted\"" {
89+
t.Fatalf("expected realm header, got %s", got)
90+
}
91+
}
92+
5793
func TestBasicAuthMiddlewareWrongCredentials(t *testing.T) {
5894
params := param.Params{
5995
BasicAuthEnabled: true,
@@ -105,3 +141,37 @@ func TestBasicAuthMiddlewareSuccess(t *testing.T) {
105141
t.Fatalf("expected body ok, got %s", rec.Body.String())
106142
}
107143
}
144+
145+
func TestBasicAuthMiddlewareWithLoggerEnabled(t *testing.T) {
146+
orig := os.Getenv("LOG_LEVEL")
147+
_ = os.Setenv("LOG_LEVEL", "debug")
148+
t.Cleanup(func() {
149+
if orig == "" {
150+
_ = os.Unsetenv("LOG_LEVEL")
151+
} else {
152+
_ = os.Setenv("LOG_LEVEL", orig)
153+
}
154+
})
155+
156+
params := param.Params{
157+
BasicAuthEnabled: true,
158+
BasicAuthUser: "user",
159+
BasicAuthPass: "pass",
160+
Logger: true,
161+
LogPretty: true,
162+
}
163+
app := NewApp(&params)
164+
165+
handler := app.BasicAuthMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
166+
w.WriteHeader(http.StatusOK)
167+
}))
168+
169+
req := httptest.NewRequest("GET", "/", nil)
170+
req.SetBasicAuth("user", "pass")
171+
rec := httptest.NewRecorder()
172+
handler.ServeHTTP(rec, req)
173+
174+
if rec.Code != http.StatusOK {
175+
t.Fatalf("expected status %d, got %d", http.StatusOK, rec.Code)
176+
}
177+
}

src/param/param_test.go

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import (
44
"context"
55
"errors"
66
"flag"
7+
"github.com/urfave/cli/v2"
78
"go-http-server/param"
89
"path/filepath"
910
"reflect"
1011
"testing"
11-
"github.com/urfave/cli/v2"
1212
)
1313

1414
func TestContextToParams(t *testing.T) {
@@ -179,3 +179,48 @@ func TestContextToParamsInvalidBasicAuthFormat(t *testing.T) {
179179
})
180180
}
181181
}
182+
183+
func TestContextToParamsBasicAuthDefaultRealm(t *testing.T) {
184+
f := flag.NewFlagSet("a", flag.ContinueOnError)
185+
f.String("directory", "example", "")
186+
f.String("basic-auth", "user:pass", "")
187+
188+
ctx := cli.NewContext(nil, f, nil)
189+
ctx.Context = context.WithValue(context.Background(), "key", "val")
190+
191+
params, err := param.ContextToParamsWithAbs(ctx, func(s string) (string, error) {
192+
return s, nil
193+
})
194+
if err != nil {
195+
t.Fatalf("expected nil error, got %v", err)
196+
}
197+
if !params.BasicAuthEnabled {
198+
t.Fatalf("expected basic auth enabled")
199+
}
200+
if params.BasicAuthRealm != "Restricted" {
201+
t.Fatalf("expected default realm Restricted, got %s", params.BasicAuthRealm)
202+
}
203+
}
204+
205+
func TestContextToParamsBasicAuthDisabledWhenEmpty(t *testing.T) {
206+
f := flag.NewFlagSet("a", flag.ContinueOnError)
207+
f.String("directory", "example", "")
208+
f.String("basic-auth", "", "")
209+
f.String("basic-auth-realm", "ShouldNotApply", "")
210+
211+
ctx := cli.NewContext(nil, f, nil)
212+
ctx.Context = context.WithValue(context.Background(), "key", "val")
213+
214+
params, err := param.ContextToParamsWithAbs(ctx, func(s string) (string, error) {
215+
return s, nil
216+
})
217+
if err != nil {
218+
t.Fatalf("expected nil error, got %v", err)
219+
}
220+
if params.BasicAuthEnabled {
221+
t.Fatalf("expected basic auth disabled")
222+
}
223+
if params.BasicAuthRealm != "ShouldNotApply" {
224+
t.Fatalf("expected realm value to be preserved, got %s", params.BasicAuthRealm)
225+
}
226+
}

0 commit comments

Comments
 (0)