github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/util/ui/request_logger_terminal_display_test.go (about) 1 package ui_test 2 3 import ( 4 "errors" 5 "time" 6 7 . "github.com/liamawhite/cli-with-i18n/util/ui" 8 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11 . "github.com/onsi/gomega/gbytes" 12 ) 13 14 var _ = Describe("Request Logger Terminal Display", func() { 15 var ( 16 out *Buffer 17 testUI *UI 18 display *RequestLoggerTerminalDisplay 19 ) 20 21 BeforeEach(func() { 22 out = NewBuffer() 23 testUI = NewTestUI(nil, out, NewBuffer()) 24 display = testUI.RequestLoggerTerminalDisplay() 25 Expect(display.Start()).ToNot(HaveOccurred()) 26 }) 27 28 Describe("DisplayBody", func() { 29 It("displays the redacted value", func() { 30 err := display.DisplayBody([]byte("some-string body")) 31 Expect(err).ToNot(HaveOccurred()) 32 33 err = display.Stop() 34 Expect(err).ToNot(HaveOccurred()) 35 36 Expect(testUI.Out).To(Say("\\[PRIVATE DATA HIDDEN\\]")) 37 }) 38 }) 39 40 Describe("DisplayDump", func() { 41 It("displays the passed in string", func() { 42 err := display.DisplayDump("some-dump-of-string") 43 Expect(err).ToNot(HaveOccurred()) 44 45 err = display.Stop() 46 Expect(err).ToNot(HaveOccurred()) 47 48 Expect(testUI.Out).To(Say("some-dump-of-string")) 49 }) 50 51 It("redacts auth tokens", func() { 52 dump := `GET /apps/ce03a2e2-95c0-4f3b-abb9-32718d408c8b/stream HTTP/1.1 53 Host: wss://doppler.bosh-lite.com:443 54 Upgrade: websocket 55 Connection: Upgrade 56 Sec-WebSocket-Version: 13 57 Sec-WebSocket-Key: [HIDDEN] 58 Authorization: bearer eyJhbGciOiJSUzI1NiIsImtpZCI6ImtleS0xIiwidHlwIjoiSldUIn0.eyJqdGkiOiI3YzRmYWEyZjI5MmQ0MTQ5ODM5NGE3OTU0Y2E3ZWNlMCIsInN1YiI6IjIyMjNiM2IzLTE3ZTktNDJkNi1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jlhkuDQOcNSaAyWxQ_7425hiA7x8omBgEr-uST7GsxLvgoHqQaDH0JSTgMmO_GaN_QD52JVuru9og 59 Origin: wss://doppler.bosh-lite.com:443` 60 err := display.DisplayDump(dump) 61 Expect(err).ToNot(HaveOccurred()) 62 63 err = display.Stop() 64 Expect(err).ToNot(HaveOccurred()) 65 66 Expect(testUI.Out).To(Say("Connection: Upgrade")) 67 Expect(testUI.Out).To(Say("Authorization: \\[PRIVATE DATA HIDDEN\\]")) 68 Expect(testUI.Out).To(Say("Origin: wss://doppler.bosh-lite.com:443")) 69 }) 70 }) 71 72 Describe("DisplayHeader", func() { 73 It("displays the header key and value", func() { 74 err := display.DisplayHeader("Header", "Value") 75 Expect(err).ToNot(HaveOccurred()) 76 77 err = display.Stop() 78 Expect(err).ToNot(HaveOccurred()) 79 80 Expect(testUI.Out).To(Say("Header: Value")) 81 }) 82 }) 83 84 Describe("DisplayHost", func() { 85 It("displays the host", func() { 86 err := display.DisplayHost("banana") 87 Expect(err).ToNot(HaveOccurred()) 88 89 err = display.Stop() 90 Expect(err).ToNot(HaveOccurred()) 91 92 Expect(testUI.Out).To(Say("Host: banana")) 93 }) 94 }) 95 96 Describe("DisplayJSONBody", func() { 97 Context("when provided well formed JSON", func() { 98 It("displayed a formated output", func() { 99 raw := `{"a":"b", "c":"d", "don't html escape":"<&>"}` 100 formatted := `{ 101 "a": "b", 102 "c": "d", 103 "don't html escape": "<&>" 104 }` 105 err := display.DisplayJSONBody([]byte(raw)) 106 Expect(err).ToNot(HaveOccurred()) 107 108 err = display.Stop() 109 Expect(err).ToNot(HaveOccurred()) 110 111 Expect(testUI.Out).To(Say(formatted)) 112 }) 113 }) 114 115 Context("when the body is empty", func() { 116 It("does not display the body", func() { 117 err := display.DisplayJSONBody(nil) 118 Expect(err).ToNot(HaveOccurred()) 119 120 err = display.Stop() 121 Expect(err).ToNot(HaveOccurred()) 122 123 Expect(string(out.Contents())).To(Equal("\n")) 124 }) 125 }) 126 127 Context("when provided malformed JSON", func() { 128 It("displays the raw body", func() { 129 raw := `[{"data":1, "banana": 2}]` 130 err := display.DisplayJSONBody([]byte(raw)) 131 Expect(err).ToNot(HaveOccurred()) 132 133 err = display.Stop() 134 Expect(err).ToNot(HaveOccurred()) 135 136 buff, ok := testUI.Out.(*Buffer) 137 Expect(ok).To(BeTrue()) 138 Expect(string(buff.Contents())).To(Equal(raw + "\n\n")) 139 }) 140 }) 141 }) 142 143 Describe("DisplayMessage", func() { 144 It("writes the message", func() { 145 msg := "i am a message!!!!" 146 err := display.DisplayMessage(msg) 147 Expect(err).ToNot(HaveOccurred()) 148 149 err = display.Stop() 150 Expect(err).ToNot(HaveOccurred()) 151 152 Expect(testUI.Out).To(Say(msg)) 153 }) 154 }) 155 156 Describe("DisplayRequestHeader", func() { 157 It("displays the method, uri and http protocol", func() { 158 err := display.DisplayRequestHeader("GET", "/v2/spaces/guid/summary", "HTTP/1.1") 159 Expect(err).ToNot(HaveOccurred()) 160 161 err = display.Stop() 162 Expect(err).ToNot(HaveOccurred()) 163 164 Expect(testUI.Out).To(Say("GET /v2/spaces/guid/summary HTTP/1.1")) 165 }) 166 }) 167 168 Describe("DisplayResponseHeader", func() { 169 It("displays the method, uri and http protocol", func() { 170 err := display.DisplayResponseHeader("HTTP/1.1", "200 OK") 171 Expect(err).ToNot(HaveOccurred()) 172 173 err = display.Stop() 174 Expect(err).ToNot(HaveOccurred()) 175 176 Expect(testUI.Out).To(Say("HTTP/1.1 200 OK")) 177 }) 178 }) 179 180 Describe("DisplayType", func() { 181 It("displays the passed type and time in localized ISO 8601", func() { 182 passedTime := time.Now() 183 err := display.DisplayType("banana", passedTime) 184 Expect(err).ToNot(HaveOccurred()) 185 186 err = display.Stop() 187 Expect(err).ToNot(HaveOccurred()) 188 189 Expect(testUI.Out).To(Say("banana: \\[%s\\]", passedTime.Format(time.RFC3339))) 190 }) 191 }) 192 193 Describe("HandleInternalError", func() { 194 It("sends error to standard error", func() { 195 err := errors.New("foobar") 196 display.HandleInternalError(err) 197 198 err = display.Stop() 199 Expect(err).ToNot(HaveOccurred()) 200 201 Expect(testUI.Err).To(Say("foobar")) 202 }) 203 }) 204 205 Describe("Start and Stop", func() { 206 It("locks and then unlocks the mutex properly", func() { 207 c := make(chan bool) 208 go func() { 209 Expect(display.Start()).NotTo(HaveOccurred()) 210 c <- true 211 }() 212 Consistently(c).ShouldNot(Receive()) 213 Expect(display.Stop()).NotTo(HaveOccurred()) 214 Eventually(c).Should(Receive()) 215 Expect(display.Stop()).NotTo(HaveOccurred()) 216 }) 217 }) 218 })