(t *testing.T)
| 2230 | } |
| 2231 | |
| 2232 | func TestDo_preservesResponseInHTTPError(t *testing.T) { |
| 2233 | t.Parallel() |
| 2234 | client, mux, _ := setup(t) |
| 2235 | |
| 2236 | mux.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) { |
| 2237 | w.Header().Set("Content-Type", "application/json") |
| 2238 | w.WriteHeader(http.StatusNotFound) |
| 2239 | fmt.Fprint(w, `{ |
| 2240 | "message": "Resource not found", |
| 2241 | "documentation_url": "https://docs.github.com/rest/reference/repos#get-a-repository" |
| 2242 | }`) |
| 2243 | }) |
| 2244 | |
| 2245 | req, _ := client.NewRequest(t.Context(), "GET", ".", nil) |
| 2246 | var resp *Response |
| 2247 | var data any |
| 2248 | resp, err := client.Do(req, &data) |
| 2249 | |
| 2250 | if err == nil { |
| 2251 | t.Fatal("Expected error response") |
| 2252 | } |
| 2253 | |
| 2254 | // Verify error type and access to status code |
| 2255 | var errResp *ErrorResponse |
| 2256 | if !errors.As(err, &errResp) { |
| 2257 | t.Fatalf("Expected *ErrorResponse error, got %T", err) |
| 2258 | } |
| 2259 | |
| 2260 | // Verify status code is accessible from both Response and ErrorResponse |
| 2261 | if resp == nil { |
| 2262 | t.Fatal("Expected response to be returned even with error") |
| 2263 | } |
| 2264 | if got, want := resp.StatusCode, http.StatusNotFound; got != want { |
| 2265 | t.Errorf("Response status = %v, want %v", got, want) |
| 2266 | } |
| 2267 | if got, want := errResp.Response.StatusCode, http.StatusNotFound; got != want { |
| 2268 | t.Errorf("Error response status = %v, want %v", got, want) |
| 2269 | } |
| 2270 | |
| 2271 | // Verify error contains proper message |
| 2272 | if !strings.Contains(errResp.Message, "Resource not found") { |
| 2273 | t.Errorf("Error message = %q, want to contain 'Resource not found'", errResp.Message) |
| 2274 | } |
| 2275 | } |
| 2276 | |
| 2277 | // TestDo_AcceptedError_LargeBodyTruncated verifies that when the API returns a |
| 2278 | // 202 Accepted with a body larger than maxErrorBodySize, the client reads at |
nothing calls this directly
no test coverage detected
searching dependent graphs…