| 1 | package apierrors |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "io/ioutil" |
| 6 | "net/http" |
| 7 | "reflect" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestFromResponse(t *testing.T) { |
| 12 | for _, tt := range []struct { |
| 13 | r *http.Response |
| 14 | e error |
| 15 | }{ |
| 16 | {nil, nil}, |
| 17 | { |
| 18 | &http.Response{StatusCode: 200}, |
| 19 | nil, |
| 20 | }, |
| 21 | { |
| 22 | &http.Response{StatusCode: 400, Body: ioutil.NopCloser(bytes.NewBufferString("missing framework id"))}, |
| 23 | &Error{400, ErrorTable[CodeMalformedRequest] + ": missing framework id"}, |
| 24 | }, |
| 25 | } { |
| 26 | rr := FromResponse(tt.r) |
| 27 | if !reflect.DeepEqual(tt.e, rr) { |
| 28 | t.Errorf("Expected: %v, got: %v", tt.e, rr) |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | func TestError(t *testing.T) { |
| 34 | for _, tt := range []struct { |
| 35 | code Code |
| 36 | isErr bool |
| 37 | details string |
| 38 | wantsMessage string |
| 39 | temporary bool |
| 40 | subscriptionLoss bool |
| 41 | }{ |
| 42 | {200, false, "", "", false, false}, |
| 43 | {400, true, "", "malformed request", false, false}, |
| 44 | {400, true, "foo", "malformed request: foo", false, false}, |
| 45 | } { |
| 46 | err := tt.code.Error(tt.details) |
| 47 | if !tt.code.Matches(err) { |
| 48 | t.Errorf("expected expected code %v to match that of the error %q", tt.code, err) |
| 49 | } |
| 50 | if tt.isErr != (err != nil) { |
| 51 | t.Errorf("expected isErr %v but error was %q", tt.isErr, err) |
| 52 | } |
| 53 | if err != nil { |
| 54 | if err.Error() != tt.wantsMessage { |
| 55 | t.Errorf("Expected: %s, got: %s", tt.wantsMessage, err.Error()) |
| 56 | } |
| 57 | apierr := err.(*Error) |
| 58 | if apierr.Temporary() != tt.temporary { |
| 59 | t.Errorf("expected temporary to be %v instead of %v", tt.temporary, apierr.Temporary()) |
| 60 | } |
| 61 | if apierr.SubscriptionLoss() != tt.subscriptionLoss { |
| 62 | t.Errorf("expected subscription-loss to be %v instead of %v", tt.subscriptionLoss, apierr.SubscriptionLoss()) |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | } |