-
Notifications
You must be signed in to change notification settings - Fork 1
/
endpoint_service_test.go
65 lines (54 loc) · 1.96 KB
/
endpoint_service_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
package mockservice_test
import (
"net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/wchan2/mock_service"
)
func TestEndpointService_ServeHTTP(t *testing.T) {
mockEndpoint := mockservice.MockEndpoint{
Method: http.MethodGet,
Endpoint: "/test",
StatusCode: http.StatusOK,
ResponseBody: "test",
ResponseHeaders: map[string]string{"foo": "bar"},
}
endpoints := mockservice.NewEndpoints()
endpoints.Create(&mockEndpoint)
svc := mockservice.NewEndpointService(endpoints)
t.Run("Endpoint_found", func(t *testing.T) {
recorder := httptest.NewRecorder()
req, err := http.NewRequest(http.MethodGet, "/test", nil)
if err != nil {
t.Errorf("Expected new request error to be nil but got %s", err)
}
svc.ServeHTTP(recorder, req)
if recorder.Code != http.StatusOK {
t.Errorf("Expected response status code to be %d but got %d", http.StatusOK, recorder.Code)
}
if reflect.DeepEqual(recorder.Header(), mockEndpoint.ResponseHeaders) {
t.Errorf("Expected %+v but got %+v", mockEndpoint.ResponseHeaders, recorder.Header())
}
if recorder.Body.String() != mockEndpoint.ResponseBody {
t.Errorf("Expected %s but got %s", mockEndpoint.ResponseBody, recorder.Body.String())
}
})
t.Run("Endpoint_not_found", func(t *testing.T) {
recorder := httptest.NewRecorder()
req, err := http.NewRequest(http.MethodGet, "/not-found", nil)
if err != nil {
t.Errorf("Expected new request error to be nil but got %s", err)
}
svc.ServeHTTP(recorder, req)
if recorder.Code != http.StatusNotFound {
t.Errorf("Expected response status code to be %d but got %d", http.StatusNotFound, recorder.Code)
}
if reflect.DeepEqual(recorder.Header(), mockEndpoint.ResponseHeaders) {
t.Errorf("Expected %+v but got %+v", mockEndpoint.ResponseHeaders, recorder.Header())
}
if recorder.Body.String() != "Endpoint does not exist\n" {
t.Errorf(`Expected "Endpoint does not exist\n" but got %s`, recorder.Body.String())
}
})
}