(t *testing.T)
| 109 | } |
| 110 | |
| 111 | func TestResolveResponseStatus(t *testing.T) { |
| 112 | someErr := errors.New("some error") |
| 113 | |
| 114 | var testCases = []struct { |
| 115 | name string |
| 116 | whenResp http.ResponseWriter |
| 117 | whenErr error |
| 118 | expectStatus int |
| 119 | expectResp bool |
| 120 | }{ |
| 121 | { |
| 122 | name: "nil resp, nil err -> 200", |
| 123 | whenResp: nil, |
| 124 | whenErr: nil, |
| 125 | expectStatus: http.StatusOK, |
| 126 | expectResp: false, |
| 127 | }, |
| 128 | { |
| 129 | name: "resp suggested status used when no error", |
| 130 | whenResp: &Response{Status: http.StatusCreated}, |
| 131 | whenErr: nil, |
| 132 | expectStatus: http.StatusCreated, |
| 133 | expectResp: true, |
| 134 | }, |
| 135 | { |
| 136 | name: "error overrides suggested status with StatusCode(err)", |
| 137 | whenResp: &Response{Status: http.StatusAccepted}, |
| 138 | whenErr: ErrBadRequest, |
| 139 | expectStatus: http.StatusBadRequest, |
| 140 | expectResp: true, |
| 141 | }, |
| 142 | { |
| 143 | name: "error overrides suggested status with 500 when StatusCode(err)==0", |
| 144 | whenResp: &Response{Status: http.StatusAccepted}, |
| 145 | whenErr: ErrInternalServerError, |
| 146 | expectStatus: http.StatusInternalServerError, |
| 147 | expectResp: true, |
| 148 | }, |
| 149 | { |
| 150 | name: "nil resp, error -> 500 when StatusCode(err)==0", |
| 151 | whenResp: nil, |
| 152 | whenErr: someErr, |
| 153 | expectStatus: http.StatusInternalServerError, |
| 154 | expectResp: false, |
| 155 | }, |
| 156 | { |
| 157 | name: "committed response wins over error", |
| 158 | whenResp: &Response{Committed: true, Status: http.StatusNoContent}, |
| 159 | whenErr: someErr, |
| 160 | expectStatus: http.StatusNoContent, |
| 161 | expectResp: true, |
| 162 | }, |
| 163 | { |
| 164 | name: "committed response with status 0 falls back to 200 (defensive)", |
| 165 | whenResp: &Response{Committed: true, Status: 0}, |
| 166 | whenErr: someErr, |
| 167 | expectStatus: http.StatusOK, |
| 168 | expectResp: true, |
nothing calls this directly
no test coverage detected
searching dependent graphs…