(w http.ResponseWriter, r *http.Request)
| 144 | } |
| 145 | |
| 146 | func (ms *mockUpstream) handle(w http.ResponseWriter, r *http.Request) { |
| 147 | call := int(ms.Calls.Add(1) - 1) |
| 148 | |
| 149 | body, err := io.ReadAll(r.Body) |
| 150 | defer r.Body.Close() |
| 151 | require.NoError(ms.t, err) |
| 152 | |
| 153 | ms.mu.Lock() |
| 154 | ms.requests = append(ms.requests, receivedRequest{ |
| 155 | Method: r.Method, |
| 156 | Path: r.URL.Path, |
| 157 | Header: r.Header.Clone(), |
| 158 | Body: append([]byte(nil), body...), |
| 159 | }) |
| 160 | ms.mu.Unlock() |
| 161 | |
| 162 | validateRequest(ms.t, call, r.URL.Path, body) |
| 163 | |
| 164 | resp := ms.responseForCall(call) |
| 165 | if resp.OnRequest != nil { |
| 166 | resp.OnRequest(r, body) |
| 167 | } |
| 168 | |
| 169 | if isStreaming(body, r.URL.Path) { |
| 170 | require.NotEmpty(ms.t, resp.Streaming, "response #%d: Streaming body is empty (fixture missing streaming response?)", call+1) |
| 171 | if isRawHTTPResponse(resp.Streaming) { |
| 172 | ms.writeRawHTTPResponse(w, r, resp.Streaming) |
| 173 | return |
| 174 | } |
| 175 | ms.writeSSE(w, resp.Streaming) |
| 176 | return |
| 177 | } |
| 178 | |
| 179 | require.NotEmpty(ms.t, resp.Blocking, "response #%d: Blocking body is empty (fixture missing non-streaming response?)", call+1) |
| 180 | if isRawHTTPResponse(resp.Blocking) { |
| 181 | ms.writeRawHTTPResponse(w, r, resp.Blocking) |
| 182 | return |
| 183 | } |
| 184 | |
| 185 | status := cmp.Or(ms.StatusCode, http.StatusOK) |
| 186 | w.Header().Set("Content-Type", "application/json") |
| 187 | w.WriteHeader(status) |
| 188 | _, _ = w.Write(resp.Blocking) |
| 189 | } |
| 190 | |
| 191 | func (ms *mockUpstream) responseForCall(call int) upstreamResponse { |
| 192 | if call >= len(ms.responses) { |
nothing calls this directly
no test coverage detected