mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 10:41:03 +01:00
testing: add a 'Do' helper that accepts http.ResponseWriter and http.Request for integration with the standard net/http/httptest package for easier transition from net/http and other frameworks into Iris
Former-commit-id: 8ec6e3468affc28ce34c9bef6795b444fffa08c5
This commit is contained in:
parent
302597faac
commit
beb67dc495
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/kataras/iris/v12"
|
||||||
"github.com/kataras/iris/v12/httptest"
|
"github.com/kataras/iris/v12/httptest"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -28,3 +29,16 @@ func TestNewApp(t *testing.T) {
|
||||||
e.GET("/admin/settings").WithBasicAuth("invalidusername", "invalidpassword").
|
e.GET("/admin/settings").WithBasicAuth("invalidusername", "invalidpassword").
|
||||||
Expect().Status(httptest.StatusUnauthorized)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/kataras/golog"
|
||||||
"github.com/kataras/iris/v12/context"
|
"github.com/kataras/iris/v12/context"
|
||||||
"github.com/kataras/iris/v12/core/netutil"
|
"github.com/kataras/iris/v12/core/netutil"
|
||||||
|
|
||||||
|
@ -184,6 +185,11 @@ var WithGlobalConfiguration = func(app *Application) {
|
||||||
// WithLogLevel sets the `Configuration.LogLevel` field.
|
// WithLogLevel sets the `Configuration.LogLevel` field.
|
||||||
func WithLogLevel(level string) Configurator {
|
func WithLogLevel(level string) Configurator {
|
||||||
return func(app *Application) {
|
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
|
app.config.LogLevel = level
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -990,6 +996,11 @@ func WithConfiguration(c Configuration) Configurator {
|
||||||
return func(app *Application) {
|
return func(app *Application) {
|
||||||
main := app.config
|
main := app.config
|
||||||
|
|
||||||
|
if main == nil {
|
||||||
|
app.config = &c
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if v := c.LogLevel; v != "" {
|
if v := c.LogLevel; v != "" {
|
||||||
main.LogLevel = v
|
main.LogLevel = v
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,9 +3,12 @@ package httptest
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/kataras/iris/v12"
|
"github.com/kataras/iris/v12"
|
||||||
|
"github.com/kataras/iris/v12/context"
|
||||||
|
"github.com/kataras/iris/v12/core/router"
|
||||||
|
|
||||||
"github.com/iris-contrib/httpexpect/v2"
|
"github.com/iris-contrib/httpexpect/v2"
|
||||||
)
|
)
|
||||||
|
@ -141,3 +144,28 @@ func NewInsecure(t *testing.T, setters ...OptionSetter) *httpexpect.Expect {
|
||||||
|
|
||||||
return httpexpect.WithConfig(testConfiguration)
|
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)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user