github.com/google/martian/v3@v3.3.3/status/status_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 status 16 17 import ( 18 "fmt" 19 "net/http" 20 "testing" 21 22 "github.com/google/martian/v3/parse" 23 "github.com/google/martian/v3/proxyutil" 24 ) 25 26 func TestFromJSON(t *testing.T) { 27 msg := []byte(`{ 28 "status.Modifier": { 29 "scope": ["response"], 30 "statusCode": 400 31 } 32 }`) 33 34 r, err := parse.FromJSON(msg) 35 if err != nil { 36 t.Fatalf("parse.FromJSON(): got %v, want no error", err) 37 } 38 resmod := r.ResponseModifier() 39 if resmod == nil { 40 t.Fatal("resmod: got nil, want not nil") 41 } 42 43 res := proxyutil.NewResponse(200, nil, nil) 44 if err := resmod.ModifyResponse(res); err != nil { 45 t.Fatalf("ModifyResponse(): got %v, want no error", err) 46 } 47 if got, want := res.StatusCode, 400; got != want { 48 t.Errorf("res.StatusCode: got %d, want %d", got, want) 49 } 50 } 51 52 func TestStatusModifierOnResponse(t *testing.T) { 53 for i, status := range []int{ 54 http.StatusForbidden, 55 http.StatusOK, 56 http.StatusTemporaryRedirect, 57 } { 58 req, err := http.NewRequest("GET", "/", nil) 59 if err != nil { 60 t.Fatalf("NewRequest(): got %v, want no error", err) 61 } 62 63 res := proxyutil.NewResponse(200, nil, req) 64 65 mod := NewModifier(status) 66 67 if err := mod.ModifyResponse(res); err != nil { 68 t.Fatalf("%d. ModifyResponse(): got %v, want no error", i, err) 69 } 70 71 if got, want := res.StatusCode, status; got != want { 72 t.Errorf("%d. res.StatusCode: got %v, want %v", i, got, want) 73 } 74 if got, want := res.Status, fmt.Sprintf("%d %s", res.StatusCode, http.StatusText(status)); got != want { 75 t.Errorf("%d. res.Status: got %q, want %q", i, got, want) 76 } 77 } 78 }