-
Notifications
You must be signed in to change notification settings - Fork 6
/
client_test.go
61 lines (51 loc) · 1.59 KB
/
client_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright 2021-present Airheart, Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
package duffel
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"gopkg.in/h2non/gock.v1"
)
func TestClientError(t *testing.T) {
ctx := context.TODO()
a := assert.New(t)
gock.New("https://api.duffel.com/air/offer_requests").
MatchParam("return_offers", "false").
Reply(400).
File("fixtures/400-bad-request.json")
defer gock.Off()
client := New("duffel_test_123")
data, err := client.CreateOfferRequest(ctx, OfferRequestInput{
ReturnOffers: false,
})
a.Error(err)
a.Nil(data)
a.Equal("duffel: The airline responded with an unexpected error, please contact support", err.Error())
derr := err.(*DuffelError)
a.True(derr.IsType(AirlineError))
a.True(derr.IsCode(AirlineUnknown))
a.True(IsErrorType(err, AirlineError))
a.True(IsErrorCode(err, AirlineUnknown))
a.True(ErrIsRetryable(err))
reqId, ok := RequestIDFromError(err)
a.True(ok)
a.Equal("FZW0H3HdJwKk5HMAAKxB", reqId)
}
func TestClientErrorBadGateway(t *testing.T) {
ctx := context.TODO()
a := assert.New(t)
gock.New("https://api.duffel.com/air/offer_requests").
Reply(502).
AddHeader("Content-Type", "text/html").
File("fixtures/502-bad-gateway.html")
defer gock.Off()
client := New("duffel_test_123")
data, err := client.CreateOfferRequest(ctx, OfferRequestInput{
ReturnOffers: true,
})
a.Error(err)
a.Nil(data)
a.Equal("duffel: An internal server error occurred. Please try again later.", err.Error())
}