From 3cf4c2cfbd174ccaadce91d3b436022ff0130139 Mon Sep 17 00:00:00 2001 From: Jacob Bednarz Date: Tue, 1 Aug 2017 13:51:26 +1000 Subject: [PATCH] Add test coverage for disallowed methods --- csp_collector_test.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 csp_collector_test.go diff --git a/csp_collector_test.go b/csp_collector_test.go new file mode 100644 index 0000000..640c1b4 --- /dev/null +++ b/csp_collector_test.go @@ -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) + } + }) + } + } +}