-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.go
55 lines (47 loc) · 1.28 KB
/
util.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
package coda
import (
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
func buildTestClient(testServerUrl string) *Client {
httpClient := &http.Client{
Transport: &transport{
defaultTransport: http.DefaultTransport,
token: "fakeKey",
},
}
u, _ := url.Parse(testServerUrl)
codaClient := &Client{
UserAgent: "golang_bot/1.0",
HttpClient: httpClient,
BaseURL: u,
}
return codaClient
}
func buildTestServer(expectedPath, sampleDataPath string, expectedStatus int, t *testing.T) *httptest.Server {
return buildTestServerFull(expectedPath, sampleDataPath, expectedStatus, "", t)
}
func buildTestServerFull(expectedPath, sampleDataPath string, expectedStatus int, expectedBody string, t *testing.T) *httptest.Server {
// load data from filepath
data, err := ioutil.ReadFile(sampleDataPath)
if err != nil {
panic(err)
}
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
if expectedBody != "" {
bodyBytes, err := ioutil.ReadAll(req.Body)
if err != nil {
panic(err)
}
assert.Equal(t, string(bodyBytes), expectedBody)
}
assert.Equal(t, req.URL.String(), expectedPath)
rw.WriteHeader(expectedStatus)
rw.Write(data)
}))
return server
}