-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_helpers.go
114 lines (99 loc) · 2.68 KB
/
test_helpers.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package gofamilysearch
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"regexp"
"strings"
"testing"
)
var (
// mux is the HTTP request multiplexer used with the test server.
testMux *http.ServeMux
// server is a test HTTP server used to provide mock API responses.
testServer *httptest.Server
)
// setup sets up a test HTTP server along with a github.Client that is
// configured to talk to that test server. Tests should register handlers on
// mux which provide mock responses for the API method being tested.
func testSetup() {
// test server
testMux = http.NewServeMux()
testServer = httptest.NewServer(testMux)
}
// teardown closes the test HTTP server.
func testTeardown() {
testServer.Close()
}
func testMethod(t *testing.T, r *http.Request, want string) {
if want != r.Method {
t.Errorf("Request method = %v, want %v", r.Method, want)
}
}
func testQueryParameters(t *testing.T, r *http.Request, want map[string]string) {
r.ParseForm()
eq := true
if want == nil {
eq = len(r.Form) == 0
} else if len(r.Form) != len(want) {
eq = false
} else {
for k, v := range want {
if r.FormValue(k) != v {
eq = false
break
}
}
}
if !eq {
t.Errorf("Query Parameters = %+v, want %+v", r.Form, want)
}
}
func testHeader(t *testing.T, r *http.Request, header string, want string) {
if value := r.Header.Get(header); want != value {
t.Errorf("Header %s = %s, want: %s", header, value, want)
}
}
var charsToTranslate = regexp.MustCompile("[./]")
func testRespond(t *testing.T, method string, url string, queryParams map[string]string, filename string) {
if filename == "" {
filename = charsToTranslate.ReplaceAllString(url[1:], "_") + ".json"
}
testMux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, method)
testQueryParameters(t, r, queryParams)
contents, err := readFile("test_responses/" + filename)
if err != nil {
panic(err)
}
fmt.Fprintf(w, string(contents))
})
}
func testClient() *Client {
contents, err := readFile("test_responses/_well-known_app-meta.json")
if err != nil {
panic(err)
}
response := &discoveryResponse{}
if err := json.Unmarshal(contents, response); err != nil {
panic(err)
}
templates, err := generateTemplates(testServer.URL, response)
if err != nil {
panic(err)
}
// need to replace all hosts with our testServer.URL
for k, v := range templates {
v = strings.Replace(v, "https://sandbox.familysearch.org", testServer.URL, 1)
v = strings.Replace(v, "https://integration.familysearch.org", testServer.URL, 1)
templates[k] = v
}
return &Client{
AccessToken: "accessToken",
Transport: http.DefaultTransport,
Context: &Context{
templates: templates,
},
}
}