-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_test.go
More file actions
106 lines (83 loc) · 2.4 KB
/
Copy pathget_test.go
File metadata and controls
106 lines (83 loc) · 2.4 KB
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
102
103
104
105
106
package examples
import (
"errors"
"fmt"
"net/http"
"os"
"strings"
"testing"
"github.com/opnscty/go-httpclient/gohttp_mock"
)
func TestMain(m *testing.M) {
fmt.Println("Starting test cases for package 'examples'...")
// Tell HTTP library to mock all following requests
gohttp_mock.MockupServer.Start()
os.Exit(m.Run())
}
func TestGetEndpoints(t *testing.T) {
t.Run("TestErrorFetchingFromGithub", func(t *testing.T) {
// Initialization:
gohttp_mock.MockupServer.DeleteMocks()
gohttp_mock.MockupServer.AddMock(gohttp_mock.Mock{
Method: http.MethodGet,
URL: "https://api.github.com",
Error: errors.New("Request Timeout: Could not fetch Github endpoints"),
})
// Execution:
endpoints, err := GetEndpoints()
// Validation:
if endpoints != nil {
t.Error("No endpoints expected.")
}
if err == nil {
t.Error("An error was expected.")
}
if err.Error() != "Request Timeout: Could not fetch Github endpoints" {
t.Error("Invalid error message received.")
}
})
t.Run("TestErrorUnmarshalResponseBody", func(t *testing.T) {
gohttp_mock.MockupServer.DeleteMocks()
// Initialization:
gohttp_mock.MockupServer.AddMock(gohttp_mock.Mock{
Method: http.MethodGet,
URL: "https://api.github.com",
ResponseStatusCode: http.StatusOK,
ResponseBody: `{"current_user_url": 123}`,
})
// Execution:
endpoints, err := GetEndpoints()
// Validation:
if endpoints != nil {
t.Error("No endpoints expected.")
}
if err == nil {
t.Error("An error was expected.")
}
if !strings.Contains(err.Error(), "cannot unmarshal number into Go struct field") {
t.Error("Invalid error message received.")
}
})
t.Run("TestNoError", func(t *testing.T) {
gohttp_mock.MockupServer.DeleteMocks()
// Initialization:
gohttp_mock.MockupServer.AddMock(gohttp_mock.Mock{
Method: http.MethodGet,
URL: "https://api.github.com",
ResponseStatusCode: http.StatusOK,
ResponseBody: `{"current_user_url": "http://api.github.com/user"}`,
})
// Execution:
endpoints, err := GetEndpoints()
// Validation:
if err != nil {
t.Error(fmt.Sprintf("No error was expected. Received: '%s'", err.Error()))
}
if endpoints == nil {
t.Error("Endpoints were expected. Nil was returned.")
}
if endpoints.CurrentUser != "http://api.github.com/user" {
t.Error("Invalid current user url.")
}
})
}