github.com/google/martian/v3@v3.3.3/martianlog/logger_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 martianlog 16 17 import ( 18 "bytes" 19 "compress/gzip" 20 "fmt" 21 "net/http" 22 "strings" 23 "testing" 24 25 "github.com/google/martian/v3" 26 "github.com/google/martian/v3/parse" 27 "github.com/google/martian/v3/proxyutil" 28 ) 29 30 func ExampleLogger() { 31 l := NewLogger() 32 l.SetLogFunc(func(line string) { 33 // Remove \r to make it easier to test with examples. 34 fmt.Print(strings.Replace(line, "\r", "", -1)) 35 }) 36 l.SetDecode(true) 37 38 buf := new(bytes.Buffer) 39 gw := gzip.NewWriter(buf) 40 gw.Write([]byte("request content")) 41 gw.Close() 42 43 req, err := http.NewRequest("GET", "http://example.com/path?querystring", buf) 44 if err != nil { 45 fmt.Println(err) 46 return 47 } 48 req.TransferEncoding = []string{"chunked"} 49 req.Header.Set("Content-Encoding", "gzip") 50 51 _, remove, err := martian.TestContext(req, nil, nil) 52 if err != nil { 53 fmt.Println(err) 54 return 55 } 56 defer remove() 57 58 if err := l.ModifyRequest(req); err != nil { 59 fmt.Println(err) 60 return 61 } 62 63 res := proxyutil.NewResponse(200, strings.NewReader("response content"), req) 64 res.ContentLength = 16 65 res.Header.Set("Date", "Tue, 15 Nov 1994 08:12:31 GMT") 66 res.Header.Set("Other-Header", "values") 67 68 if err := l.ModifyResponse(res); err != nil { 69 fmt.Println(err) 70 return 71 } 72 // Output: 73 // -------------------------------------------------------------------------------- 74 // Request to http://example.com/path?querystring 75 // -------------------------------------------------------------------------------- 76 // GET http://example.com/path?querystring HTTP/1.1 77 // Host: example.com 78 // Transfer-Encoding: chunked 79 // Content-Encoding: gzip 80 // 81 // request content 82 // 83 // -------------------------------------------------------------------------------- 84 // 85 // -------------------------------------------------------------------------------- 86 // Response from http://example.com/path?querystring 87 // -------------------------------------------------------------------------------- 88 // HTTP/1.1 200 OK 89 // Content-Length: 16 90 // Date: Tue, 15 Nov 1994 08:12:31 GMT 91 // Other-Header: values 92 // 93 // response content 94 // -------------------------------------------------------------------------------- 95 } 96 97 func TestLoggerFromJSON(t *testing.T) { 98 msg := []byte(`{ 99 "log.Logger": { 100 "scope": ["request", "response"], 101 "headersOnly": true, 102 "decode": true 103 } 104 }`) 105 106 r, err := parse.FromJSON(msg) 107 if err != nil { 108 t.Fatalf("parse.FromJSON(): got %v, want no error", err) 109 } 110 111 reqmod := r.RequestModifier() 112 if reqmod == nil { 113 t.Fatal("r.RequestModifier(): got nil, want not nil") 114 } 115 if _, ok := reqmod.(*Logger); !ok { 116 t.Error("reqmod.(*Logger): got !ok, want ok") 117 } 118 119 resmod := r.ResponseModifier() 120 if resmod == nil { 121 t.Fatal("r.ResponseModifier(): got nil, want not nil") 122 } 123 124 l, ok := resmod.(*Logger) 125 if !ok { 126 t.Error("resmod.(*Logger); got !ok, want ok") 127 } 128 129 if !l.headersOnly { 130 t.Error("l.headersOnly: got false, want true") 131 } 132 133 if !l.decode { 134 t.Error("l.decode: got false, want true") 135 } 136 }