forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_logger_file_writer_test.go
382 lines (309 loc) · 12.6 KB
/
request_logger_file_writer_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package ui_test
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
. "code.cloudfoundry.org/cli/util/ui"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gbytes"
)
var _ = Describe("Request Logger File Writer", func() {
var (
ui *UI
display *RequestLoggerFileWriter
tmpdir string
logFile1 string
logFile2 string
)
BeforeEach(func() {
ui = NewTestUI(NewBuffer(), NewBuffer(), NewBuffer())
})
Describe("Valid file paths", func() {
BeforeEach(func() {
var err error
tmpdir, err = ioutil.TempDir("", "request_logger")
Expect(err).ToNot(HaveOccurred())
logFile1 = filepath.Join(tmpdir, "tmp_sub_dir", "tmpfile1")
logFile2 = filepath.Join(tmpdir, "tmp", "sub", "dir", ".", "tmpfile2")
display = ui.RequestLoggerFileWriter([]string{logFile1, logFile2})
Expect(display.Start()).ToNot(HaveOccurred())
})
AfterEach(func() {
Expect(os.RemoveAll(tmpdir)).NotTo(HaveOccurred())
})
Describe("DisplayBody", func() {
It("writes the redacted value", func() {
err := display.DisplayBody([]byte("this is a body"))
Expect(err).ToNot(HaveOccurred())
err = display.Stop()
Expect(err).ToNot(HaveOccurred())
contents, err := ioutil.ReadFile(logFile1)
Expect(err).ToNot(HaveOccurred())
Expect(string(contents)).To(Equal(RedactedValue + "\n"))
contents, err = ioutil.ReadFile(logFile2)
Expect(err).ToNot(HaveOccurred())
Expect(string(contents)).To(Equal(RedactedValue + "\n"))
})
})
Describe("DisplayDump", func() {
It("creates the intermediate dirs and writes the dump to file", func() {
err := display.DisplayDump("this is a dump of stuff")
Expect(err).ToNot(HaveOccurred())
err = display.Stop()
Expect(err).ToNot(HaveOccurred())
contents, err := ioutil.ReadFile(logFile1)
Expect(err).ToNot(HaveOccurred())
Expect(string(contents)).To(Equal("this is a dump of stuff\n"))
contents, err = ioutil.ReadFile(logFile2)
Expect(err).ToNot(HaveOccurred())
Expect(string(contents)).To(Equal("this is a dump of stuff\n"))
})
It("redacts auth tokens", func() {
dump := `GET /apps/ce03a2e2-95c0-4f3b-abb9-32718d408c8b/stream HTTP/1.1
Host: wss://doppler.bosh-lite.com:443
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Version: 13
Sec-WebSocket-Key: [HIDDEN]
Set-Cookie: please-redact-this
Authorization: bearer eyJhbGciOiJ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m0U-ZPnTyV8qjtHUoLvks4GOuktXc6ZE3NriWODpKIU5WdMgEbvyhuTnUEn88rQnmGJbKvHOIilulb6avSkZfTEq1o8w4VLCeRDlVLNh5JzCUtGzLfImNb31ks_Wv6HuI8kFjQZ5PQiTYjlhkuDQOcNSaAyWxQ_7425hiA7x8omBgEr-uST7GsxLvgoHqQaDH0JSTgMmO_GaN_QD52JVuru9og
Origin: wss://doppler.bosh-lite.com:443`
err := display.DisplayDump(dump)
Expect(err).ToNot(HaveOccurred())
err = display.Stop()
Expect(err).ToNot(HaveOccurred())
raw, err := ioutil.ReadFile(logFile1)
Expect(err).ToNot(HaveOccurred())
contents := string(raw)
Expect(contents).To(MatchRegexp("Connection: Upgrade"))
Expect(contents).To(MatchRegexp(`Set-Cookie: \[PRIVATE DATA HIDDEN\]`))
Expect(contents).To(MatchRegexp(`Authorization: \[PRIVATE DATA HIDDEN\]`))
Expect(contents).To(MatchRegexp("Origin: wss://doppler.bosh-lite.com:443"))
raw, err = ioutil.ReadFile(logFile2)
Expect(err).ToNot(HaveOccurred())
contents = string(raw)
Expect(contents).To(MatchRegexp("Connection: Upgrade"))
Expect(contents).To(MatchRegexp(`Authorization: \[PRIVATE DATA HIDDEN\]`))
Expect(contents).To(MatchRegexp("Origin: wss://doppler.bosh-lite.com:443"))
})
})
Describe("DisplayHeader", func() {
It("writes the header key and value", func() {
err := display.DisplayHeader("Header", "Value")
Expect(err).ToNot(HaveOccurred())
err = display.Stop()
Expect(err).ToNot(HaveOccurred())
contents, err := ioutil.ReadFile(logFile1)
Expect(err).ToNot(HaveOccurred())
Expect(string(contents)).To(Equal("Header: Value\n\n"))
contents, err = ioutil.ReadFile(logFile2)
Expect(err).ToNot(HaveOccurred())
Expect(string(contents)).To(Equal("Header: Value\n\n"))
})
})
Describe("DisplayHost", func() {
It("writes the host", func() {
err := display.DisplayHost("banana")
Expect(err).ToNot(HaveOccurred())
err = display.Stop()
Expect(err).ToNot(HaveOccurred())
contents, err := ioutil.ReadFile(logFile1)
Expect(err).ToNot(HaveOccurred())
Expect(string(contents)).To(Equal("Host: banana\n\n"))
contents, err = ioutil.ReadFile(logFile2)
Expect(err).ToNot(HaveOccurred())
Expect(string(contents)).To(Equal("Host: banana\n\n"))
})
})
Describe("DisplayJSONBody", func() {
When("provided well formed JSON", func() {
It("writes a formatted output", func() {
raw := `{"a":"b", "c":"d", "don't escape HTML":"<&>"}`
formatted := `{
"a": "b",
"c": "d",
"don't escape HTML": "<&>"
}
` // Additional spaces required
err := display.DisplayJSONBody([]byte(raw))
Expect(err).ToNot(HaveOccurred())
err = display.Stop()
Expect(err).ToNot(HaveOccurred())
contents, err := ioutil.ReadFile(logFile1)
Expect(err).ToNot(HaveOccurred())
Expect(string(contents)).To(Equal(formatted))
contents, err = ioutil.ReadFile(logFile2)
Expect(err).ToNot(HaveOccurred())
Expect(string(contents)).To(Equal(formatted))
})
})
When("the body is empty", func() {
It("does not write the body", func() {
err := display.DisplayJSONBody(nil)
Expect(err).ToNot(HaveOccurred())
err = display.Stop()
Expect(err).ToNot(HaveOccurred())
contents, err := ioutil.ReadFile(logFile1)
Expect(err).ToNot(HaveOccurred())
Expect(string(contents)).To(Equal("\n"))
contents, err = ioutil.ReadFile(logFile2)
Expect(err).ToNot(HaveOccurred())
Expect(string(contents)).To(Equal("\n"))
})
})
When("provided malformed JSON", func() {
It("displays the raw body", func() {
raw := `[{"data":1, "banana": 2}`
err := display.DisplayJSONBody([]byte(raw))
Expect(err).ToNot(HaveOccurred())
err = display.Stop()
Expect(err).ToNot(HaveOccurred())
contents, err := ioutil.ReadFile(logFile1)
Expect(err).ToNot(HaveOccurred())
Expect(string(contents)).To(Equal(raw + "\n\n"))
contents, err = ioutil.ReadFile(logFile2)
Expect(err).ToNot(HaveOccurred())
Expect(string(contents)).To(Equal(raw + "\n\n"))
})
})
})
Describe("DisplayMessage", func() {
It("writes the message", func() {
msg := "i am a message!!!!"
err := display.DisplayMessage(msg)
Expect(err).ToNot(HaveOccurred())
err = display.Stop()
Expect(err).ToNot(HaveOccurred())
contents, err := ioutil.ReadFile(logFile1)
Expect(err).ToNot(HaveOccurred())
Expect(string(contents)).To(ContainSubstring(msg))
contents, err = ioutil.ReadFile(logFile2)
Expect(err).ToNot(HaveOccurred())
Expect(string(contents)).To(ContainSubstring(msg))
})
})
Describe("DisplayRequestHeader", func() {
It("writes the method, uri and http protocol", func() {
err := display.DisplayRequestHeader("GET", "/v2/spaces/guid/summary", "HTTP/1.1")
Expect(err).ToNot(HaveOccurred())
err = display.Stop()
Expect(err).ToNot(HaveOccurred())
contents, err := ioutil.ReadFile(logFile1)
Expect(err).ToNot(HaveOccurred())
Expect(string(contents)).To(Equal("GET /v2/spaces/guid/summary HTTP/1.1\n\n"))
contents, err = ioutil.ReadFile(logFile2)
Expect(err).ToNot(HaveOccurred())
Expect(string(contents)).To(Equal("GET /v2/spaces/guid/summary HTTP/1.1\n\n"))
})
})
Describe("DisplayResponseHeader", func() {
It("writes the method, uri and http protocol", func() {
err := display.DisplayResponseHeader("HTTP/1.1", "200 OK")
Expect(err).ToNot(HaveOccurred())
err = display.Stop()
Expect(err).ToNot(HaveOccurred())
contents, err := ioutil.ReadFile(logFile1)
Expect(err).ToNot(HaveOccurred())
Expect(string(contents)).To(Equal("HTTP/1.1 200 OK\n\n"))
contents, err = ioutil.ReadFile(logFile2)
Expect(err).ToNot(HaveOccurred())
Expect(string(contents)).To(Equal("HTTP/1.1 200 OK\n\n"))
})
})
Describe("DisplayType", func() {
It("writes the passed type and time in localized ISO 8601", func() {
passedTime := time.Now()
err := display.DisplayType("banana", passedTime)
Expect(err).ToNot(HaveOccurred())
err = display.Stop()
Expect(err).ToNot(HaveOccurred())
contents, err := ioutil.ReadFile(logFile1)
Expect(err).ToNot(HaveOccurred())
Expect(string(contents)).To(Equal(fmt.Sprintf("banana: [%s]\n\n", passedTime.Format(time.RFC3339))))
contents, err = ioutil.ReadFile(logFile2)
Expect(err).ToNot(HaveOccurred())
Expect(string(contents)).To(Equal(fmt.Sprintf("banana: [%s]\n\n", passedTime.Format(time.RFC3339))))
})
})
Describe("HandleInternalError", func() {
It("sends error to standard error", func() {
err := errors.New("foobar")
display.HandleInternalError(err)
Expect(ui.Err).To(Say("foobar"))
Expect(display.Stop()).NotTo(HaveOccurred())
})
})
Describe("Start and Stop", func() {
BeforeEach(func() {
// Cleanup old display output directory
Expect(display.Stop()).NotTo(HaveOccurred())
logFile1 = filepath.Join(tmpdir, "tmp_sub_dir", "tmpfile3")
logFile2 = filepath.Join(tmpdir, "tmp", "sub", "dir", ".", "tmpfile4")
display = ui.RequestLoggerFileWriter([]string{logFile1, logFile2})
})
It("locks and then unlocks the mutex properly", func() { // and creates the intermediate dirs
c := make(chan bool)
go func() {
Expect(display.Start()).ToNot(HaveOccurred())
c <- true
}()
Eventually(c).Should(Receive())
Expect(display.Stop()).NotTo(HaveOccurred())
})
})
})
Describe("when the log file path is invalid", func() {
var pathName string
BeforeEach(func() {
var err error
tmpdir, err = ioutil.TempDir("", "request_logger")
Expect(err).ToNot(HaveOccurred())
pathName = filepath.Join(tmpdir, "foo")
})
AfterEach(func() {
Expect(display.Stop()).NotTo(HaveOccurred())
Expect(os.RemoveAll(tmpdir)).NotTo(HaveOccurred())
})
It("returns the os error when we unsuccessfully try to write to a file", func() {
Expect(os.Mkdir(pathName, os.ModeDir|os.ModePerm)).NotTo(HaveOccurred())
display = ui.RequestLoggerFileWriter([]string{pathName})
err := display.Start()
Expect(err).To(MatchError(fmt.Sprintf("open %s: is a directory", pathName)))
})
It("returns the os error when the parent directory for the log file is in the root directory", func() {
file, err := os.OpenFile(pathName, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
Expect(err).ToNot(HaveOccurred())
_, err = file.WriteString("hello world")
Expect(err).ToNot(HaveOccurred())
err = file.Close()
Expect(err).ToNot(HaveOccurred())
display = ui.RequestLoggerFileWriter([]string{filepath.Join(pathName, "bar")})
err = display.Start()
pathName = strings.Replace(pathName, `\`, `\\`, -1)
Expect(err).To(MatchError(MatchRegexp(fmt.Sprintf("mkdir %s", pathName))))
})
})
Describe("UI", func() {
Describe("RequestLoggerFileWriter", func() {
It("returns a RequestLoggerFileWriter with the consistent filewriting mutex", func() {
logger1 := ui.RequestLoggerFileWriter(nil)
logger2 := ui.RequestLoggerFileWriter(nil)
c := make(chan bool)
err := logger1.Start()
Expect(err).ToNot(HaveOccurred())
go func() {
Expect(logger2.Start()).ToNot(HaveOccurred())
c <- true
}()
Consistently(c).ShouldNot(Receive())
Expect(logger1.Stop()).ToNot(HaveOccurred())
Eventually(c).Should(Receive())
})
})
})
})