This repository has been archived by the owner on Oct 4, 2019. It is now read-only.
/
gock.go
101 lines (86 loc) · 2.34 KB
/
gock.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
package test
import (
"crypto/sha1"
"encoding/json"
"fmt"
gogh "github.com/google/go-github/github"
"github.com/stretchr/testify/require"
"gopkg.in/h2non/gock.v1"
"math/rand"
"net/http"
"strings"
"testing"
)
const gitHubNotFound = `{
"message": "Not Found",
"documentation_url": "https://developer.github.com/v3/...."
}`
func MockGHHeadCalls(prjPath, branch string, files SliceOfStrings, modifiers ...GockModifier) {
for _, file := range files() {
mock := gock.New(host).
Head(fmt.Sprintf("/%s/blob/%s/%s", prjPath, branch, file))
for _, modify := range modifiers {
modify(mock)
}
mock.Reply(200)
}
}
func MockGHGetApiCalls(t *testing.T, prjPath, branch string, files, langs SliceOfStrings, modifiers ...GockModifier) {
var entries []gogh.TreeEntry
for _, file := range files() {
entries = append(entries, gogh.TreeEntry{
SHA: sha(file),
Path: String(file),
})
}
tree := gogh.Tree{
SHA: sha(files()...),
Truncated: Boolean(false),
Entries: entries,
}
bytes, err := json.Marshal(tree)
require.NoError(t, err)
newApiMock(fmt.Sprintf("/repos/%s/git/trees/%s", prjPath, branch), bytes, modifiers...)
languages := map[string]int{}
for _, lang := range langs() {
languages[lang] = rand.Int()
}
bytes, err = json.Marshal(languages)
require.NoError(t, err)
newApiMock(fmt.Sprintf("/repos/%s/languages", prjPath), bytes, modifiers...)
}
func newApiMock(path string, bytes []byte, modifiers ...GockModifier) {
treeMock := gock.New("https://api.github.com").
Get(path)
for _, modify := range modifiers {
modify(treeMock)
}
treeMock.Reply(200).
BodyString(string(bytes))
}
func MockNotFoundGitHub(repoIdentifier string) {
gock.New("https://api.github.com").
Get(fmt.Sprintf("/repos/%s/.*", repoIdentifier)).
Times(2).
Reply(404).
BodyString(gitHubNotFound)
}
type GockModifier func(mock *gock.Request)
func sha(files ...string) *string {
return String(string(sha1.New().Sum([]byte(strings.Join(files, "-")))))
}
func String(value string) *string {
return &value
}
func Boolean(value bool) *bool {
return &value
}
// TurnOffAllGockWhenMatched turns off all gock mocks when the matcher is matched
func TurnOffAllGockWhenMatched() gock.Matcher {
matcher := gock.NewBasicMatcher()
matcher.Add(func(req *http.Request, _ *gock.Request) (bool, error) {
gock.OffAll()
return true, nil
})
return matcher
}