fake_handler_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  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 testing
  14. import (
  15. "bytes"
  16. "net/http"
  17. "net/http/httptest"
  18. "testing"
  19. )
  20. func TestFakeHandlerPath(t *testing.T) {
  21. handler := FakeHandler{StatusCode: http.StatusOK}
  22. server := httptest.NewServer(&handler)
  23. defer server.Close()
  24. method := "GET"
  25. path := "/foo/bar"
  26. body := "somebody"
  27. req, err := http.NewRequest(method, server.URL+path, bytes.NewBufferString(body))
  28. if err != nil {
  29. t.Errorf("unexpected error: %v", err)
  30. }
  31. client := http.Client{}
  32. _, err = client.Do(req)
  33. if err != nil {
  34. t.Errorf("unexpected error: %v", err)
  35. }
  36. handler.ValidateRequest(t, path, method, &body)
  37. }
  38. func TestFakeHandlerPathNoBody(t *testing.T) {
  39. handler := FakeHandler{StatusCode: http.StatusOK}
  40. server := httptest.NewServer(&handler)
  41. defer server.Close()
  42. method := "GET"
  43. path := "/foo/bar"
  44. req, err := http.NewRequest(method, server.URL+path, nil)
  45. if err != nil {
  46. t.Errorf("unexpected error: %v", err)
  47. }
  48. client := http.Client{}
  49. _, err = client.Do(req)
  50. if err != nil {
  51. t.Errorf("unexpected error: %v", err)
  52. }
  53. handler.ValidateRequest(t, path, method, nil)
  54. }
  55. type fakeError struct {
  56. errors []string
  57. }
  58. func (f *fakeError) Errorf(format string, args ...interface{}) {
  59. f.errors = append(f.errors, format)
  60. }
  61. func (f *fakeError) Logf(format string, args ...interface{}) {}
  62. func TestFakeHandlerWrongPath(t *testing.T) {
  63. handler := FakeHandler{StatusCode: http.StatusOK}
  64. server := httptest.NewServer(&handler)
  65. defer server.Close()
  66. method := "GET"
  67. path := "/foo/bar"
  68. fakeT := fakeError{}
  69. req, err := http.NewRequest(method, server.URL+"/foo/baz", nil)
  70. if err != nil {
  71. t.Errorf("unexpected error: %v", err)
  72. }
  73. client := http.Client{}
  74. _, err = client.Do(req)
  75. if err != nil {
  76. t.Errorf("unexpected error: %v", err)
  77. }
  78. handler.ValidateRequest(&fakeT, path, method, nil)
  79. if len(fakeT.errors) != 1 {
  80. t.Errorf("Unexpected error set: %#v", fakeT.errors)
  81. }
  82. }
  83. func TestFakeHandlerWrongMethod(t *testing.T) {
  84. handler := FakeHandler{StatusCode: http.StatusOK}
  85. server := httptest.NewServer(&handler)
  86. defer server.Close()
  87. method := "GET"
  88. path := "/foo/bar"
  89. fakeT := fakeError{}
  90. req, err := http.NewRequest("PUT", server.URL+path, nil)
  91. if err != nil {
  92. t.Errorf("unexpected error: %v", err)
  93. }
  94. client := http.Client{}
  95. _, err = client.Do(req)
  96. if err != nil {
  97. t.Errorf("unexpected error: %v", err)
  98. }
  99. handler.ValidateRequest(&fakeT, path, method, nil)
  100. if len(fakeT.errors) != 1 {
  101. t.Errorf("Unexpected error set: %#v", fakeT.errors)
  102. }
  103. }
  104. func TestFakeHandlerWrongBody(t *testing.T) {
  105. handler := FakeHandler{StatusCode: http.StatusOK}
  106. server := httptest.NewServer(&handler)
  107. defer server.Close()
  108. method := "GET"
  109. path := "/foo/bar"
  110. body := "somebody"
  111. fakeT := fakeError{}
  112. req, err := http.NewRequest(method, server.URL+path, bytes.NewBufferString(body))
  113. if err != nil {
  114. t.Errorf("unexpected error: %v", err)
  115. }
  116. client := http.Client{}
  117. _, err = client.Do(req)
  118. if err != nil {
  119. t.Errorf("unexpected error: %v", err)
  120. }
  121. otherbody := "otherbody"
  122. handler.ValidateRequest(&fakeT, path, method, &otherbody)
  123. if len(fakeT.errors) != 1 {
  124. t.Errorf("Unexpected error set: %#v", fakeT.errors)
  125. }
  126. }
  127. func TestFakeHandlerNilBody(t *testing.T) {
  128. handler := FakeHandler{StatusCode: http.StatusOK}
  129. server := httptest.NewServer(&handler)
  130. defer server.Close()
  131. method := "GET"
  132. path := "/foo/bar"
  133. body := "somebody"
  134. fakeT := fakeError{}
  135. req, err := http.NewRequest(method, server.URL+path, nil)
  136. if err != nil {
  137. t.Errorf("unexpected error: %v", err)
  138. }
  139. client := http.Client{}
  140. _, err = client.Do(req)
  141. if err != nil {
  142. t.Errorf("unexpected error: %v", err)
  143. }
  144. handler.ValidateRequest(&fakeT, path, method, &body)
  145. if len(fakeT.errors) != 1 {
  146. t.Errorf("Unexpected error set: %#v", fakeT.errors)
  147. }
  148. }