github.com/arunkumar7540/cli@v6.45.0+incompatible/util/ui/request_logger_terminal_display_test.go (about)

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