Skip to content

Commit

Permalink
Add test coverage for disallowed methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobbednarz committed Aug 1, 2017
1 parent 36dacd7 commit 3cf4c2c
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions csp_collector_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"net/http"
"net/http/httptest"
"testing"
)

func TestHandlerForDisallowedMethods(t *testing.T) {
disallowedMethods := []string{"GET", "DELETE", "PUT", "TRACE", "PATCH"}
randomUrls := []string{"/", "/blah"}

for _, method := range disallowedMethods {
for _, url := range randomUrls {
t.Run(method+url, func(t *testing.T) {
request, err := http.NewRequest(method, "localhost:8080"+url, nil)
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
recorder := httptest.NewRecorder()
handleViolationReport(recorder, request)

response := recorder.Result()
defer response.Body.Close()

if response.StatusCode != http.StatusMethodNotAllowed {
t.Errorf("Expected HTTP status %v; got %v", http.StatusMethodNotAllowed, response.Status)
}
})
}
}
}

0 comments on commit 3cf4c2c

Please sign in to comment.