Skip to content

Commit

Permalink
makes code tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
ksysoev committed Jun 12, 2024
1 parent d190894 commit d87d3bc
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions middleware/http/basic_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestUnauthorized(t *testing.T) {

authHeader := resp.Header.Get("WWW-Authenticate")
expectedAuthHeader := `Basic realm="` + realm + `"`

if authHeader != expectedAuthHeader {
t.Errorf("Expected WWW-Authenticate header %q, got %q", expectedAuthHeader, authHeader)
}
Expand All @@ -40,33 +41,47 @@ func TestNewBasicAuthMiddleware(t *testing.T) {
handler := middleware(nextHandler)

// Test case 1: Valid credentials
req1, _ := http.NewRequest("GET", "/", nil)
req1, _ := http.NewRequest("GET", "/", http.NoBody)
req1.SetBasicAuth("admin", "password")

w1 := httptest.NewRecorder()

handler.ServeHTTP(w1, req1)

resp1 := w1.Result()

defer resp1.Body.Close()

if resp1.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d, got %d", http.StatusOK, resp1.StatusCode)
}

// Test case 2: Invalid credentials
req2, _ := http.NewRequest("GET", "/", nil)
req2, _ := http.NewRequest("GET", "/", http.NoBody)
req2.SetBasicAuth("admin", "wrongpassword")

w2 := httptest.NewRecorder()

handler.ServeHTTP(w2, req2)

resp2 := w2.Result()

defer resp2.Body.Close()

if resp2.StatusCode != http.StatusUnauthorized {
t.Errorf("Expected status code %d, got %d", http.StatusUnauthorized, resp2.StatusCode)
}

// Test case 3: Missing credentials
req3, _ := http.NewRequest("GET", "/", nil)
req3, _ := http.NewRequest("GET", "/", http.NoBody)
w3 := httptest.NewRecorder()

handler.ServeHTTP(w3, req3)

resp3 := w3.Result()

defer resp3.Body.Close()

if resp3.StatusCode != http.StatusUnauthorized {
t.Errorf("Expected status code %d, got %d", http.StatusUnauthorized, resp3.StatusCode)
}
Expand Down

0 comments on commit d87d3bc

Please sign in to comment.