forked from messagebird/go-rest-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
76 lines (64 loc) · 1.73 KB
/
main_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
package messagebird
import (
"crypto/tls"
"log"
"net"
"net/http"
"net/http/httptest"
"os"
"testing"
)
var mbClient *Client
var mbServer *httptest.Server
var mbServerResponseCode int
var mbServerResponseBody []byte
var accessKeyErrorObject = []byte(`{
"errors":[
{
"code":2,
"description":"Request not allowed (incorrect access_key)",
"parameter":"access_key"
}
]
}`)
// startFauxServer sets up a fake HTTPS server which the testing client will
// connect to, instead of the actual https://rest.messagebird.com URL.
func startFauxServer() {
mbServer = httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(mbServerResponseCode)
w.Header().Set("Content-Type", "application/json")
w.Write(mbServerResponseBody)
}))
transport := &http.Transport{
DialTLS: func(netw, addr string) (net.Conn, error) {
conn, err := tls.Dial(netw, mbServer.Listener.Addr().String(), &tls.Config{InsecureSkipVerify: true})
if err != nil {
return nil, err
}
return conn, nil
},
}
mbClient = &Client{
AccessKey: "test_gshuPaZoeEG6ovbc8M79w0QyM",
HTTPClient: &http.Client{Transport: transport},
}
if testing.Verbose() {
mbClient.DebugLog = log.New(os.Stdout, "DEBUG", log.Lshortfile)
}
}
// stopFauxServer is called after testing is done and stops the fake HTTPS
// server.
func stopFauxServer() {
mbServer.Close()
}
func TestMain(m *testing.M) {
startFauxServer()
exitCode := m.Run()
stopFauxServer()
os.Exit(exitCode)
}
// SetServerResponse sets the response and HTTP status code the fake
// MessageBird server should return.
func SetServerResponse(statusCode int, response []byte) {
mbServerResponseCode, mbServerResponseBody = statusCode, response
}