-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost_test.go
75 lines (58 loc) · 1.65 KB
/
post_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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package examples
import (
"errors"
"fmt"
"net/http"
"testing"
"github.com/opnscty/go-httpclient/gohttp_mock"
)
func TestCreateRepo(t *testing.T) {
t.Run("TimeoutFromGithub", func(t *testing.T) {
gohttp_mock.MockupServer.DeleteMocks()
gohttp_mock.MockupServer.AddMock(gohttp_mock.Mock{
Method: http.MethodPost,
URL: "https://api.github.com/user/repos",
RequestBody: `{"name":"test-repo","private":true}`,
Error: errors.New("Timeout from Github."),
})
repository := Repository{
Name: "test-repo",
Private: true,
}
repo, err := CreateRepo(repository)
if repo != nil {
t.Error("no repo expected when we get a timeout from github")
}
if err == nil {
t.Error("an error is expected when we get a timeout from github")
}
if err.Error() != "Timeout from Github." {
fmt.Println(err.Error())
t.Error("invalid error message")
}
})
t.Run("NoErrorFromGithub", func(t *testing.T) {
gohttp_mock.MockupServer.DeleteMocks()
gohttp_mock.MockupServer.AddMock(gohttp_mock.Mock{
Method: http.MethodPost,
URL: "https://api.github.com/user/repos",
RequestBody: `{"name":"test-repo","private":true}`,
ResponseStatusCode: http.StatusCreated,
ResponseBody: `{"id":123,"name":"test-repo"}`,
})
repository := Repository{
Name: "test-repo",
Private: true,
}
repo, err := CreateRepo(repository)
if err != nil {
t.Error("no error expected when we get valid response from github")
}
if repo == nil {
t.Error("a valid repo was expected at this point")
}
if repo.Name != repository.Name {
t.Error("invalid repository name obtained from github")
}
})
}