forked from deis/deisrel
/
test_server.go
42 lines (35 loc) · 959 Bytes
/
test_server.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
package testutil
import (
"net/http"
"net/http/httptest"
"net/url"
"github.com/google/go-github/github"
)
// TestServer represents a test HTTP server along with a github.Client and Mux
type TestServer struct {
Server *httptest.Server
Client *github.Client
Mux *http.ServeMux
}
// NewTestServer 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 NewTestServer() *TestServer {
// test server
mux := http.NewServeMux()
server := httptest.NewServer(mux)
// github client configured to use test server
client := github.NewClient(nil)
url, _ := url.Parse(server.URL)
client.BaseURL = url
client.UploadURL = url
return &TestServer{
Server: server,
Client: client,
Mux: mux,
}
}
// Close closes the test HTTP server.
func (t *TestServer) Close() {
t.Server.Close()
}