-
Notifications
You must be signed in to change notification settings - Fork 26
/
main_test.go
43 lines (35 loc) · 1.17 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package main
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/jacobbednarz/go-csp-collector/internal/handler"
"github.com/jacobbednarz/go-csp-collector/internal/utils"
"github.com/sirupsen/logrus"
)
var cspViolationReportHandler = &handler.CSPViolationReportHandler{
BlockedURIs: utils.DefaultIgnoredBlockedURIs,
TruncateQueryStringFragment: false,
Logger: logrus.New(),
}
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, url, nil)
if err != nil {
t.Fatalf("failed to create request: %v", err)
}
recorder := httptest.NewRecorder()
cspViolationReportHandler.ServeHTTP(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.StatusCode)
}
})
}
}
}