|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "go-http-server/param" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestBasicAuthMiddlewareDisabled(t *testing.T) { |
| 11 | + params := param.Params{ |
| 12 | + BasicAuthEnabled: false, |
| 13 | + } |
| 14 | + app := NewApp(¶ms) |
| 15 | + |
| 16 | + handler := app.BasicAuthMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 17 | + w.WriteHeader(http.StatusOK) |
| 18 | + w.Write([]byte("ok")) |
| 19 | + })) |
| 20 | + |
| 21 | + req := httptest.NewRequest("GET", "/", nil) |
| 22 | + rec := httptest.NewRecorder() |
| 23 | + handler.ServeHTTP(rec, req) |
| 24 | + |
| 25 | + if rec.Code != http.StatusOK { |
| 26 | + t.Fatalf("expected status %d, got %d", http.StatusOK, rec.Code) |
| 27 | + } |
| 28 | + if rec.Body.String() != "ok" { |
| 29 | + t.Fatalf("expected body ok, got %s", rec.Body.String()) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func TestBasicAuthMiddlewareMissingHeader(t *testing.T) { |
| 34 | + params := param.Params{ |
| 35 | + BasicAuthEnabled: true, |
| 36 | + BasicAuthUser: "user", |
| 37 | + BasicAuthPass: "pass", |
| 38 | + } |
| 39 | + app := NewApp(¶ms) |
| 40 | + |
| 41 | + handler := app.BasicAuthMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 42 | + w.WriteHeader(http.StatusOK) |
| 43 | + })) |
| 44 | + |
| 45 | + req := httptest.NewRequest("GET", "/", nil) |
| 46 | + rec := httptest.NewRecorder() |
| 47 | + handler.ServeHTTP(rec, req) |
| 48 | + |
| 49 | + if rec.Code != http.StatusUnauthorized { |
| 50 | + t.Fatalf("expected status %d, got %d", http.StatusUnauthorized, rec.Code) |
| 51 | + } |
| 52 | + if got := rec.Header().Get("WWW-Authenticate"); got != "Basic realm=\"Restricted\"" { |
| 53 | + t.Fatalf("expected realm header, got %s", got) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func TestBasicAuthMiddlewareWrongCredentials(t *testing.T) { |
| 58 | + params := param.Params{ |
| 59 | + BasicAuthEnabled: true, |
| 60 | + BasicAuthUser: "user", |
| 61 | + BasicAuthPass: "pass", |
| 62 | + BasicAuthRealm: "CustomRealm", |
| 63 | + } |
| 64 | + app := NewApp(¶ms) |
| 65 | + |
| 66 | + handler := app.BasicAuthMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 67 | + w.WriteHeader(http.StatusOK) |
| 68 | + })) |
| 69 | + |
| 70 | + req := httptest.NewRequest("GET", "/", nil) |
| 71 | + req.SetBasicAuth("user", "wrong") |
| 72 | + rec := httptest.NewRecorder() |
| 73 | + handler.ServeHTTP(rec, req) |
| 74 | + |
| 75 | + if rec.Code != http.StatusUnauthorized { |
| 76 | + t.Fatalf("expected status %d, got %d", http.StatusUnauthorized, rec.Code) |
| 77 | + } |
| 78 | + if got := rec.Header().Get("WWW-Authenticate"); got != "Basic realm=\"CustomRealm\"" { |
| 79 | + t.Fatalf("expected realm header, got %s", got) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func TestBasicAuthMiddlewareSuccess(t *testing.T) { |
| 84 | + params := param.Params{ |
| 85 | + BasicAuthEnabled: true, |
| 86 | + BasicAuthUser: "user", |
| 87 | + BasicAuthPass: "pass", |
| 88 | + } |
| 89 | + app := NewApp(¶ms) |
| 90 | + |
| 91 | + handler := app.BasicAuthMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 92 | + w.WriteHeader(http.StatusOK) |
| 93 | + w.Write([]byte("ok")) |
| 94 | + })) |
| 95 | + |
| 96 | + req := httptest.NewRequest("GET", "/", nil) |
| 97 | + req.SetBasicAuth("user", "pass") |
| 98 | + rec := httptest.NewRecorder() |
| 99 | + handler.ServeHTTP(rec, req) |
| 100 | + |
| 101 | + if rec.Code != http.StatusOK { |
| 102 | + t.Fatalf("expected status %d, got %d", http.StatusOK, rec.Code) |
| 103 | + } |
| 104 | + if rec.Body.String() != "ok" { |
| 105 | + t.Fatalf("expected body ok, got %s", rec.Body.String()) |
| 106 | + } |
| 107 | +} |
0 commit comments