proxy_test.go 1.5 KB

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