|
4 | 4 | "go-http-server/param" |
5 | 5 | "net/http" |
6 | 6 | "net/http/httptest" |
| 7 | + "os" |
7 | 8 | "testing" |
8 | 9 | ) |
9 | 10 |
|
@@ -54,6 +55,41 @@ func TestBasicAuthMiddlewareMissingHeader(t *testing.T) { |
54 | 55 | } |
55 | 56 | } |
56 | 57 |
|
| 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(¶ms) |
| 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 | + |
57 | 93 | func TestBasicAuthMiddlewareWrongCredentials(t *testing.T) { |
58 | 94 | params := param.Params{ |
59 | 95 | BasicAuthEnabled: true, |
@@ -105,3 +141,37 @@ func TestBasicAuthMiddlewareSuccess(t *testing.T) { |
105 | 141 | t.Fatalf("expected body ok, got %s", rec.Body.String()) |
106 | 142 | } |
107 | 143 | } |
| 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(¶ms) |
| 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 | +} |
0 commit comments