github.com/google/martian/v3@v3.3.3/status/status_verifier_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 status 16 17 import ( 18 "net/http" 19 "testing" 20 21 "github.com/google/martian/v3" 22 "github.com/google/martian/v3/parse" 23 "github.com/google/martian/v3/proxyutil" 24 "github.com/google/martian/v3/verify" 25 ) 26 27 func TestVerifyResponses(t *testing.T) { 28 v := NewVerifier(301) 29 30 tt := []struct { 31 got int 32 want string 33 }{ 34 {200, "response(http://www.example.com) status code verify failure: got 200, want 301"}, 35 {302, "response(http://www.example.com) status code verify failure: got 302, want 301"}, 36 {400, "response(http://www.example.com) status code verify failure: got 400, want 301"}, 37 } 38 39 for i, tc := range tt { 40 req, err := http.NewRequest("GET", "http://www.example.com", nil) 41 if err != nil { 42 t.Fatalf("%d. http.NewRequest(): got %v, want no error", i, err) 43 } 44 _, remove, err := martian.TestContext(req, nil, nil) 45 if err != nil { 46 t.Fatalf("TestContext(): got %v, want no error", err) 47 } 48 defer remove() 49 50 res := proxyutil.NewResponse(tc.got, nil, req) 51 52 if err := v.ModifyResponse(res); err != nil { 53 t.Fatalf("%d. ModifyResponse(): got %v, want no error", i, err) 54 } 55 } 56 57 merr, ok := v.VerifyResponses().(*martian.MultiError) 58 if !ok { 59 t.Fatal("VerifyResponses(): got nil, want *verify.MultiError") 60 } 61 errs := merr.Errors() 62 if got, want := len(errs), len(tt); got != want { 63 t.Fatalf("len(merr.Errors(): got %d, want %d", got, want) 64 } 65 66 for i, tc := range tt { 67 if got, want := errs[i].Error(), tc.want; got != want { 68 t.Errorf("%d. merr.Errors(): got %q, want %q", i, got, want) 69 } 70 } 71 72 v.ResetResponseVerifications() 73 74 if err := v.VerifyResponses(); err != nil { 75 t.Errorf("v.VerifyResponses(): got %v, want no error", err) 76 } 77 } 78 79 func TestVerifierFromJSON(t *testing.T) { 80 msg := []byte(`{ 81 "status.Verifier": { 82 "scope": ["response"], 83 "statusCode": 400 84 } 85 }`) 86 87 r, err := parse.FromJSON(msg) 88 if err != nil { 89 t.Fatalf("parse.FromJSON(): got %v, want no error", err) 90 } 91 resmod := r.ResponseModifier() 92 if resmod == nil { 93 t.Fatal("resmod: got nil, want not nil") 94 } 95 resv, ok := resmod.(verify.ResponseVerifier) 96 if !ok { 97 t.Fatal("reqmod.(verify.RequestVerifier): got !ok, want ok") 98 } 99 100 req, err := http.NewRequest("GET", "http://www.example.com", nil) 101 if err != nil { 102 t.Fatalf("http.NewRequest(): got %v, want no error", err) 103 } 104 105 _, remove, err := martian.TestContext(req, nil, nil) 106 if err != nil { 107 t.Fatalf("TestContext(): got %v, want no error", err) 108 } 109 defer remove() 110 111 res := proxyutil.NewResponse(200, nil, req) 112 if err := resv.ModifyResponse(res); err != nil { 113 t.Fatalf("ModifyResponse(): got %v, want no error", err) 114 } 115 if err := resv.VerifyResponses(); err == nil { 116 t.Error("VerifyResponses(): got nil, want not nil") 117 } 118 }