Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

98 righe
2.3 KiB

  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. "net"
  17. "net/http"
  18. "net/http/httptest"
  19. "net/url"
  20. "strings"
  21. "testing"
  22. "github.com/google/martian"
  23. "github.com/google/martian/martiantest"
  24. _ "github.com/google/martian/header"
  25. )
  26. func TestIntegration(t *testing.T) {
  27. ptr := martiantest.NewTransport()
  28. proxy := martian.NewProxy()
  29. defer proxy.Close()
  30. proxy.SetRoundTripper(ptr)
  31. l, err := net.Listen("tcp", "[::]:0")
  32. if err != nil {
  33. t.Fatalf("net.Listen(): got %v, want no error", err)
  34. }
  35. go proxy.Serve(l)
  36. m := NewModifier()
  37. proxy.SetRequestModifier(m)
  38. proxy.SetResponseModifier(m)
  39. mux := http.NewServeMux()
  40. mux.Handle("/", m)
  41. s := httptest.NewServer(mux)
  42. defer s.Close()
  43. body := strings.NewReader(`{
  44. "header.Modifier": {
  45. "scope": ["request", "response"],
  46. "name": "Martian-Test",
  47. "value": "true"
  48. }
  49. }`)
  50. res, err := http.Post(s.URL, "application/json", body)
  51. if err != nil {
  52. t.Fatalf("http.Post(%s): got %v, want no error", s.URL, err)
  53. }
  54. res.Body.Close()
  55. if got, want := res.StatusCode, 200; got != want {
  56. t.Fatalf("res.StatusCode: got %d, want %d", got, want)
  57. }
  58. tr := &http.Transport{
  59. Proxy: http.ProxyURL(&url.URL{
  60. Scheme: "http",
  61. Host: l.Addr().String(),
  62. }),
  63. }
  64. defer tr.CloseIdleConnections()
  65. req, err := http.NewRequest("GET", "http://example.com", nil)
  66. if err != nil {
  67. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  68. }
  69. req.Header.Set("Connection", "close")
  70. res, err = tr.RoundTrip(req)
  71. if err != nil {
  72. t.Fatalf("transport.RoundTrip(%q): got %v, want no error", req.URL, err)
  73. }
  74. res.Body.Close()
  75. if got, want := res.Header.Get("Martian-Test"), "true"; got != want {
  76. t.Errorf("res.Header.Get(%q): got %q, want %q", "Martian-Test", got, want)
  77. }
  78. }