Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
evg4b committed Jun 18, 2023
1 parent e34f5e7 commit 6984183
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
23 changes: 19 additions & 4 deletions internal/contracts/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@ func TestCastToHTTPHandler(t *testing.T) {
sfmt.Fprint(w, expectedBody)
})

request := httptest.NewRequest(http.MethodGet, "/data", nil)
httpHandler := contracts.CastToHTTPHandler(uncorsHandler)

req := httptest.NewRequest(http.MethodGet, "/data", nil)

t.Run("cast correctly", func(t *testing.T) {
recirder := httptest.NewRecorder()
responceWriter := contracts.WrapResponseWriter(recirder)

assert.NotPanics(t, func() {
httpHandler.ServeHTTP(responceWriter, req)
httpHandler.ServeHTTP(responceWriter, request)
assert.Equal(t, expectedBody, testutils.ReadBody(t, recirder))
})
})
Expand All @@ -36,7 +35,23 @@ func TestCastToHTTPHandler(t *testing.T) {
recirder := httptest.NewRecorder()

assert.PanicsWithValue(t, contracts.ErrResponceNotCasted, func() {
httpHandler.ServeHTTP(recirder, req)
httpHandler.ServeHTTP(recirder, request)
})
})
}

func TestHandlerFunc(t *testing.T) {
const expectedBody = `{ "OK": true }`
uncorsHandler := contracts.HandlerFunc(func(w *contracts.ResponseWriter, r *contracts.Request) {
w.WriteHeader(http.StatusOK)
sfmt.Fprint(w, expectedBody)
})

recirder := httptest.NewRecorder()
responceWriter := contracts.WrapResponseWriter(recirder)
request := httptest.NewRequest(http.MethodGet, "/data", nil)

uncorsHandler.ServeHTTP(responceWriter, request)

assert.Equal(t, expectedBody, testutils.ReadBody(t, recirder))
}
2 changes: 1 addition & 1 deletion internal/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestNewUncorsServer(t *testing.T) {
ctx := context.Background()
expectedResponse := "UNCORS OK!"

var handler contracts.HandlerFunc = func(w *contracts.ResponseWriter, _r *contracts.Request) {
var handler contracts.HandlerFunc = func(w *contracts.ResponseWriter, _ *contracts.Request) {
w.WriteHeader(http.StatusOK)
sfmt.Fprint(w, expectedResponse)
}
Expand Down

0 comments on commit 6984183

Please sign in to comment.