This repository has been archived by the owner on Nov 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
verifier_test.go
177 lines (149 loc) · 4.82 KB
/
verifier_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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package processor
import (
"bytes"
"errors"
"fmt"
"net/http"
"testing"
"github.com/smartystreets/assertions/should"
"github.com/smartystreets/gunit"
)
func TestVerifierFixture(t *testing.T) {
gunit.Run(new(VerifierFixture), t)
}
type VerifierFixture struct {
*gunit.Fixture
client *FakeHTTPClient
verifier *SmartyVerifier
}
func (this *VerifierFixture) Setup() {
this.client = &FakeHTTPClient{}
this.verifier = NewSmartyVerifier(this.client)
}
func (this *VerifierFixture) TestRequestComposedProperly() {
input := AddressInput{
Street1: "Street1",
City: "City",
State: "State",
ZIPCode: "ZIPCode",
}
this.client.Configure("[{}]", http.StatusOK, nil)
this.verifier.Verify(input)
this.So(this.client.request.Method, should.Equal, "GET")
this.So(this.client.request.URL.Path, should.Equal, "/street-address")
this.AssertQueryStringValue("street", input.Street1)
this.AssertQueryStringValue("city", input.City)
this.AssertQueryStringValue("state", input.State)
this.AssertQueryStringValue("zipcode", input.ZIPCode)
}
func (this *VerifierFixture) AssertQueryStringValue(key, expected string) {
query := this.client.request.URL.Query()
this.So(query.Get(key), should.Equal, expected)
}
func (this *VerifierFixture) TestResponseParsed() {
this.client.Configure(rawJSONOutput, http.StatusOK, nil)
result := this.verifier.Verify(AddressInput{})
this.So(result.DeliveryLine1, should.Equal, "1 Santa Claus Ln")
this.So(result.LastLine, should.Equal, "North Pole AK 99705-9901")
this.So(result.City, should.Equal, "North Pole")
this.So(result.State, should.Equal, "AK")
this.So(result.ZIPCode, should.Equal, "99705")
}
const rawJSONOutput = `
[
{
"delivery_line_1": "1 Santa Claus Ln",
"last_line": "North Pole AK 99705-9901",
"components": {
"city_name": "North Pole",
"state_abbreviation": "AK",
"zipcode": "99705"
}
}
]`
func (this *VerifierFixture) TestMalformedJSONHandled() {
const malformedRawJSONOutput = `alert('Hello, World!' DROP TABLE Users);`
this.client.Configure(malformedRawJSONOutput, http.StatusOK, nil)
result := this.verifier.Verify(AddressInput{})
this.So(result.Status, should.Equal, "Invalid API Response")
}
func (this *VerifierFixture) TestHTTPErrorHandled() {
this.client.Configure("", 0, errors.New("GOPHERS!"))
result := this.verifier.Verify(AddressInput{})
this.So(result.Status, should.Equal, "Invalid API Response")
}
func (this *VerifierFixture) TestHTTPResponseBodyClosed() {
this.client.Configure(rawJSONOutput, http.StatusOK, nil)
this.verifier.Verify(AddressInput{})
this.So(this.client.responseBody.closed, should.Equal, 1)
}
func (this *VerifierFixture) TestAddressStatus() {
var (
deliverableJSON = buildAnalysisJSON("Y", "N", "Y")
missingSecondaryJSON = buildAnalysisJSON("D", "N", "Y")
droppedSecondaryJSON = buildAnalysisJSON("S", "N", "Y")
vacantJSON = buildAnalysisJSON("Y", "Y", "Y")
inactiveJSON = buildAnalysisJSON("Y", "N", "?")
invalidJSON = buildAnalysisJSON("N", "?", "?")
)
this.verifyAndAssertStatus(deliverableJSON, "Deliverable")
this.verifyAndAssertStatus(missingSecondaryJSON, "Deliverable")
this.verifyAndAssertStatus(droppedSecondaryJSON, "Deliverable")
this.verifyAndAssertStatus(vacantJSON, "Vacant")
this.verifyAndAssertStatus(inactiveJSON, "Inactive")
this.verifyAndAssertStatus(invalidJSON, "Invalid")
}
func (this *VerifierFixture) verifyAndAssertStatus(jsonResponse, expectedStatus string) {
this.client.Configure(jsonResponse, http.StatusOK, nil)
output := this.verifier.Verify(AddressInput{})
this.So(output.Status, should.Equal, expectedStatus)
}
func buildAnalysisJSON(match, vacant, active string) string {
template := `
[
{
"analysis": {
"dpv_match_code": "%s",
"dpv_vacant": "%s",
"active": "%s"
}
}
]`
return fmt.Sprintf(template, match, vacant, active)
}
///////////////////////////////////////////////////////////////
type FakeHTTPClient struct {
request *http.Request
response *http.Response
responseBody *VerifierSpyBuffer
err error
}
func (this *FakeHTTPClient) Configure(responseText string, statusCode int, err error) {
if err == nil {
this.responseBody = NewVerifierSpyBuffer(responseText)
this.response = &http.Response{
Body: this.responseBody,
StatusCode: statusCode,
}
}
this.err = err
}
func (this *FakeHTTPClient) Do(request *http.Request) (*http.Response, error) {
this.request = request
return this.response, this.err
}
///////////////////////////////////////////////////////////////
type VerifierSpyBuffer struct {
*bytes.Buffer
closed int
}
func NewVerifierSpyBuffer(value string) *VerifierSpyBuffer {
return &VerifierSpyBuffer{
Buffer: bytes.NewBufferString(value),
}
}
func (this *VerifierSpyBuffer) Close() error {
this.closed++
this.Buffer.Reset()
return nil
}