httpexpect icon indicating copy to clipboard operation
httpexpect copied to clipboard

Colorize http requests and responses

Open gavv opened this issue 3 years ago • 3 comments

Follow-up for https://github.com/gavv/httpexpect/issues/159

Currently defaultFailureTemplate colorizes requests/responses as follows:

{{ .Request | indent | trim | color $.EnableColors "HiMagenta" }}
{{ .Response | indent | trim | color $.EnableColors "HiMagenta" }}

The following improvement is suggested:

  • add new function "colorhttp" to defaultTemplateFuncs and use it for request and response
  • if colors are disabled, the function will return original value
  • the function will apply the following colorization:
    • in lines like GET /test HTTP/1.1 it will highlight GET (method) and HTTP/1.1 (protocol)
    • in lines like HTTP/1.1 200 OK 229.642µs it will highlight HTTP/1.1 (protocol) and 200 OK (status code)
    • in lines like Host: 127.0.0.1:38681 it will highlight Host: (header field name)
  • the suggested color for highlighting is HiMagenta (in line with #334)

Example for testing:

$ FORCE_COLOR=1 go test -v -run TestE2EReport_Names
=== RUN   TestE2EReport_Names
    e2e_report_test.go:52: 
        invalid "Content-Type" response header
          mime: no media type
        
        test name: TestExample
        
        request name: RequestExample
        
        request: GET /test HTTP/1.1
          Host: 127.0.0.1:35779
        
        response: HTTP/1.1 200 OK 246.75µs
          Date: Wed, 22 Mar 2023 18:38:49 GMT
          Content-Length: 0
        
        assertion:
          Request("GET").Expect().JSON()
        
        actual value:
          ""
--- PASS: TestE2EReport_Names (0.00s)
PASS
ok  	github.com/gavv/httpexpect/v2	0.003s

Related: #334, #335.

gavv avatar Mar 22 '23 18:03 gavv

Hey @gavv, can I take this one? 🙂

gdguesser avatar Mar 22 '23 19:03 gdguesser

Sure, thanks!

gavv avatar Mar 22 '23 20:03 gavv

Hey @gavv can I pick this task if @gdguesser is not working on this?. I have made code changes for this. https://github.com/gavv/httpexpect/pull/414

FillerFree avatar Apr 27 '23 04:04 FillerFree