-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathmailchimp_test.go
71 lines (61 loc) · 1.75 KB
/
mailchimp_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
package mailchimp
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"testing"
)
func equals(t *testing.T, a interface{}, b interface{}) {
if a != b {
t.Errorf("Expected %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
}
}
func TestBadAPIKey(t *testing.T) {
_, err := NewClient("asz", nil)
if err == nil {
t.Fail()
}
}
func TestURL(t *testing.T) {
client, _ := NewClient("a-lit11", nil)
equals(t, "https://lit11.api.mailchimp.com/3.0", client.BaseURL.String())
}
func TestSubscribeError(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, _ := NewClient("a-lit11", &http.Client{Transport: transport})
client.BaseURL, _ = url.Parse("http://localhost/")
_, err := client.Subscribe("me@matthewbrown.io", "abc_test")
if err == nil {
t.Fatal(err)
}
equals(t, err.Error(), "Error 0 ()")
}
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.Fprintln(rw, `{"email":"bob@example.com","status":"sent","reject_reason":"hard-bounce","_id":"1"}`)
}))
defer server.Close()
transport := &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
return url.Parse(server.URL)
},
}
client, _ := NewClient("a-lit11", &http.Client{Transport: transport})
client.BaseURL, _ = url.Parse("http://localhost/")
_, err := client.Subscribe("me@matthewbrown.io", "abc_test")
if err != nil {
t.Fatal(err)
}
}