| 154 | } |
| 155 | |
| 156 | func (r *Response) getContent(opChain *chain, method string) ([]byte, bool) { |
| 157 | switch r.contentState { |
| 158 | case contentRetreived: |
| 159 | return r.content, true |
| 160 | |
| 161 | case contentFailed: |
| 162 | return nil, false |
| 163 | |
| 164 | case contentPending: |
| 165 | break |
| 166 | |
| 167 | case contentHijacked: |
| 168 | opChain.fail(AssertionFailure{ |
| 169 | Type: AssertUsage, |
| 170 | Errors: []error{ |
| 171 | fmt.Errorf("cannot call %s because Reader() was already called", method), |
| 172 | }, |
| 173 | }) |
| 174 | return nil, false |
| 175 | } |
| 176 | |
| 177 | resp := r.httpResp |
| 178 | |
| 179 | if resp.Body == nil || resp.Body == http.NoBody { |
| 180 | return []byte{}, true |
| 181 | } |
| 182 | |
| 183 | if bw, ok := resp.Body.(*bodyWrapper); ok { |
| 184 | bw.Rewind() |
| 185 | } |
| 186 | |
| 187 | content, err := io.ReadAll(resp.Body) |
| 188 | |
| 189 | closeErr := resp.Body.Close() |
| 190 | if err == nil { |
| 191 | err = closeErr |
| 192 | } |
| 193 | |
| 194 | if err != nil { |
| 195 | opChain.fail(AssertionFailure{ |
| 196 | Type: AssertOperation, |
| 197 | Errors: []error{ |
| 198 | errors.New("failed to read response body"), |
| 199 | err, |
| 200 | }, |
| 201 | }) |
| 202 | |
| 203 | r.content = nil |
| 204 | r.contentState = contentFailed |
| 205 | |
| 206 | return nil, false |
| 207 | } |
| 208 | |
| 209 | r.content = content |
| 210 | r.contentState = contentRetreived |
| 211 | r.contentMethod = method |
| 212 | |
| 213 | return r.content, true |