You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

157 rivejä
4.0 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 martianurl
  15. import (
  16. "net/http"
  17. "net/http/httptest"
  18. "net/url"
  19. "testing"
  20. "github.com/google/martian/parse"
  21. )
  22. func TestNewModifier(t *testing.T) {
  23. tt := []struct {
  24. want string
  25. url *url.URL
  26. }{
  27. {
  28. want: "https://www.example.com",
  29. url: &url.URL{Scheme: "https"},
  30. },
  31. {
  32. want: "http://www.martian.local",
  33. url: &url.URL{Host: "www.martian.local"},
  34. },
  35. {
  36. want: "http://www.example.com/test",
  37. url: &url.URL{Path: "/test"},
  38. },
  39. {
  40. want: "http://www.example.com?test=true",
  41. url: &url.URL{RawQuery: "test=true"},
  42. },
  43. {
  44. want: "http://www.example.com#test",
  45. url: &url.URL{Fragment: "test"},
  46. },
  47. {
  48. want: "https://martian.local/test?test=true#test",
  49. url: &url.URL{
  50. Scheme: "https",
  51. Host: "martian.local",
  52. Path: "/test",
  53. RawQuery: "test=true",
  54. Fragment: "test",
  55. },
  56. },
  57. }
  58. for i, tc := range tt {
  59. req, err := http.NewRequest("GET", "http://www.example.com", nil)
  60. if err != nil {
  61. t.Fatalf("%d. NewRequest(): got %v, want no error", i, err)
  62. }
  63. mod := NewModifier(tc.url)
  64. if err := mod.ModifyRequest(req); err != nil {
  65. t.Fatalf("%d. ModifyRequest(): got %q, want no error", i, err)
  66. }
  67. if got := req.URL.String(); got != tc.want {
  68. t.Errorf("%d. req.URL: got %q, want %q", i, got, tc.want)
  69. }
  70. }
  71. }
  72. func TestIntegration(t *testing.T) {
  73. server := httptest.NewServer(http.HandlerFunc(
  74. func(w http.ResponseWriter, r *http.Request) {
  75. r.URL.Scheme = "http"
  76. r.URL.Host = r.Host
  77. w.Header().Set("Martian-URL", r.URL.String())
  78. }))
  79. defer server.Close()
  80. u := &url.URL{
  81. Scheme: "http",
  82. Host: server.Listener.Addr().String(),
  83. }
  84. m := NewModifier(u)
  85. req, err := http.NewRequest("GET", "https://example.com/test", nil)
  86. if err != nil {
  87. t.Fatalf("http.NewRequest(%q, %q, nil): got %v, want no error", "GET", "http://example.com/test", err)
  88. }
  89. if err := m.ModifyRequest(req); err != nil {
  90. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  91. }
  92. res, err := http.DefaultClient.Do(req)
  93. if err != nil {
  94. t.Fatalf("http.DefaultClient.Do(): got %v, want no error", err)
  95. }
  96. want := "http://example.com/test"
  97. if got := res.Header.Get("Martian-URL"); got != want {
  98. t.Errorf("res.Header.Get(%q): got %q, want %q", "Martian-URL", got, want)
  99. }
  100. }
  101. func TestModifierFromJSON(t *testing.T) {
  102. msg := []byte(`{
  103. "url.Modifier": {
  104. "scope": ["request"],
  105. "scheme": "https",
  106. "host": "www.martian.proxy",
  107. "path": "/testing",
  108. "query": "test=true"
  109. }
  110. }`)
  111. r, err := parse.FromJSON(msg)
  112. if err != nil {
  113. t.Fatalf("parse.FromJSON(): got %v, want no error", err)
  114. }
  115. reqmod := r.RequestModifier()
  116. if reqmod == nil {
  117. t.Fatal("reqmod: got nil, want not nil")
  118. }
  119. req, err := http.NewRequest("GET", "http://example.com", nil)
  120. if err != nil {
  121. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  122. }
  123. if err := reqmod.ModifyRequest(req); err != nil {
  124. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  125. }
  126. if got, want := req.URL.Scheme, "https"; got != want {
  127. t.Errorf("req.URL.Scheme: got %q, want %q", got, want)
  128. }
  129. if got, want := req.URL.Host, "www.martian.proxy"; got != want {
  130. t.Errorf("req.URL.Host: got %q, want %q", got, want)
  131. }
  132. if got, want := req.URL.Path, "/testing"; got != want {
  133. t.Errorf("req.URL.Path: got %q, want %q", got, want)
  134. }
  135. if got, want := req.URL.RawQuery, "test=true"; got != want {
  136. t.Errorf("req.URL.RawQuery: got %q, want %q", got, want)
  137. }
  138. }