mirror of
https://github.com/davidebianchi/gswagger.git
synced 2025-12-23 23:38:43 -05:00
66 lines
1.7 KiB
Go
66 lines
1.7 KiB
Go
package echo
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/davidebianchi/gswagger/apirouter"
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestGorillaMuxRouter(t *testing.T) {
|
|
echoRouter := echo.New()
|
|
ar := NewRouter(echoRouter)
|
|
|
|
t.Run("create a new api router", func(t *testing.T) {
|
|
require.Implements(t, (*apirouter.Router[echo.HandlerFunc, Route])(nil), ar)
|
|
})
|
|
|
|
t.Run("add new route", func(t *testing.T) {
|
|
route := ar.AddRoute(http.MethodGet, "/foo", func(c echo.Context) error {
|
|
return c.String(http.StatusOK, "")
|
|
})
|
|
require.IsType(t, route, &echo.Route{})
|
|
|
|
t.Run("router exposes correctly api", func(t *testing.T) {
|
|
w := httptest.NewRecorder()
|
|
r := httptest.NewRequest(http.MethodGet, "/foo", nil)
|
|
|
|
echoRouter.ServeHTTP(w, r)
|
|
|
|
require.Equal(t, http.StatusOK, w.Result().StatusCode)
|
|
})
|
|
|
|
t.Run("router exposes api only to the specific method", func(t *testing.T) {
|
|
w := httptest.NewRecorder()
|
|
r := httptest.NewRequest(http.MethodPost, "/foo", nil)
|
|
|
|
echoRouter.ServeHTTP(w, r)
|
|
|
|
require.Equal(t, http.StatusMethodNotAllowed, w.Result().StatusCode)
|
|
})
|
|
})
|
|
|
|
t.Run("create openapi handler", func(t *testing.T) {
|
|
handlerFunc := ar.SwaggerHandler("text/html", []byte("some data"))
|
|
echoRouter.GET("/oas", handlerFunc)
|
|
|
|
t.Run("responds correctly to the API", func(t *testing.T) {
|
|
w := httptest.NewRecorder()
|
|
r := httptest.NewRequest(http.MethodGet, "/oas", nil)
|
|
|
|
echoRouter.ServeHTTP(w, r)
|
|
|
|
require.Equal(t, http.StatusOK, w.Result().StatusCode)
|
|
require.Equal(t, "text/html", w.Result().Header.Get("Content-Type"))
|
|
|
|
body, err := io.ReadAll(w.Result().Body)
|
|
require.NoError(t, err)
|
|
require.Equal(t, "some data", string(body))
|
|
})
|
|
})
|
|
}
|