martianhttp_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // Copyright 2015 Google Inc. All rights reserved.
  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 martianhttp
  15. import (
  16. "bytes"
  17. "encoding/json"
  18. "fmt"
  19. "net/http"
  20. "net/http/httptest"
  21. "testing"
  22. "github.com/google/martian/martiantest"
  23. "github.com/google/martian/proxyutil"
  24. "github.com/google/martian/verify"
  25. _ "github.com/google/martian/header"
  26. )
  27. func TestNoModifiers(t *testing.T) {
  28. m := NewModifier()
  29. m.SetRequestModifier(nil)
  30. m.SetResponseModifier(nil)
  31. req, err := http.NewRequest("GET", "http://example.com", nil)
  32. if err != nil {
  33. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  34. }
  35. if err := m.ModifyRequest(req); err != nil {
  36. t.Errorf("ModifyRequest(): got %v, want no error", err)
  37. }
  38. res := proxyutil.NewResponse(200, nil, req)
  39. if err := m.ModifyResponse(res); err != nil {
  40. t.Errorf("ModifyResponse(): got %v, want no error", err)
  41. }
  42. }
  43. func TestModifyRequest(t *testing.T) {
  44. m := NewModifier()
  45. tm := martiantest.NewModifier()
  46. m.SetRequestModifier(tm)
  47. req, err := http.NewRequest("GET", "http://example.com", nil)
  48. if err != nil {
  49. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  50. }
  51. if err := m.ModifyRequest(req); err != nil {
  52. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  53. }
  54. if !tm.RequestModified() {
  55. t.Error("tm.RequestModified(): got false, want true")
  56. }
  57. m.SetRequestModifier(nil)
  58. if err := m.ModifyRequest(req); err != nil {
  59. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  60. }
  61. }
  62. func TestModifyResponse(t *testing.T) {
  63. m := NewModifier()
  64. tm := martiantest.NewModifier()
  65. m.SetResponseModifier(tm)
  66. res := proxyutil.NewResponse(200, nil, nil)
  67. if err := m.ModifyResponse(res); err != nil {
  68. t.Fatalf("ModifyResponse(): got %v, want no error", err)
  69. }
  70. if !tm.ResponseModified() {
  71. t.Error("tm.ResponseModified(): got false, want true")
  72. }
  73. m.SetResponseModifier(nil)
  74. if err := m.ModifyResponse(res); err != nil {
  75. t.Fatalf("ModifyResponse(): got %v, want no error", err)
  76. }
  77. }
  78. func TestVerifyRequests(t *testing.T) {
  79. m := NewModifier()
  80. if err := m.VerifyRequests(); err != nil {
  81. t.Errorf("VerifyRequests(): got %v, want no error", err)
  82. }
  83. verr := fmt.Errorf("request verification failure")
  84. m.SetRequestModifier(&verify.TestVerifier{
  85. RequestError: verr,
  86. })
  87. if err := m.VerifyRequests(); err != verr {
  88. t.Errorf("VerifyRequests(): got %v, want %v", err, verr)
  89. }
  90. m.ResetRequestVerifications()
  91. if err := m.VerifyRequests(); err != nil {
  92. t.Errorf("m.VerifyRequests(): got %v, want no error", err)
  93. }
  94. }
  95. func TestVerifyResponses(t *testing.T) {
  96. m := NewModifier()
  97. if err := m.VerifyResponses(); err != nil {
  98. t.Errorf("VerifyResponses(): got %v, want no error", err)
  99. }
  100. verr := fmt.Errorf("response verification failure")
  101. m.SetResponseModifier(&verify.TestVerifier{
  102. ResponseError: verr,
  103. })
  104. if err := m.VerifyResponses(); err != verr {
  105. t.Errorf("VerifyResponses(): got %v, want %v", err, verr)
  106. }
  107. m.ResetResponseVerifications()
  108. if err := m.VerifyResponses(); err != nil {
  109. t.Errorf("m.VerifyResponses(): got %v, want no error", err)
  110. }
  111. }
  112. func TestServeHTTPInvalidMethod(t *testing.T) {
  113. m := NewModifier()
  114. req, err := http.NewRequest("PATCH", "/configure", nil)
  115. if err != nil {
  116. t.Fatalf("http.NewRequest(%q, ...): got %v, want no error", "GET", err)
  117. }
  118. rw := httptest.NewRecorder()
  119. m.ServeHTTP(rw, req)
  120. if got, want := rw.Code, 405; got != want {
  121. t.Errorf("rw.Code: got %d, want %d", got, want)
  122. }
  123. if got, want := rw.Header().Get("Allow"), "GET, POST"; got != want {
  124. t.Errorf("rw.Header().Get(%q): got %q, want %q", "Allow", got, want)
  125. }
  126. }
  127. func TestServeHTTPInvalidJSON(t *testing.T) {
  128. m := NewModifier()
  129. req, err := http.NewRequest("POST", "/configure", bytes.NewReader([]byte("not-json")))
  130. if err != nil {
  131. t.Fatalf("http.NewRequest(%q, %q, ...): got %v, want no error", "POST", "/configure", err)
  132. }
  133. rw := httptest.NewRecorder()
  134. m.ServeHTTP(rw, req)
  135. if got, want := rw.Code, 400; got != want {
  136. t.Errorf("rw.Code: got %d, want %d", got, want)
  137. }
  138. }
  139. func TestServeHTTP(t *testing.T) {
  140. m := NewModifier()
  141. body := []byte(`{
  142. "header.Modifier": {
  143. "scope": ["request", "response"],
  144. "name": "Martian-Test",
  145. "value": "true"
  146. }
  147. }`)
  148. req, err := http.NewRequest("POST", "/configure", bytes.NewReader(body))
  149. if err != nil {
  150. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  151. }
  152. req.Header.Set("Content-Type", "application/json")
  153. rw := httptest.NewRecorder()
  154. m.ServeHTTP(rw, req)
  155. if got, want := rw.Code, 200; got != want {
  156. t.Errorf("rw.Code: got %d, want %d", got, want)
  157. }
  158. req, err = http.NewRequest("GET", "http://example.com", nil)
  159. if err != nil {
  160. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  161. }
  162. if err := m.ModifyRequest(req); err != nil {
  163. t.Fatalf("m.ModifyRequest(): got %v, want no error", err)
  164. }
  165. if got, want := req.Header.Get("Martian-Test"), "true"; got != want {
  166. t.Errorf("req.Header.Get(%q): got %q, want %q", "Martian-Test", got, want)
  167. }
  168. res := proxyutil.NewResponse(200, nil, req)
  169. if err := m.ModifyResponse(res); err != nil {
  170. t.Fatalf("m.ModifyResponse(): got %v, want no error", err)
  171. }
  172. if got, want := res.Header.Get("Martian-Test"), "true"; got != want {
  173. t.Errorf("res.Header.Get(%q): got %q, want %q", "Martian-Test", got, want)
  174. }
  175. req, err = http.NewRequest("GET", "/configure", nil)
  176. if err != nil {
  177. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  178. }
  179. rw = httptest.NewRecorder()
  180. m.ServeHTTP(rw, req)
  181. if got, want := rw.Code, 200; got != want {
  182. t.Errorf("rw.Code: got %d, want %d", got, want)
  183. }
  184. got := new(bytes.Buffer)
  185. want := new(bytes.Buffer)
  186. if err := json.Compact(got, body); err != nil {
  187. t.Fatalf("json.Compact(body): got %v, want no error", err)
  188. }
  189. if err := json.Compact(want, rw.Body.Bytes()); err != nil {
  190. t.Fatalf("json.Compact(rw.Body): got %v, want no error", err)
  191. }
  192. if !bytes.Equal(got.Bytes(), want.Bytes()) {
  193. t.Errorf("rw.Body: got %q, want %q", got.Bytes(), want.Bytes())
  194. }
  195. }