feat: remove ServeHTTP method. Use the external mux router method instead

This commit is contained in:
Davide Bianchi
2020-10-14 23:14:42 +02:00
parent ec6788e607
commit fd0b235774
3 changed files with 10 additions and 14 deletions

View File

@@ -20,12 +20,12 @@ const (
func TestIntegration(t *testing.T) {
t.Run("router works correctly", func(t *testing.T) {
router := setupSwagger(t)
muxRouter := setupSwagger(t)
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/hello", nil)
router.ServeHTTP(w, r)
muxRouter.ServeHTTP(w, r)
require.Equal(t, http.StatusOK, w.Result().StatusCode)
@@ -36,7 +36,7 @@ func TestIntegration(t *testing.T) {
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, JSONDocumentationPath, nil)
router.ServeHTTP(w, r)
muxRouter.ServeHTTP(w, r)
require.Equal(t, http.StatusOK, w.Result().StatusCode)
@@ -55,13 +55,13 @@ func readBody(t *testing.T, requestBody io.ReadCloser) string {
return string(body)
}
func setupSwagger(t *testing.T) *Router {
func setupSwagger(t *testing.T) *mux.Router {
t.Helper()
context := context.Background()
r := mux.NewRouter()
muxRouter := mux.NewRouter()
router, err := New(r, Options{
router, err := New(muxRouter, Options{
Context: context,
Openapi: &openapi3.Swagger{
Info: &openapi3.Info{
@@ -84,5 +84,5 @@ func setupSwagger(t *testing.T) *Router {
err = router.GenerateAndExposeSwagger()
require.NoError(t, err)
return router
return muxRouter
}

View File

@@ -125,10 +125,6 @@ func (r Router) AddRoute(method string, path string, handler Handler, schema Sch
return r.AddRawRoute(method, path, handler, Operation{operation})
}
func (r Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
r.router.ServeHTTP(w, req)
}
func (r Router) getSchemaFromInterface(v interface{}, components *openapi3.Components) (*openapi3.Schema, *openapi3.Components, error) {
if v == nil {
return &openapi3.Schema{}, components, nil

View File

@@ -89,7 +89,7 @@ func TestAddRoute(t *testing.T) {
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/users", nil)
router.ServeHTTP(w, req)
r.ServeHTTP(w, req)
require.Equal(t, http.StatusOK, w.Result().StatusCode)
@@ -98,9 +98,9 @@ func TestAddRoute(t *testing.T) {
t.Run("and generate swagger", func(t *testing.T) {
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, JSONDocumentationPath, nil)
req := httptest.NewRequest(http.MethodGet, JSONDocumentationPath, nil)
router.ServeHTTP(w, r)
r.ServeHTTP(w, req)
require.Equal(t, http.StatusOK, w.Result().StatusCode)