proxy_test.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2015 The etcd Authors
  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 httpproxy
  15. import (
  16. "io/ioutil"
  17. "net/http"
  18. "net/http/httptest"
  19. "net/url"
  20. "testing"
  21. "time"
  22. )
  23. func TestReadonlyHandler(t *testing.T) {
  24. fixture := func(w http.ResponseWriter, req *http.Request) {
  25. w.WriteHeader(http.StatusOK)
  26. }
  27. hdlrFunc := readonlyHandlerFunc(http.HandlerFunc(fixture))
  28. tests := []struct {
  29. method string
  30. want int
  31. }{
  32. // GET is only passing method
  33. {"GET", http.StatusOK},
  34. // everything but GET is StatusNotImplemented
  35. {"POST", http.StatusNotImplemented},
  36. {"PUT", http.StatusNotImplemented},
  37. {"PATCH", http.StatusNotImplemented},
  38. {"DELETE", http.StatusNotImplemented},
  39. {"FOO", http.StatusNotImplemented},
  40. }
  41. for i, tt := range tests {
  42. req, _ := http.NewRequest(tt.method, "http://example.com", nil)
  43. rr := httptest.NewRecorder()
  44. hdlrFunc(rr, req)
  45. if tt.want != rr.Code {
  46. t.Errorf("#%d: incorrect HTTP status code: method=%s want=%d got=%d", i, tt.method, tt.want, rr.Code)
  47. }
  48. }
  49. }
  50. func TestConfigHandlerGET(t *testing.T) {
  51. var err error
  52. us := make([]*url.URL, 3)
  53. us[0], err = url.Parse("http://example1.com")
  54. if err != nil {
  55. t.Fatal(err)
  56. }
  57. us[1], err = url.Parse("http://example2.com")
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. us[2], err = url.Parse("http://example3.com")
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. rp := reverseProxy{
  66. director: &director{
  67. ep: []*endpoint{
  68. newEndpoint(*us[0], 1*time.Second),
  69. newEndpoint(*us[1], 1*time.Second),
  70. newEndpoint(*us[2], 1*time.Second),
  71. },
  72. },
  73. }
  74. req, _ := http.NewRequest("GET", "http://example.com//v2/config/local/proxy", nil)
  75. rr := httptest.NewRecorder()
  76. rp.configHandler(rr, req)
  77. wbody := "{\"endpoints\":[\"http://example1.com\",\"http://example2.com\",\"http://example3.com\"]}\n"
  78. body, err := ioutil.ReadAll(rr.Body)
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. if string(body) != wbody {
  83. t.Errorf("body = %s, want %s", string(body), wbody)
  84. }
  85. }