github.com/google/martian/v3@v3.3.3/api/forwarder_test.go (about) 1 // Copyright 2016 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 api 16 17 import ( 18 "net/http" 19 "testing" 20 21 "github.com/google/martian/v3" 22 ) 23 24 func TestApiForwarder(t *testing.T) { 25 forwarder := NewForwarder("", 8181) 26 27 req, err := http.NewRequest("GET", "https://martian.proxy/configure", nil) 28 if err != nil { 29 t.Fatalf("NewRequest(): got %v, want no error", err) 30 } 31 32 ctx, remove, err := martian.TestContext(req, nil, nil) 33 if err != nil { 34 t.Fatalf("TestContext(): got %v, want no error", err) 35 } 36 defer remove() 37 38 if err := forwarder.ModifyRequest(req); err != nil { 39 t.Fatalf("ModifyRequest(): got %v, want no error", err) 40 } 41 42 if got, want := req.URL.Scheme, "http"; got != want { 43 t.Errorf("req.URL.Scheme: got %s, want %s", got, want) 44 } 45 if got, want := req.URL.Host, "localhost:8181"; got != want { 46 t.Errorf("req.URL.Host: got %s, want %s", got, want) 47 } 48 49 if !ctx.SkippingLogging() { 50 t.Errorf("ctx.SkippingLogging: got false, want true") 51 } 52 53 if !ctx.IsAPIRequest() { 54 t.Errorf("ctx.IsApiRequest: got false, want true") 55 } 56 } 57 58 func TestApiForwarderWithHost(t *testing.T) { 59 forwarder := NewForwarder("example.com", 8181) 60 61 req, err := http.NewRequest("GET", "https://martian.proxy/configure", nil) 62 if err != nil { 63 t.Fatalf("NewRequest(): got %v, want no error", err) 64 } 65 66 _, remove, err := martian.TestContext(req, nil, nil) 67 if err != nil { 68 t.Fatalf("TestContext(): got %v, want no error", err) 69 } 70 defer remove() 71 72 if err := forwarder.ModifyRequest(req); err != nil { 73 t.Fatalf("ModifyRequest(): got %v, want no error", err) 74 } 75 76 if got, want := req.URL.Host, "example.com:8181"; got != want { 77 t.Errorf("req.URL.Host: got %s, want %s", got, want) 78 } 79 }