-
Notifications
You must be signed in to change notification settings - Fork 44
/
shipping_methods_test.go
87 lines (75 loc) · 2.47 KB
/
shipping_methods_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package recurly_test
import (
"context"
"encoding/xml"
"net/http"
"testing"
"github.com/blacklightcms/recurly"
"github.com/google/go-cmp/cmp"
)
func TestShippingMethods_Get(t *testing.T) {
t.Run("OK", func(t *testing.T) {
client, s := recurly.NewTestServer()
defer s.Close()
s.HandleFunc("GET", "/v2/shipping_methods/fast_fast_fast", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write(MustOpenFile("shipping_method.xml"))
}, t)
if method, err := client.ShippingMethods.Get(context.Background(), "fast_fast_fast"); err != nil {
t.Fatal(err)
} else if diff := cmp.Diff(method, NewTestShippingMethod()); diff != "" {
t.Fatal(diff)
} else if !s.Invoked {
t.Fatal("expected fn invocation")
}
})
// Ensure a 404 returns nil values.
t.Run("ErrNotFound", func(t *testing.T) {
client, s := recurly.NewTestServer()
defer s.Close()
s.HandleFunc("GET", "/v2/shipping_methods/fast_fast_fast", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
}, t)
if method, err := client.ShippingMethods.Get(context.Background(), "fast_fast_fast"); !s.Invoked {
t.Fatal("expected fn invocation")
} else if err != nil {
t.Fatal(err)
} else if method != nil {
t.Fatalf("expected nil: %#v", method)
}
})
}
func TestShippingMethods_List(t *testing.T) {
client, s := recurly.NewTestServer()
defer s.Close()
var invocations int
s.HandleFunc("GET", "/v2/shipping_methods", func(w http.ResponseWriter, r *http.Request) {
invocations++
w.WriteHeader(http.StatusOK)
w.Write(MustOpenFile("shipping_methods.xml"))
}, t)
pager := client.ShippingMethods.List(nil)
for pager.Next() {
var methods []recurly.ShippingMethod
if err := pager.Fetch(context.Background(), &methods); err != nil {
t.Fatal(err)
} else if !s.Invoked {
t.Fatal("expected s to be invoked")
} else if diff := cmp.Diff(methods, []recurly.ShippingMethod{*NewTestShippingMethod()}); diff != "" {
t.Fatal(diff)
}
}
if invocations != 1 {
t.Fatalf("unexpected number of invocations: %d", invocations)
}
}
// Returns a ShippingMethod corresponding to testdata/shipping_method.xml.
func NewTestShippingMethod() *recurly.ShippingMethod {
return &recurly.ShippingMethod{
XMLName: xml.Name{Local: "shipping_method"},
Code: "fast_fast_fast",
Name: "Fast Fast Fast",
CreatedAt: recurly.NewTime(MustParseTime("2019-05-03T23:17:16Z")),
UpdatedAt: recurly.NewTime(MustParseTime("2019-05-03T23:17:16Z")),
}
}