forked from AreaHQ/go-mailchimp
-
Notifications
You must be signed in to change notification settings - Fork 17
/
subscribe_test.go
95 lines (76 loc) · 2.91 KB
/
subscribe_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
88
89
90
91
92
93
94
95
package mailchimp_test
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
mailchimp "github.com/RichardKnop/go-mailchimp"
"github.com/RichardKnop/go-mailchimp/status"
"github.com/stretchr/testify/assert"
)
func TestSubscribeError(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(400)
rw.Header().Set("Content-Type", "application/json")
fmt.Fprint(rw, alreadySubscribedErrorResponse)
}))
defer server.Close()
transport := &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
return url.Parse(server.URL)
},
}
client, err := mailchimp.NewClient("the_api_key-us13", &http.Client{Transport: transport})
assert.NoError(t, err)
baseURL, _ := url.Parse("http://localhost/")
client.SetBaseURL(baseURL)
memberResponse, err := client.Subscribe("list_id", "[email protected]", map[string]interface{}{})
assert.Nil(t, memberResponse)
assert.Equal(t, "Error 400 Member Exists ( is already a list member. Use PUT to insert or update list members.)", err.Error())
errResponse, ok := err.(*mailchimp.ErrorResponse)
assert.True(t, ok)
assert.Equal(t, "Member Exists", errResponse.Title)
assert.Equal(t, 400, errResponse.Status)
assert.Equal(t, " is already a list member. Use PUT to insert or update list members.", errResponse.Detail)
}
func TestSubscribeMalformedError(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(500)
}))
defer server.Close()
transport := &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
return url.Parse(server.URL)
},
}
client, err := mailchimp.NewClient("the_api_key-us13", &http.Client{Transport: transport})
assert.NoError(t, err)
baseURL, _ := url.Parse("http://localhost/")
client.SetBaseURL(baseURL)
memberResponse, err := client.Subscribe("list_id", "[email protected]", map[string]interface{}{})
assert.Nil(t, memberResponse)
assert.Equal(t, "unexpected end of JSON input", err.Error())
}
func TestSubscribe(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(200)
rw.Header().Set("Content-Type", "application/json")
fmt.Fprint(rw, successResponse)
}))
defer server.Close()
transport := &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
return url.Parse(server.URL)
},
}
client, err := mailchimp.NewClient("the_api_key-us13", &http.Client{Transport: transport})
assert.NoError(t, err)
baseURL, _ := url.Parse("http://localhost/")
client.SetBaseURL(baseURL)
memberResponse, err := client.Subscribe("list_id", "[email protected]", map[string]interface{}{})
assert.NoError(t, err)
assert.Equal(t, "11bf13d1eb58116eba1de370b2bd796b", memberResponse.ID)
assert.Equal(t, "[email protected]", memberResponse.EmailAddress)
assert.Equal(t, status.Subscribed, memberResponse.Status)
}