github.com/google/martian/v3@v3.3.3/martiantest/martiantest_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 martiantest 16 17 import ( 18 "errors" 19 "net/http" 20 "testing" 21 22 "github.com/google/martian/v3/proxyutil" 23 ) 24 25 func TestModifier(t *testing.T) { 26 var reqrun bool 27 var resrun bool 28 29 moderr := errors.New("modifier error") 30 tm := NewModifier() 31 tm.RequestError(moderr) 32 tm.RequestFunc(func(*http.Request) { 33 reqrun = true 34 }) 35 36 tm.ResponseError(moderr) 37 tm.ResponseFunc(func(*http.Response) { 38 resrun = true 39 }) 40 41 req, err := http.NewRequest("GET", "http://example.com", nil) 42 if err != nil { 43 t.Fatalf("http.NewRequest(): got %v, want no error", err) 44 } 45 46 if err := tm.ModifyRequest(req); err != moderr { 47 t.Fatalf("tm.ModifyRequest(): got %v, want %v", err, moderr) 48 } 49 if !tm.RequestModified() { 50 t.Errorf("tm.RequestModified(): got false, want true") 51 } 52 if tm.RequestCount() != 1 { 53 t.Errorf("tm.RequestCount(): got %d, want %d", tm.RequestCount(), 1) 54 } 55 if !reqrun { 56 t.Error("reqrun: got false, want true") 57 } 58 59 res := proxyutil.NewResponse(200, nil, req) 60 61 if err := tm.ModifyResponse(res); err != moderr { 62 t.Fatalf("tm.ModifyResponse(): got %v, want %v", err, moderr) 63 } 64 if !tm.ResponseModified() { 65 t.Errorf("tm.ResponseModified(): got false, want true") 66 } 67 if tm.ResponseCount() != 1 { 68 t.Errorf("tm.ResponseCount(): got %d, want %d", tm.ResponseCount(), 1) 69 } 70 if !resrun { 71 t.Error("resrun: got false, want true") 72 } 73 74 tm.Reset() 75 76 if tm.RequestModified() { 77 t.Error("tm.RequestModified(): got true, want false") 78 } 79 if tm.ResponseModified() { 80 t.Error("tm.ResponseModified(): got true, want false") 81 } 82 }