-
Notifications
You must be signed in to change notification settings - Fork 0
/
receiver_web_test.go
136 lines (119 loc) · 4.22 KB
/
receiver_web_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
package main
import (
"fmt"
"io"
"net"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
const (
ipv4Addr = "1.2.3.4"
)
func makeReq(t *testing.T, s *httptest.Server, method, path string, h http.Header) *http.Response {
req, err := http.NewRequest(method, s.URL+path, nil)
if err != nil {
t.Fatalf("Failed to create HTTP request: %v", err)
}
req.Header = h
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
t.Fatalf("Failed to make HTTP request: %v", err)
}
return resp
}
func TestIndexRequest(t *testing.T) {
inbox := make(chan serializer, 10) // We're using a buffered channel to prevent a deadlock.
srv := httptest.NewServer(newRouter(inbox))
defer srv.Close()
resp := makeReq(t, srv, http.MethodGet, "/", nil)
if resp.StatusCode != http.StatusOK {
t.Fatalf("Expected HTTP status code %d but got %d.", http.StatusOK, resp.StatusCode)
}
body, _ := io.ReadAll(resp.Body)
if strings.TrimSpace(string(body)) != indexPage {
t.Fatalf("Expected body %q but got %q.", indexPage, string(body))
}
}
func TestGoodRequest(t *testing.T) {
walletID := newV4(t)
expected := clientRequest{
Addr: net.ParseIP(ipv4Addr),
Wallet: walletID,
}
inbox := make(chan serializer, 10) // We're using a buffered channel to prevent a deadlock.
path := fmt.Sprintf("/v2/confirmation/token/%s", walletID)
srv := httptest.NewServer(newRouter(inbox))
defer srv.Close()
resp := makeReq(t, srv, http.MethodGet, path, http.Header{fastlyClientIP: []string{ipv4Addr}})
if resp.StatusCode != http.StatusOK {
t.Fatalf("Expected HTTP status code %d but got %d.", http.StatusOK, resp.StatusCode)
}
r := <-inbox
received := r.(*clientRequest)
if !net.IP.Equal(received.Addr, expected.Addr) {
t.Fatalf("Expected address %q but got %q.", expected.Addr, received.Addr)
}
if received.Wallet != expected.Wallet {
t.Fatalf("Expected wallet %q but got %q.", expected.Wallet, received.Wallet)
}
}
func TestBadWalletId(t *testing.T) {
srv := httptest.NewServer(newRouter(make(chan serializer)))
defer srv.Close()
badPath := "/v2/confirmation/token/foobar"
resp := makeReq(t, srv, http.MethodGet, badPath, http.Header{fastlyClientIP: []string{ipv4Addr}})
if resp.StatusCode != http.StatusBadRequest {
t.Fatalf("Expected HTTP status code %d but got %d.", http.StatusBadRequest, resp.StatusCode)
}
body, _ := io.ReadAll(resp.Body)
received := strings.TrimSpace(string(body))
expected := errBadWalletFmt.Error()
if received != expected {
t.Fatalf("Expected error %q but got %q.", expected, received)
}
}
func TestNoFastlyHeader(t *testing.T) {
srv := httptest.NewServer(newRouter(make(chan serializer)))
defer srv.Close()
path := fmt.Sprintf("/v2/confirmation/token/%s", newV4(t))
resp := makeReq(t, srv, http.MethodGet, path, http.Header{})
if resp.StatusCode != http.StatusBadRequest {
t.Fatalf("Expected HTTP status code %d but got %d.", http.StatusBadRequest, resp.StatusCode)
}
body, _ := io.ReadAll(resp.Body)
received := strings.TrimSpace(string(body))
expected := errNoFastlyHeader.Error()
if received != expected {
t.Fatalf("Expected error %q but got %q.", expected, received)
}
}
func TestBadFastlyAddr(t *testing.T) {
srv := httptest.NewServer(newRouter(make(chan serializer)))
defer srv.Close()
path := fmt.Sprintf("/v2/confirmation/token/%s", newV4(t))
resp := makeReq(t, srv, http.MethodGet, path, http.Header{fastlyClientIP: []string{"foo"}})
if resp.StatusCode != http.StatusBadRequest {
t.Fatalf("Expected HTTP status code %d but got %d.", http.StatusBadRequest, resp.StatusCode)
}
body, _ := io.ReadAll(resp.Body)
received := strings.TrimSpace(string(body))
expected := errBadFastlyAddrFormat.Error()
if received != expected {
t.Fatalf("Expected error %q but got %q.", expected, received)
}
}
func TestIsValidApiVersion(t *testing.T) {
assertEqual(t, isValidApiVersion("1"), true)
assertEqual(t, isValidApiVersion("2"), true)
assertEqual(t, isValidApiVersion("3"), true)
assertEqual(t, isValidApiVersion("4"), true)
assertEqual(t, isValidApiVersion("0"), false)
assertEqual(t, isValidApiVersion("5"), false)
assertEqual(t, isValidApiVersion("99"), false)
assertEqual(t, isValidApiVersion("-1"), false)
assertEqual(t, isValidApiVersion("1.1"), false)
assertEqual(t, isValidApiVersion("foo"), false)
}