Skip to content

Commit 3fc804a

Browse files
committed
Record last request to test server
1 parent 5be64f8 commit 3fc804a

File tree

1 file changed

+52
-6
lines changed

1 file changed

+52
-6
lines changed

internal/messagebirdtest/test_server.go

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
11
package messagebirdtest
22

33
import (
4+
"io/ioutil"
45
"net/http"
56
"net/http/httptest"
7+
"net/url"
68
"os"
79
"testing"
810
)
911

12+
type resetFunc func()
13+
14+
type request struct {
15+
Body []byte
16+
Method string
17+
URL *url.URL
18+
}
19+
20+
// Request contains the lastly received http.Request by the fake server.
21+
var Request request
22+
1023
var server *httptest.Server
1124

1225
var responseBody []byte
13-
var status int = 200
26+
var status int
1427

1528
// EnableServer starts a fake server, runs the test and closes the server.
1629
func EnableServer(m *testing.M) {
@@ -23,6 +36,18 @@ func EnableServer(m *testing.M) {
2336

2437
func initAndStartServer() {
2538
server = httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
39+
Request = request{
40+
Method: r.Method,
41+
URL: r.URL,
42+
}
43+
44+
var err error
45+
46+
Request.Body, err = ioutil.ReadAll(r.Body)
47+
if err != nil {
48+
panic(err.Error())
49+
}
50+
2651
// status and responseBody are defined in returns.go.
2752
w.Header().Set("Content-Type", "application/json")
2853
w.WriteHeader(status)
@@ -36,20 +61,41 @@ func closeServer() {
3661
server.Close()
3762
}
3863

64+
// setRequest sets some basic fields from the http.Request in our global Request
65+
// struct.
66+
func setRequest(r *http.Request) error {
67+
Request := request{
68+
Method: r.Method,
69+
URL: r.URL,
70+
}
71+
72+
var err error
73+
74+
// Reading from the request body is fine, as it's not used elsewhere.
75+
// Server always returns fake data/testdata.
76+
Request.Body, err = ioutil.ReadAll(r.Body)
77+
78+
return err
79+
}
80+
81+
func WillReturn(b []byte, s int) {
82+
responseBody = b
83+
status = s
84+
}
85+
3986
// WillReturnTestdata sets the status (s) for the test server to respond with.
4087
// Additionally it reads the bytes from the relativePath file and returns that
4188
// for requests. It fails the test if the file can not be read. The path is
4289
// relative to the testdata directory (the go tool ignores directories named
4390
// "testdata" in test packages: https://golang.org/cmd/go/#hdr-Test_packages).
4491
func WillReturnTestdata(t *testing.T, relativePath string, s int) {
45-
responseBody = testdata(t, relativePath)
46-
status = s
92+
WillReturn(Testdata(t, relativePath), s)
4793
}
4894

4995
// WillReturnAccessKeyError sets the response body and status for requests to
5096
// indicate the request is not allowed due to an incorrect access key.
5197
func WillReturnAccessKeyError() {
52-
responseBody = []byte(`
98+
WillReturn([]byte(`
5399
{
54100
"errors": [
55101
{
@@ -58,6 +104,6 @@ func WillReturnAccessKeyError() {
58104
"parameter":"access_key"
59105
}
60106
]
61-
}`)
62-
status = 401
107+
}
108+
`), 401)
63109
}

0 commit comments

Comments
 (0)