proxy_test.go 915 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package proxy
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "testing"
  6. )
  7. func TestReadonlyHandler(t *testing.T) {
  8. fixture := func(w http.ResponseWriter, req *http.Request) {
  9. w.WriteHeader(http.StatusOK)
  10. }
  11. hdlrFunc := readonlyHandlerFunc(http.HandlerFunc(fixture))
  12. tests := []struct {
  13. method string
  14. want int
  15. }{
  16. // GET is only passing method
  17. {"GET", http.StatusOK},
  18. // everything but GET is StatusNotImplemented
  19. {"POST", http.StatusNotImplemented},
  20. {"PUT", http.StatusNotImplemented},
  21. {"PATCH", http.StatusNotImplemented},
  22. {"DELETE", http.StatusNotImplemented},
  23. {"FOO", http.StatusNotImplemented},
  24. }
  25. for i, tt := range tests {
  26. req, _ := http.NewRequest(tt.method, "http://example.com", nil)
  27. rr := httptest.NewRecorder()
  28. hdlrFunc(rr, req)
  29. if tt.want != rr.Code {
  30. t.Errorf("#%d: incorrect HTTP status code: method=%s want=%d got=%d", i, tt.method, tt.want, rr.Code)
  31. }
  32. }
  33. }