Skip to content

Commit e352a17

Browse files
committed
domain: syncs unit tests
1 parent 4a289a1 commit e352a17

File tree

2 files changed

+12
-25
lines changed

2 files changed

+12
-25
lines changed

domain/auth/handler_test.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"testing"
1212

1313
"github.com/chris-ramon/golang-scaffolding/domain/auth/types"
14-
"github.com/julienschmidt/httprouter"
1514
)
1615

1716
type testReaderError int
@@ -37,9 +36,8 @@ func TestGetPing(t *testing.T) {
3736
h := NewHandlers(&serviceMock{})
3837
req := httptest.NewRequest("GET", "/ping", nil)
3938
w := httptest.NewRecorder()
40-
params := httprouter.Params{}
4139

42-
h.GetPing()(w, req, params)
40+
h.GetPing()(w, req)
4341

4442
body, err := io.ReadAll(w.Result().Body)
4543
if err != nil {
@@ -56,7 +54,6 @@ func TestGetCurrentUser(t *testing.T) {
5654
srvMock *serviceMock
5755
request *http.Request
5856
responseWriter *httptest.ResponseRecorder
59-
params httprouter.Params
6057
header http.Header
6158
expectedBody string
6259
expectedStatusCode uint
@@ -74,7 +71,6 @@ func TestGetCurrentUser(t *testing.T) {
7471
},
7572
request: httptest.NewRequest("GET", "/auth/current-user", nil),
7673
responseWriter: httptest.NewRecorder(),
77-
params: httprouter.Params{},
7874
header: map[string][]string{
7975
"Authorization": []string{"Bearer Test-JWT-Token"},
8076
},
@@ -86,7 +82,6 @@ func TestGetCurrentUser(t *testing.T) {
8682
srvMock: &serviceMock{},
8783
request: httptest.NewRequest("GET", "/auth/current-user", nil),
8884
responseWriter: httptest.NewRecorder(),
89-
params: httprouter.Params{},
9085
header: map[string][]string{},
9186
expectedBody: "failed to get authorization header",
9287
expectedStatusCode: http.StatusInternalServerError,
@@ -100,7 +95,6 @@ func TestGetCurrentUser(t *testing.T) {
10095
},
10196
request: httptest.NewRequest("GET", "/auth/current-user", nil),
10297
responseWriter: httptest.NewRecorder(),
103-
params: httprouter.Params{},
10498
header: map[string][]string{
10599
"Authorization": []string{"Bearer Test-JWT-Token"},
106100
},
@@ -119,7 +113,7 @@ func TestGetCurrentUser(t *testing.T) {
119113
}
120114
}
121115

122-
h.GetCurrentUser()(testCase.responseWriter, testCase.request, testCase.params)
116+
h.GetCurrentUser()(testCase.responseWriter, testCase.request)
123117

124118
if !strings.Contains(testCase.responseWriter.Body.String(), testCase.expectedBody) {
125119
t.Fatalf("expected: %v, got: %v", testCase.expectedBody, testCase.responseWriter.Body.String())
@@ -134,7 +128,6 @@ func TestPostSignIn(t *testing.T) {
134128
srvMock *serviceMock
135129
request *http.Request
136130
responseWriter *httptest.ResponseRecorder
137-
params httprouter.Params
138131
expectedBody string
139132
}
140133

@@ -154,7 +147,6 @@ func TestPostSignIn(t *testing.T) {
154147
bytes.NewBuffer([]byte(`{"email":"[email protected]","password":"test-pwd"}`)),
155148
),
156149
responseWriter: httptest.NewRecorder(),
157-
params: httprouter.Params{},
158150
expectedBody: "test user",
159151
},
160152
{
@@ -172,7 +164,6 @@ func TestPostSignIn(t *testing.T) {
172164
testReaderError(0),
173165
),
174166
responseWriter: httptest.NewRecorder(),
175-
params: httprouter.Params{},
176167
expectedBody: "failed to read request body",
177168
},
178169
{
@@ -190,7 +181,6 @@ func TestPostSignIn(t *testing.T) {
190181
bytes.NewBuffer([]byte(`{invalid}`)),
191182
),
192183
responseWriter: httptest.NewRecorder(),
193-
params: httprouter.Params{},
194184
expectedBody: "failed to json unmarshal request body",
195185
},
196186
{
@@ -206,15 +196,14 @@ func TestPostSignIn(t *testing.T) {
206196
bytes.NewBuffer([]byte(`{"email":"[email protected]","password":"test-pwd"}`)),
207197
),
208198
responseWriter: httptest.NewRecorder(),
209-
params: httprouter.Params{},
210199
expectedBody: "failed to find current user",
211200
},
212201
}
213202

214203
for _, testCase := range testCases {
215204
t.Run(testCase.name, func(t *testing.T) {
216205
h := NewHandlers(testCase.srvMock)
217-
h.PostSignIn()(testCase.responseWriter, testCase.request, testCase.params)
206+
h.PostSignIn()(testCase.responseWriter, testCase.request)
218207
if !strings.Contains(testCase.responseWriter.Body.String(), testCase.expectedBody) {
219208
t.Fatalf("expected: %v, got: %v", testCase.expectedBody, testCase.responseWriter.Body.String())
220209
}

domain/auth/route_test.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,26 @@ import (
44
"net/http"
55
"testing"
66

7-
"github.com/julienschmidt/httprouter"
8-
97
"github.com/chris-ramon/golang-scaffolding/pkg/route"
108
)
119

1210
type handlersMock struct {
13-
getPing httprouter.Handle
14-
getCurrentUser httprouter.Handle
15-
postSignIn httprouter.Handle
11+
getPing http.HandlerFunc
12+
getCurrentUser http.HandlerFunc
13+
postSignIn http.HandlerFunc
1614
}
1715

18-
func (h *handlersMock) GetPing() httprouter.Handle { return h.getPing }
19-
func (h *handlersMock) GetCurrentUser() httprouter.Handle { return h.getCurrentUser }
20-
func (h *handlersMock) PostSignIn() httprouter.Handle { return h.postSignIn }
16+
func (h *handlersMock) GetPing() http.HandlerFunc { return h.getPing }
17+
func (h *handlersMock) GetCurrentUser() http.HandlerFunc { return h.getCurrentUser }
18+
func (h *handlersMock) PostSignIn() http.HandlerFunc { return h.postSignIn }
2119

2220
func TestRoutesAll(t *testing.T) {
2321
h := &handlersMock{
24-
getPing: func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
22+
getPing: func(w http.ResponseWriter, r *http.Request) {
2523
},
26-
getCurrentUser: func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
24+
getCurrentUser: func(w http.ResponseWriter, r *http.Request) {
2725
},
28-
postSignIn: func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
26+
postSignIn: func(w http.ResponseWriter, r *http.Request) {
2927
},
3028
}
3129

0 commit comments

Comments
 (0)