(t *testing.T)
| 125 | } |
| 126 | |
| 127 | func TestServerHandleDoNotLogError(t *testing.T) { |
| 128 | testCases := map[string]struct { |
| 129 | errorCode int |
| 130 | doNotLogError bool |
| 131 | expectedError bool |
| 132 | }{ |
| 133 | "HTTPResponse with code 5xx and with DoNotLogError header should return a non-loggable error": { |
| 134 | errorCode: http.StatusInternalServerError, |
| 135 | doNotLogError: true, |
| 136 | expectedError: true, |
| 137 | }, |
| 138 | "HTTPResponse with code 5xx and without DoNotLogError header should return a loggable error": { |
| 139 | errorCode: http.StatusInternalServerError, |
| 140 | expectedError: true, |
| 141 | }, |
| 142 | "HTTPResponse with code different from 5xx and with DoNotLogError header should not return an error": { |
| 143 | errorCode: http.StatusBadRequest, |
| 144 | doNotLogError: true, |
| 145 | expectedError: false, |
| 146 | }, |
| 147 | "HTTPResponse with code different from 5xx and without DoNotLogError header should not return an error": { |
| 148 | errorCode: http.StatusBadRequest, |
| 149 | expectedError: false, |
| 150 | }, |
| 151 | } |
| 152 | errMsg := "this is an error" |
| 153 | for testName, testData := range testCases { |
| 154 | t.Run(testName, func(t *testing.T) { |
| 155 | h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 156 | if testData.doNotLogError { |
| 157 | w.Header().Set(DoNotLogErrorHeaderKey, "true") |
| 158 | } |
| 159 | http.Error(w, errMsg, testData.errorCode) |
| 160 | }) |
| 161 | |
| 162 | s := NewServer(h) |
| 163 | req := &httpgrpc.HTTPRequest{ |
| 164 | Method: "GET", |
| 165 | Url: "/test", |
| 166 | } |
| 167 | resp, err := s.Handle(context.Background(), req) |
| 168 | if testData.expectedError { |
| 169 | require.Error(t, err) |
| 170 | require.Nil(t, resp) |
| 171 | var optional middleware.OptionalLogging |
| 172 | if testData.doNotLogError { |
| 173 | require.ErrorAs(t, err, &optional) |
| 174 | |
| 175 | actual, _ := optional.ShouldLog(context.Background()) |
| 176 | require.False(t, actual) |
| 177 | } else { |
| 178 | require.False(t, errors.As(err, &optional)) |
| 179 | } |
| 180 | checkError(t, err, testData.errorCode, errMsg) |
| 181 | } else { |
| 182 | require.NoError(t, err) |
| 183 | require.NotNil(t, resp) |
| 184 | checkHTTPResponse(t, resp, testData.errorCode, errMsg) |
nothing calls this directly
no test coverage detected