-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfake_http.go
58 lines (53 loc) · 1.39 KB
/
fake_http.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
package api
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
)
// FakeHTTP provides a mechanism by which to stub HTTP responses through
type FakeHTTP struct {
// Requests stores references to sequental requests that RoundTrip has received
Requests []*http.Request
count int
responseStubs []*http.Response
}
// StubResponse pre-records an HTTP response
func (f *FakeHTTP) StubResponse(status int, body io.Reader) {
resp := &http.Response{
StatusCode: status,
Body: ioutil.NopCloser(body),
}
f.responseStubs = append(f.responseStubs, resp)
}
// RoundTrip satisfies http.RoundTripper
func (f *FakeHTTP) RoundTrip(req *http.Request) (*http.Response, error) {
if len(f.responseStubs) <= f.count {
return nil, fmt.Errorf("FakeHTTP: missing response stub for request %d", f.count)
}
resp := f.responseStubs[f.count]
f.count++
resp.Request = req
f.Requests = append(f.Requests, req)
return resp, nil
}
func (f *FakeHTTP) StubRepoResponse(owner, repo string) {
body := bytes.NewBufferString(fmt.Sprintf(`
{ "data": { "repo_000": {
"id": "REPOID",
"name": "%s",
"owner": {"login": "%s"},
"defaultBranchRef": {
"name": "master",
"target": {"oid": "deadbeef"}
},
"viewerPermission": "WRITE"
} } }
`, repo, owner))
resp := &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(body),
}
f.responseStubs = append(f.responseStubs, resp)
}