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