This repository has been archived by the owner on May 21, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmock_test.go
127 lines (93 loc) · 3.83 KB
/
mock_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
package polynym
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
)
// mockHTTP for mocking requests
type mockHTTP struct{}
// Do is a mock http request
func (m *mockHTTP) Do(req *http.Request) (*http.Response, error) {
resp := new(http.Response)
resp.StatusCode = http.StatusBadRequest
if req == nil {
return resp, fmt.Errorf("missing request")
}
if strings.Contains(req.URL.String(), "/19gKzz8XmFDyrpk4qFobG7qKoqybe78v9h") {
// Valid BSV Address
resp.StatusCode = http.StatusOK
resp.Body = validResponse("19gKzz8XmFDyrpk4qFobG7qKoqybe78v9h", req.URL.String(), resp.StatusCode)
} else if strings.Contains(req.URL.String(), "/error") {
// Return an error
return resp, fmt.Errorf("req error")
} else if strings.Contains(req.URL.String(), "/bad-poly-response") {
// Return a bad error response from Polynym (empty)
resp.Body = invalidResponse("", req.URL.String(), resp.StatusCode)
} else if strings.Contains(req.URL.String(), "/bad-poly-status") {
// Return a bad error response from Polynym (empty)
resp.Body = invalidResponse("Some error message", req.URL.String(), http.StatusUnavailableForLegalReasons)
} else if strings.Contains(req.URL.String(), "/16ZqP5Tb22KJuvSAbjNkoiZs13mmRmexZA") {
// Valid BSV Address
resp.StatusCode = http.StatusOK
resp.Body = validResponse("16ZqP5Tb22KJuvSAbjNkoiZs13mmRmexZA", req.URL.String(), resp.StatusCode)
} else if strings.Contains(req.URL.String(), "/c6ZqP5Tb22KJuvSAbjNkoi") {
// Invalid BSV Address
resp.Body = invalidResponse("Unable to resolve to address", req.URL.String(), resp.StatusCode)
} else if strings.Contains(req.URL.String(), "/1doesnotexisthandle") {
// Invalid handle
resp.Body = invalidResponse("1handle not found", req.URL.String(), resp.StatusCode)
} else if strings.Contains(req.URL.String(), "/[email protected]") {
// Invalid handle
resp.Body = invalidResponse("$handle not found", req.URL.String(), resp.StatusCode)
} else if strings.Contains(req.URL.String(), "/[email protected]") {
// Invalid paymail
resp.Body = invalidResponse("PayMail not found", req.URL.String(), resp.StatusCode)
} else if strings.Contains(req.URL.String(), "/[email protected]") {
// Valid 1handle
resp.StatusCode = http.StatusOK
resp.Body = validResponse("1Lti3s6AQNKTSgxnTyBREMa6XdHLBnPSKa", req.URL.String(), resp.StatusCode)
} else if strings.Contains(req.URL.String(), "/[email protected]") {
// Valid $handle / paymail
resp.StatusCode = http.StatusOK
resp.Body = validResponse("124dwBFyFtkcNXGfVWQroGcT9ybnpQ3G3Z", req.URL.String(), resp.StatusCode)
} else if strings.Contains(req.URL.String(), "/[email protected]") {
// Valid paymail
resp.StatusCode = http.StatusOK
resp.Body = validResponse("19gKzz8XmFDyrpk4qFobG7qKoqybe78v9h", req.URL.String(), resp.StatusCode)
} else if strings.Contains(req.URL.String(), "/@833") {
// Valid Twetch ID
resp.StatusCode = http.StatusOK
resp.Body = validResponse("19ksW6ueSw9nEj88X3QNJ9VkKPGf1zuKbQ", req.URL.String(), resp.StatusCode)
}
return resp, nil
}
// validResponse returns a valid polynym response
func validResponse(address, url string, status int) io.ReadCloser {
result := &GetAddressResponse{
Address: address,
LastRequest: &LastRequest{
Method: http.MethodGet,
StatusCode: status,
URL: url,
},
}
b, _ := json.Marshal(result) // nolint: errchkjson // used in testing
return ioutil.NopCloser(bytes.NewBuffer(b))
}
// invalidResponse returns an invalid polynym response (error)
func invalidResponse(errorMessage, url string, status int) io.ReadCloser {
result := &GetAddressResponse{
ErrorMessage: errorMessage,
LastRequest: &LastRequest{
Method: http.MethodGet,
StatusCode: status,
URL: url,
},
}
b, _ := json.Marshal(result) // nolint: errchkjson // used in testing
return ioutil.NopCloser(bytes.NewBuffer(b))
}