1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-18 10:27:06 +00: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:
Gerasimos (Makis) Maropoulos
2020-07-20 05:44:12 +03:00
parent 302597faac
commit beb67dc495
3 changed files with 53 additions and 0 deletions

View File

@@ -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)
}