-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.go
56 lines (46 loc) · 1.44 KB
/
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
package apiclient
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
)
var (
mux *http.ServeMux
server *httptest.Server
)
// setupTestServer create a local http server which mimics the endpoints
// of the SeedDMS RestAPI
// This function creates to endpoints which are use in many other test
// All other endpoints need to be created when they are used for testing
// The function returns the api client and teardown function to shutdown
// the server
func setupTestServer() (*Client, func()) {
mux = http.NewServeMux()
server = httptest.NewServer(mux)
// Create a client using the test server and a random apikey
client := Connect(server.URL, "apikey")
// fmt.Print(server.URL)
mux.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
// ... return the JSON
fmt.Fprint(w, fixture("login.json"))
})
mux.HandleFunc("/account", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
// ... return the JSON
fmt.Fprint(w, fixture("account.json"))
})
return client, func() {
server.Close()
}
}
func fixture(path string) string {
b, err := ioutil.ReadFile("testdata/fixtures/" + path)
if err != nil {
panic(err)
}
return string(b)
}