| 64 | } |
| 65 | |
| 66 | func TestWithNonFatal500WithBody(t *testing.T) { |
| 67 | c, _ := NewClient(nil) |
| 68 | |
| 69 | var called uint32 |
| 70 | |
| 71 | nonFatalCodes := map[int]string{ |
| 72 | 501: "custom 501 error", |
| 73 | 507: "custom 507 error", |
| 74 | 509: "custom 509 error", |
| 75 | } |
| 76 | |
| 77 | for nonFatalCode, expectedErr := range nonFatalCodes { |
| 78 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 79 | if r.URL.String() != "/test" { |
| 80 | w.WriteHeader(http.StatusNotFound) |
| 81 | return |
| 82 | } |
| 83 | |
| 84 | atomic.AddUint32(&called, 1) |
| 85 | w.Header().Set("Content-Type", "application/json") |
| 86 | w.WriteHeader(nonFatalCode) |
| 87 | w.Write([]byte(`{"message":"` + expectedErr + `"}`)) |
| 88 | })) |
| 89 | |
| 90 | req, err := http.NewRequest("GET", srv.URL+"/test", nil) |
| 91 | assert.Nil(t, err) |
| 92 | |
| 93 | _, err = c.Do(req) |
| 94 | t.Logf("non fatal code %d", nonFatalCode) |
| 95 | assert.NotNil(t, err) |
| 96 | assert.Equal(t, expectedErr, err.Error()) |
| 97 | srv.Close() |
| 98 | } |
| 99 | |
| 100 | assert.EqualValues(t, 3, called) |
| 101 | } |
| 102 | |
| 103 | func TestAuthErrWithoutBody(t *testing.T) { |
| 104 | var called uint32 |