-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsendpulse_test.go
63 lines (53 loc) · 1.37 KB
/
sendpulse_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
package sendpulse_sdk_go
import (
"fmt"
"github.com/stretchr/testify/suite"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
type SendpulseTestSuite struct {
suite.Suite
server *httptest.Server
client *Client
mux *http.ServeMux
}
// Rewrites scheme to http to avoid TLS cert issues
type RewriteTransport struct {
Transport http.RoundTripper
}
func (t *RewriteTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.URL.Scheme = "http"
if t.Transport == nil {
return http.DefaultTransport.RoundTrip(req)
}
return t.Transport.RoundTrip(req)
}
func (suite *SendpulseTestSuite) BeforeTest(suiteName, testName string) {
suite.mux = http.NewServeMux()
suite.mux.HandleFunc("/oauth/access_token", func(w http.ResponseWriter, r *http.Request) {
suite.Equal(http.MethodPost, r.Method)
fmt.Fprintf(w, `{"access_token": "12345"}`)
})
suite.server = httptest.NewServer(suite.mux)
transport := &RewriteTransport{&http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
return url.Parse(suite.server.URL)
},
}}
httpClient := &http.Client{
Transport: transport,
}
config := &Config{
UserID: "uid",
Secret: "secret",
}
suite.client = NewClient(httpClient, config)
}
func (suite *SendpulseTestSuite) AfterTest(suiteName, testName string) {
suite.server.Close()
}
func TestSuite(t *testing.T) {
suite.Run(t, new(SendpulseTestSuite))
}