-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathneverbounce_test.go
151 lines (135 loc) · 5.17 KB
/
neverbounce_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package neverbounce_test
import (
"github.com/NeverBounce/NeverBounceApi-Go"
"github.com/NeverBounce/NeverBounceApi-Go/models"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/jarcoal/httpmock"
"os"
"testing"
)
func TestNeverBounceApiGo(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "NeverBounceApiGo Suite")
}
var _ = Describe("NeverBounce", func() {
Describe("MakeRequest", func() {
It("should return an error when the content-type mismatches", func() {
response := httpmock.NewStringResponse(200, ``)
response.Header.Set("content-type", "text/html")
// mock the root info API
httpmock.RegisterResponder("GET", "https://api.neverbounce.com/v4.2/account/info",
httpmock.ResponderFromResponse(response))
neverBounce := neverbounce.New("apiKey")
resp, err := neverBounce.Account.Info()
Expect(resp).To(BeNil())
Expect(err).NotTo(BeNil())
if nbError, ok := err.(*neverbounce.Error); ok {
Expect(nbError.Message).To(Equal("The API responded with a datatype of \"text/html" +
"\", but \"application/json\" was expected." +
"\n\n(Internal error [status 200])"))
}
})
It("should not return an error when requesting job/download with non json response", func() {
response := httpmock.NewStringResponse(200, ``)
response.Header.Set("content-type", "text/html")
// mock the root info API
httpmock.RegisterResponder("GET", "https://api.neverbounce.com/v4.2/jobs/download",
httpmock.ResponderFromResponse(response))
neverBounce := neverbounce.New("apiKey")
err := neverBounce.Jobs.Download(&nbModels.JobsDownloadRequestModel{
JobID: 296050,
EmailStatusAsInt: true,
}, "test.csv")
Expect(err).To(BeNil())
err2 := os.Remove("test.csv")
if err2 != nil {
panic(err2)
}
})
It("should return an error when the response is empty and content-type matches", func() {
response := httpmock.NewStringResponse(200, ``)
response.Header.Set("content-type", "application/json")
// mock the root info API
httpmock.RegisterResponder("GET", "https://api.neverbounce.com/v4.2/account/info",
httpmock.ResponderFromResponse(response))
neverBounce := neverbounce.New("apiKey")
resp, err := neverBounce.Account.Info()
Expect(resp).To(BeNil())
Expect(err).NotTo(BeNil())
if nbError, ok := err.(*neverbounce.Error); ok {
Expect(nbError.Message).To(Equal("We were unable to parse the API response. " +
"The following information was supplied: " +
"\n\n(Internal error [status 200])"))
}
})
It("should return an error during a 404", func() {
// mock the root info API
httpmock.RegisterResponder("GET", "https://api.neverbounce.com/v4.2/account/info",
httpmock.NewStringResponder(404, `404 Not Found`))
neverBounce := neverbounce.New("apiKey")
resp, err := neverBounce.Account.Info()
Expect(resp).To(BeNil())
Expect(err).NotTo(BeNil())
if nbError, ok := err.(*neverbounce.Error); ok {
Expect(nbError.Message).To(Equal("We were unable to complete your request. " +
"The following information was supplied: 404 Not Found" +
"\n\n(Request error [status 404])"))
}
})
It("should return an error during a 503", func() {
// mock the root info API
httpmock.RegisterResponder("GET", "https://api.neverbounce.com/v4.2/account/info",
httpmock.NewStringResponder(503, `503 Temporarily Unavailable`))
neverBounce := neverbounce.New("apiKey")
resp, err := neverBounce.Account.Info()
Expect(resp).To(BeNil())
Expect(err).NotTo(BeNil())
if nbError, ok := err.(*neverbounce.Error); ok {
Expect(nbError.Message).To(Equal("We were unable to complete your request. " +
"The following information was supplied: 503 Temporarily Unavailable" +
"\n\n(Internal error [status 503])"))
}
})
It("should return an error when status isn't 'success'", func() {
response := httpmock.NewStringResponse(200, `{
"status": "general_failure",
"message": "Something went wrong",
"execution_time": 300
}`)
response.Header.Set("content-Type", "application/json")
// mock the root info API
httpmock.RegisterResponder("GET", "https://api.neverbounce.com/v4.2/account/info",
httpmock.ResponderFromResponse(response))
neverBounce := neverbounce.New("apiKey")
body, err := neverBounce.Account.Info()
Expect(body).To(BeNil())
Expect(err).NotTo(BeNil())
if nbError, ok := err.(*neverbounce.Error); ok {
Expect(nbError.Message).To(Equal("We were unable to complete your request. " +
"The following information was supplied: Something went wrong" +
"\n\n(general_failure)"))
}
})
})
})
var _ = BeforeSuite(func() {
// block all HTTP requests
httpmock.Activate()
// mock the root info API
httpmock.RegisterResponder("GET", "https://api.neverbounce.com/v4.2/account/info",
httpmock.NewStringResponder(200, `{
"status": "success",
"result": "valid",
"flags": [
"has_dns",
"has_dns_mx"
],
"suggested_correction": "",
"retry_token": "",
"execution_time": 499
}`))
})
var _ = AfterSuite(func() {
httpmock.DeactivateAndReset()
})