|
| 1 | +// Copyright 2021 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 noop provides a martian.RequestResponseModifier that does not |
| 16 | +// modify the request or response. |
| 17 | +package noop |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "net/http" |
| 22 | + |
| 23 | + "github.com/google/martian/v3" |
| 24 | + "github.com/google/martian/v3/log" |
| 25 | + "github.com/google/martian/v3/parse" |
| 26 | +) |
| 27 | + |
| 28 | +func init() { |
| 29 | + parse.Register("noop.Modifier", modifierFromJSON) |
| 30 | +} |
| 31 | + |
| 32 | +type noopModifier struct { |
| 33 | + id string |
| 34 | +} |
| 35 | + |
| 36 | +// Noop returns a modifier that does not change the request or the response. |
| 37 | +func Noop(id string) martian.RequestResponseModifier { |
| 38 | + return &noopModifier{ |
| 39 | + id: id, |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// ModifyRequest logs a debug line. |
| 44 | +func (nm *noopModifier) ModifyRequest(*http.Request) error { |
| 45 | + log.Debugf("noopModifier: %s: no request modification applied", nm.id) |
| 46 | + return nil |
| 47 | +} |
| 48 | + |
| 49 | +// ModifyResponse logs a debug line. |
| 50 | +func (nm *noopModifier) ModifyResponse(*http.Response) error { |
| 51 | + log.Debugf("noopModifier: %s: no response modification applied", nm.id) |
| 52 | + return nil |
| 53 | +} |
| 54 | + |
| 55 | +type modifierJSON struct { |
| 56 | + Name string `json:"name"` |
| 57 | + Scope []parse.ModifierType `json:"scope"` |
| 58 | +} |
| 59 | + |
| 60 | +// modifierFromJSON takes a JSON message as a byte slice and returns |
| 61 | +// a headerModifier and an error. |
| 62 | +// |
| 63 | +// Example JSON configuration message: |
| 64 | +// { |
| 65 | +// "scope": ["request", "result"], |
| 66 | +// "name": "noop-name", |
| 67 | +// } |
| 68 | +func modifierFromJSON(b []byte) (*parse.Result, error) { |
| 69 | + msg := &modifierJSON{} |
| 70 | + if err := json.Unmarshal(b, msg); err != nil { |
| 71 | + return nil, err |
| 72 | + } |
| 73 | + |
| 74 | + modifier := Noop(msg.Name) |
| 75 | + |
| 76 | + return parse.NewResult(modifier, msg.Scope) |
| 77 | +} |
| 78 | + |
0 commit comments