github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/httputil/response_test.go (about) 1 package httputil_test 2 3 import ( 4 "fmt" 5 "net/http" 6 "net/http/httptest" 7 "testing" 8 9 "github.com/kyma-project/kyma-environment-broker/internal/httputil" 10 "github.com/kyma-project/kyma-environment-broker/internal/logger" 11 12 "github.com/stretchr/testify/assert" 13 "github.com/stretchr/testify/require" 14 ) 15 16 func TestResponseWriterDevModeOff(t *testing.T) { 17 logSpy := logger.NewLogSpy() 18 writer := httputil.NewResponseWriter(logSpy.Logger, false) 19 20 for testName, testCase := range map[string]struct { 21 testedFn func(rw http.ResponseWriter, r *http.Request, err error, context string) 22 body string 23 status int 24 }{ 25 "NotFound": { 26 testedFn: writer.NotFound, 27 status: http.StatusNotFound, 28 body: `{ 29 "status": 404, 30 "requestId": "123-456-678", 31 "message": "Whoops! We can't find what you're looking for. Please try again." 32 }`, 33 }, 34 "InternalServerError": { 35 testedFn: writer.InternalServerError, 36 status: http.StatusInternalServerError, 37 body: `{ 38 "status": 500, 39 "requestId": "123-456-678", 40 "message": "Something went very wrong. Please try again." 41 }`, 42 }, 43 } { 44 t.Run(testName, func(t *testing.T) { 45 // given 46 logSpy.Reset() 47 var ( 48 respSpy = httptest.NewRecorder() 49 fixReq = fixRequest(t) 50 fixErr = fmt.Errorf("fix error") 51 fixErrContext = "generated by testing scenario" 52 ) 53 54 // when 55 testCase.testedFn(respSpy, fixReq, fixErr, fixErrContext) 56 57 // then 58 59 logSpy.AssertErrorLogged(t, fmt.Errorf("generated by testing scenario: %w", fixErr)) 60 61 assert.Equal(t, testCase.status, respSpy.Code) 62 assert.JSONEq(t, testCase.body, respSpy.Body.String()) 63 }) 64 } 65 } 66 67 func TestResponseWriterDevModeOn(t *testing.T) { 68 logSpy := logger.NewLogSpy() 69 writer := httputil.NewResponseWriter(logSpy.Logger, true) 70 71 for testName, testCase := range map[string]struct { 72 testedFn func(rw http.ResponseWriter, r *http.Request, err error, context string) 73 body string 74 status int 75 }{ 76 "NotFound": { 77 testedFn: writer.NotFound, 78 status: http.StatusNotFound, 79 body: `{ 80 "status": 404, 81 "requestId": "123-456-678", 82 "message": "Whoops! We can't find what you're looking for. Please try again.", 83 "details": "generated by testing scenario: fix error" 84 }`, 85 }, 86 "InternalServerError": { 87 testedFn: writer.InternalServerError, 88 status: http.StatusInternalServerError, 89 body: `{ 90 "status": 500, 91 "requestId": "123-456-678", 92 "message": "Something went very wrong. Please try again.", 93 "details": "generated by testing scenario: fix error" 94 }`, 95 }, 96 } { 97 t.Run(testName, func(t *testing.T) { 98 // given 99 logSpy.Reset() 100 var ( 101 respSpy = httptest.NewRecorder() 102 fixReq = fixRequest(t) 103 fixErr = fmt.Errorf("fix error") 104 fixErrContext = "generated by testing scenario" 105 ) 106 107 // when 108 testCase.testedFn(respSpy, fixReq, fixErr, fixErrContext) 109 110 // then 111 assert.Empty(t, logSpy.DumpAll()) 112 113 assert.Equal(t, testCase.status, respSpy.Code) 114 assert.JSONEq(t, testCase.body, respSpy.Body.String()) 115 }) 116 } 117 } 118 119 func fixRequest(t *testing.T) *http.Request { 120 req, err := http.NewRequest(http.MethodGet, "http://testing.url", nil) 121 require.NoError(t, err) 122 req.Header.Set("X-Request-Id", "123-456-678") 123 124 return req 125 }