-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi_test.go
More file actions
175 lines (162 loc) · 4.32 KB
/
api_test.go
File metadata and controls
175 lines (162 loc) · 4.32 KB
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
package aboutmyemail
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
const testServer = `http://127.0.0.1:3000/api/v1`
const testKey = `myemail_0LekAwu5Wob2kSru`
const sampleEmail = `From: <steve@blighty.com>
To: <steve@blighty.com>
Subject: test sausage
body
`
func TestApi(t *testing.T) {
client, err := New(WithServer(testServer), WithApiKey(testKey))
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
//ctx = context.Background()
token := "potato"
result, err := client.EmailWithResponse(ctx, EmailJSONRequestBody{
From: "steve@blighty.com",
Ip: "10.11.12.13",
Payload: sampleEmail,
To: "steve@blighty.com",
Token: &token,
})
if err != nil {
t.Errorf("client.Email() failed: %v", err)
return
}
if result.StatusCode() != 200 {
t.Logf("result=%v", string(result.Body))
t.Errorf("expected 200 response to Email, got %d %s", result.StatusCode(), result.Status())
return
}
if result.JSON200 == nil {
t.Errorf("expected non-nil success response")
return
}
id := result.JSON200.Id
for {
result, err := client.EmailStatusWithResponse(ctx, id)
if err != nil {
t.Errorf("client.EmailStatus() failed: %v", err)
return
}
if result.StatusCode() != 200 {
t.Logf("result=%v", string(result.Body))
t.Errorf("expected 200 response to EmailStatus, got %d %s", result.StatusCode(), result.Status())
return
}
if result.JSON200 == nil {
t.Errorf("expected non-nil success response")
return
}
if result.JSON200.Url != nil {
want := fmt.Sprintf(strings.TrimSuffix(testServer, "api/v1") + id)
got := *result.JSON200.Url
if want != got {
t.Errorf("url want '%s', got '%s'", want, got)
}
if result.JSON200.Id != id {
t.Errorf("id want '%s', got '%s'", id, result.JSON200.Id)
}
if result.JSON200.Token == nil {
t.Errorf("token want non-nil, got nil")
} else {
got := *result.JSON200.Token
want := token
if want != got {
t.Errorf("token want '%s', got '%s'", want, got)
}
}
break
}
time.Sleep(500 * time.Millisecond)
}
}
func TestApi_Callbacks(t *testing.T) {
client, err := New(WithServer(testServer), WithApiKey(testKey))
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
//ctx = context.Background()
token := "banana"
finishedChan := make(chan struct{})
var callbackBody []byte
messageCount := 0
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "message") {
messageCount++
w.WriteHeader(http.StatusNoContent)
return
}
if strings.HasSuffix(r.URL.Path, "finished") {
callbackBody, err = io.ReadAll(r.Body)
if err != nil {
panic(fmt.Errorf("failed to read callback body: %w", err))
}
w.WriteHeader(http.StatusNoContent)
close(finishedChan)
return
}
panic(fmt.Errorf("unrecognised query to callback server: %s", r.URL.Path))
}))
defer ts.Close()
finishedUrl := ts.URL + "/finished"
messageUrl := ts.URL + "/message"
result, err := client.EmailWithResponse(ctx, EmailJSONRequestBody{
From: "steve@blighty.com",
Ip: "10.11.12.13",
Payload: sampleEmail,
To: "steve@blighty.com",
Token: &token,
FinishedUrl: &finishedUrl,
ProgressUrl: &messageUrl,
})
if err != nil {
t.Errorf("client.Email() failed: %v", err)
return
}
select {
case <-ctx.Done():
t.Errorf("context timeout")
return
case <-finishedChan:
}
var finResult StatusResult
err = json.Unmarshal(callbackBody, &finResult)
if err != nil {
t.Logf("callback body:\n%s\n", string(callbackBody))
t.Errorf("failed to unmarshal callback result: %s", err)
}
if finResult.Url == nil {
t.Errorf("Wanted non-nil result url, got nil")
} else {
wantUrl := fmt.Sprintf(strings.TrimSuffix(testServer, "api/v1") + result.JSON200.Id)
gotUrl := *finResult.Url
if wantUrl != gotUrl {
t.Errorf("Finished URL want '%s', got '%s'", wantUrl, gotUrl)
}
}
if finResult.Token == nil {
t.Errorf("Wanted non-nil result token, got nil")
} else {
got := *finResult.Token
if got != token {
t.Errorf("Token want '%s', got '%s", token, got)
}
}
}