proxy_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package proxy
  14. import (
  15. "net/http"
  16. "net/http/httptest"
  17. "testing"
  18. )
  19. func TestReadonlyHandler(t *testing.T) {
  20. fixture := func(w http.ResponseWriter, req *http.Request) {
  21. w.WriteHeader(http.StatusOK)
  22. }
  23. hdlrFunc := readonlyHandlerFunc(http.HandlerFunc(fixture))
  24. tests := []struct {
  25. method string
  26. want int
  27. }{
  28. // GET is only passing method
  29. {"GET", http.StatusOK},
  30. // everything but GET is StatusNotImplemented
  31. {"POST", http.StatusNotImplemented},
  32. {"PUT", http.StatusNotImplemented},
  33. {"PATCH", http.StatusNotImplemented},
  34. {"DELETE", http.StatusNotImplemented},
  35. {"FOO", http.StatusNotImplemented},
  36. }
  37. for i, tt := range tests {
  38. req, _ := http.NewRequest(tt.method, "http://example.com", nil)
  39. rr := httptest.NewRecorder()
  40. hdlrFunc(rr, req)
  41. if tt.want != rr.Code {
  42. t.Errorf("#%d: incorrect HTTP status code: method=%s want=%d got=%d", i, tt.method, tt.want, rr.Code)
  43. }
  44. }
  45. }