-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_helpers.go
73 lines (58 loc) · 1.77 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
// +build test
package main
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
log "github.com/sirupsen/logrus"
)
const fakeAuthB64 = "ZmFrZS1zY3JpcHQ6ZmFrZS1rZXk="
func init() {
log.SetLevel(log.PanicLevel)
}
func mockShotgun(code int, body string) (*httptest.Server, *Shotgun, clientConfig) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(code)
w.Header().Set("Content-Type", "application/json")
fmt.Fprintln(w, body)
}))
transport := &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
return url.Parse(server.URL)
},
}
httpClient := &http.Client{Transport: transport}
// Update SG_HOST so that the middleware doesn't break during tesing.
// SG_HOST = server.URL
var client *Shotgun
client = &Shotgun{
ServerURL: server.URL,
ScriptName: "fake-script",
ScriptKey: "fake-key",
client: *httpClient,
}
config := newClientConfig("0.0.0-test.1", server.URL)
return server, client, config
}
func getRequest(path string) *http.Request {
req, _ := http.NewRequest("GET", path, nil)
req.Header.Set("Authorization", fmt.Sprintf("Basic %s", fakeAuthB64))
return req
}
func postRequest(path, body string) *http.Request {
req, _ := http.NewRequest("POST", path, bytes.NewReader([]byte(body)))
req.Header.Set("Authorization", fmt.Sprintf("Basic %s", fakeAuthB64))
return req
}
func deleteRequest(path string) *http.Request {
req, _ := http.NewRequest("DELETE", path, nil)
req.Header.Set("Authorization", fmt.Sprintf("Basic %s", fakeAuthB64))
return req
}
func patchRequest(path, body string) *http.Request {
req, _ := http.NewRequest("PATCH", path, bytes.NewReader([]byte(body)))
req.Header.Set("Authorization", fmt.Sprintf("Basic %s", fakeAuthB64))
return req
}