diff --git a/_examples/testing/httptest/main_test.go b/_examples/testing/httptest/main_test.go index fa528642..b542ae90 100644 --- a/_examples/testing/httptest/main_test.go +++ b/_examples/testing/httptest/main_test.go @@ -3,6 +3,7 @@ package main import ( "testing" + "github.com/kataras/iris/v12" "github.com/kataras/iris/v12/httptest" ) @@ -28,3 +29,16 @@ func TestNewApp(t *testing.T) { e.GET("/admin/settings").WithBasicAuth("invalidusername", "invalidpassword"). Expect().Status(httptest.StatusUnauthorized) } + +func TestHandlerUsingNetHTTP(t *testing.T) { + handler := func(ctx iris.Context) { + ctx.WriteString("Hello, World!") + } + + w := httptest.NewRecorder() + r := httptest.NewRequest("GET", "/", nil) + httptest.Do(w, r, handler) + if expected, got := "Hello, World!", w.Body.String(); expected != got { + t.Fatalf("expected body: %s but got: %s", expected, got) + } +} diff --git a/configuration.go b/configuration.go index fcd77337..202ffd2e 100644 --- a/configuration.go +++ b/configuration.go @@ -10,6 +10,7 @@ import ( "runtime" "strings" + "github.com/kataras/golog" "github.com/kataras/iris/v12/context" "github.com/kataras/iris/v12/core/netutil" @@ -184,6 +185,11 @@ var WithGlobalConfiguration = func(app *Application) { // WithLogLevel sets the `Configuration.LogLevel` field. func WithLogLevel(level string) Configurator { return func(app *Application) { + if app.logger == nil { + app.logger = golog.Default + } + app.logger.SetLevel(level) // can be fired through app.Configure. + app.config.LogLevel = level } } @@ -990,6 +996,11 @@ func WithConfiguration(c Configuration) Configurator { return func(app *Application) { main := app.config + if main == nil { + app.config = &c + return + } + if v := c.LogLevel; v != "" { main.LogLevel = v } diff --git a/httptest/httptest.go b/httptest/httptest.go index 479e3fa7..38a2432e 100644 --- a/httptest/httptest.go +++ b/httptest/httptest.go @@ -3,9 +3,12 @@ package httptest import ( "crypto/tls" "net/http" + "net/http/httptest" "testing" "github.com/kataras/iris/v12" + "github.com/kataras/iris/v12/context" + "github.com/kataras/iris/v12/core/router" "github.com/iris-contrib/httpexpect/v2" ) @@ -141,3 +144,28 @@ func NewInsecure(t *testing.T, setters ...OptionSetter) *httpexpect.Expect { return httpexpect.WithConfig(testConfiguration) } + +// Aliases for "net/http/httptest" package. See `Do` package-level function. +var ( + NewRecorder = httptest.NewRecorder + NewRequest = httptest.NewRequest +) + +// Do is a simple helper which can be used to test handlers individually +// with the "net/http/httptest" package. +// This package contains aliases for `NewRequest` and `NewRecorder` too. +// +// For a more efficient testing please use the `New` function instead. +func Do(w http.ResponseWriter, r *http.Request, handler iris.Handler, irisConfigurators ...iris.Configurator) { + app := new(iris.Application) + app.Configure(iris.WithConfiguration(iris.DefaultConfiguration()), iris.WithLogLevel("disable")) + app.HTTPErrorHandler = router.NewDefaultHandler(app.ConfigurationReadOnly(), app.Logger()) + app.ContextPool = context.New(func() interface{} { + return context.NewContext(app) + }) + app.Configure(irisConfigurators...) + + ctx := app.ContextPool.Acquire(w, r) + handler(ctx) + app.ContextPool.Release(ctx) +}