-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
client_test.go
94 lines (76 loc) · 2.2 KB
/
client_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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package gocorona_test
import (
"bytes"
"flag"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
gocorona "github.com/itsksaurabh/go-corona"
"github.com/pkg/errors"
)
var (
testServer string
testDataDir = "./testdata/"
updateTestData = flag.Bool("update", false, "if set then update testdata else use saved testdata for testing.")
)
func TestMain(m *testing.M) {
flag.Parse()
// Run testServer for unit tests
if !*updateTestData {
server := httptest.NewServer(http.FileServer(http.Dir(testDataDir)))
defer server.Close()
testServer = server.URL
}
os.Exit(m.Run())
return
}
// testClient returns a gocorna.Client mainly for testing purposes.
// It behaves as a reverse proxy agent, it reads testfile and
// returns data as a response to requests made through it.
//
// If update flag is set, it will save the real data to testfile.
func testClient(t *testing.T) gocorona.Client {
c := gocorona.Client{
HTTP: &http.Client{},
}
c.HTTP.Transport = &loaderTransport{t}
if *updateTestData {
c.HTTP.Transport = &saverTransport{t}
}
return c
}
// saverTransport saves response body to testdata file
type saverTransport struct{ t *testing.T }
func (st saverTransport) RoundTrip(r *http.Request) (*http.Response, error) {
resp, err := http.DefaultTransport.RoundTrip(r)
if err != nil {
return resp, errors.Wrap(err, "request failed")
}
if resp.StatusCode != http.StatusOK {
return resp, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return resp, errors.Wrap(err, "read body failed")
}
resp.Body = ioutil.NopCloser(bytes.NewBuffer(body))
err = ioutil.WriteFile(testDataDir+filename(st.t), body, 0644)
return resp, errors.Wrap(err, "write file failed")
}
// loaderTransport loads response from testdata file
type loaderTransport struct{ t *testing.T }
func (lt loaderTransport) RoundTrip(r *http.Request) (*http.Response, error) {
return http.Get(testServer + "/" + filename(lt.t))
}
// returns filename for saving/loading response JSON
func filename(t *testing.T) string {
name := t.Name()
if strings.Contains(name, "/") { // If a subtest
name = strings.Replace(name, "/", "_", -1)
}
name = strings.TrimPrefix(name, "Test")
return name + ".json"
}