github.com/zchee/zap-cloudlogging@v0.0.0-20220819025602-19b026d3900e/http_test.go (about) 1 // Copyright 2022 The zap-cloudlogging Authors 2 // SPDX-License-Identifier: BSD-3-Clause 3 4 package zapcloudlogging 5 6 import ( 7 "io" 8 "net/http" 9 "net/http/httptest" 10 "net/url" 11 "strings" 12 "testing" 13 14 "github.com/google/go-cmp/cmp" 15 logtypepb "google.golang.org/genproto/googleapis/logging/type" 16 "google.golang.org/protobuf/testing/protocmp" 17 ) 18 19 func TestHTTPRequestField(t *testing.T) { 20 t.Parallel() 21 22 tests := map[string]struct { 23 r *http.Request 24 res *http.Response 25 want *HTTPPayload 26 }{ 27 "Empty": { 28 r: nil, 29 res: nil, 30 want: &HTTPPayload{ 31 HttpRequest: &logtypepb.HttpRequest{}, 32 }, 33 }, 34 "RequestMethod": { 35 r: &http.Request{ 36 Method: "GET", 37 }, 38 res: nil, 39 want: &HTTPPayload{ 40 HttpRequest: &logtypepb.HttpRequest{ 41 RequestMethod: "GET", 42 }, 43 }, 44 }, 45 "Status": { 46 r: nil, 47 res: &http.Response{StatusCode: 404}, 48 want: &HTTPPayload{ 49 HttpRequest: &logtypepb.HttpRequest{ 50 Status: 404, 51 }, 52 }, 53 }, 54 "UserAgent": { 55 r: &http.Request{ 56 Header: http.Header{ 57 "User-Agent": []string{"hello world"}, 58 }, 59 }, 60 res: nil, 61 want: &HTTPPayload{ 62 HttpRequest: &logtypepb.HttpRequest{ 63 UserAgent: "hello world", 64 }, 65 }, 66 }, 67 "RemoteIP": { 68 r: &http.Request{ 69 RemoteAddr: "127.0.0.1", 70 }, 71 res: nil, 72 want: &HTTPPayload{ 73 HttpRequest: &logtypepb.HttpRequest{ 74 RemoteIp: "127.0.0.1", 75 }, 76 }, 77 }, 78 "Referrer": { 79 r: &http.Request{ 80 Header: http.Header{ 81 "Referer": []string{"hello universe"}, 82 }, 83 }, 84 res: nil, 85 want: &HTTPPayload{ 86 HttpRequest: &logtypepb.HttpRequest{ 87 Referer: "hello universe", 88 }, 89 }, 90 }, 91 "Protocol": { 92 r: &http.Request{ 93 Proto: "HTTP/1.1", 94 }, 95 res: nil, 96 want: &HTTPPayload{ 97 HttpRequest: &logtypepb.HttpRequest{ 98 Protocol: "HTTP/1.1", 99 }, 100 }, 101 }, 102 "RequestURL": { 103 r: &http.Request{ 104 URL: &url.URL{ 105 Host: "example.com", 106 Scheme: "https", 107 }, 108 }, 109 res: nil, 110 want: &HTTPPayload{ 111 HttpRequest: &logtypepb.HttpRequest{ 112 RequestUrl: "https://example.com", 113 }, 114 }, 115 }, 116 "RequestSize": { 117 r: &http.Request{ 118 Body: io.NopCloser(strings.NewReader("12345")), 119 }, 120 res: nil, 121 want: &HTTPPayload{ 122 HttpRequest: &logtypepb.HttpRequest{ 123 RequestSize: 5, 124 }, 125 }, 126 }, 127 "ResponseSize": { 128 r: nil, 129 res: &http.Response{ 130 Body: io.NopCloser(strings.NewReader("12345")), 131 }, 132 want: &HTTPPayload{ 133 HttpRequest: &logtypepb.HttpRequest{ 134 ResponseSize: 5, 135 }, 136 }, 137 }, 138 "SimpleRequest": { 139 r: httptest.NewRequest("POST", "/", strings.NewReader("12345")), 140 res: nil, 141 want: &HTTPPayload{ 142 HttpRequest: &logtypepb.HttpRequest{ 143 RequestSize: 5, 144 RequestMethod: "POST", 145 RemoteIp: "192.0.2.1:1234", 146 Protocol: "HTTP/1.1", 147 RequestUrl: "/", 148 }, 149 }, 150 }, 151 "SimpleResponse": { 152 r: nil, 153 res: &http.Response{ 154 Body: io.NopCloser(strings.NewReader("12345")), 155 StatusCode: 404, 156 }, 157 want: &HTTPPayload{ 158 HttpRequest: &logtypepb.HttpRequest{ 159 ResponseSize: 5, 160 Status: 404, 161 }, 162 }, 163 }, 164 "RequestAndResponse": { 165 r: &http.Request{ 166 Method: "POST", 167 Proto: "HTTP/1.1", 168 }, 169 res: &http.Response{StatusCode: 200}, 170 want: &HTTPPayload{ 171 HttpRequest: &logtypepb.HttpRequest{ 172 RequestMethod: "POST", 173 Protocol: "HTTP/1.1", 174 Status: 200, 175 }, 176 }, 177 }, 178 } 179 for name, tt := range tests { 180 tt := tt 181 t.Run(name, func(t *testing.T) { 182 t.Parallel() 183 184 if diff := cmp.Diff(tt.want, NewHTTPRequest(tt.r, tt.res), 185 protocmp.Transform(), 186 ); diff != "" { 187 t.Fatalf("(-want, +got)\n%s\n", diff) 188 } 189 }) 190 } 191 }