-
Notifications
You must be signed in to change notification settings - Fork 1
/
endpoints_test.go
74 lines (62 loc) · 2.07 KB
/
endpoints_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
package mockservice_test
import (
"net/http"
"testing"
"github.com/wchan2/mock_service"
)
func TestLookup(t *testing.T) {
mockEndpoint := &mockservice.MockEndpoint{
Method: http.MethodPost,
Endpoint: "/test/endpoint",
StatusCode: http.StatusOK,
ResponseBody: "sample response",
ResponseHeaders: map[string]string{},
}
endpoints := mockservice.NewEndpoints()
if err := endpoints.Create(mockEndpoint); err != nil {
t.Errorf("Expected creating endpoint to return a nil error but got %s", err)
}
storedEp, err := endpoints.Lookup(http.MethodPost, "/test/endpoint")
if err != nil {
t.Errorf("Expected endpoint lookup error to be nil but got %s", err)
}
if storedEp != mockEndpoint {
t.Errorf("Expected %+v endpoint but got %+v", mockEndpoint, storedEp)
}
}
func TestLookupNonExistentEndpoint(t *testing.T) {
endpoints := mockservice.NewEndpoints()
ep, err := endpoints.Lookup("POST", "/does/not/exist")
if ep != nil {
t.Errorf("Expected nil endpoint but got %+v", ep)
}
if err != mockservice.ErrEndpointDoesNotExist {
t.Errorf("Expected %s error but got %s", mockservice.ErrEndpointDoesNotExist, err)
}
}
func TestCreateErrEmptyHTTPMethod(t *testing.T) {
mockEndpoint := &mockservice.MockEndpoint{
Method: " ",
Endpoint: "/test/endpoint",
StatusCode: http.StatusOK,
ResponseBody: "sample response",
ResponseHeaders: map[string]string{},
}
endpoints := mockservice.NewEndpoints()
if err := endpoints.Create(mockEndpoint); err != mockservice.ErrEmptyHTTPMethod {
t.Errorf("Expected %s error but got %s", mockservice.ErrEmptyHTTPMethod, err)
}
}
func TestCreateErrEmptyEndpoint(t *testing.T) {
mockEndpoint := &mockservice.MockEndpoint{
Method: "POST",
Endpoint: " ",
StatusCode: http.StatusOK,
ResponseBody: "sample response",
ResponseHeaders: map[string]string{},
}
endpoints := mockservice.NewEndpoints()
if err := endpoints.Create(mockEndpoint); err != mockservice.ErrEmptyEndpoint {
t.Errorf("Expected %s error but got %s", mockservice.ErrEmptyEndpoint, err)
}
}