Add Strict option to httptest as requested at: #1722

This commit is contained in:
Gerasimos (Makis) Maropoulos 2021-02-13 21:21:19 +02:00
parent 5fe233278a
commit d26b9bfbed
No known key found for this signature in database
GPG Key ID: A771A828097B36C7
5 changed files with 39 additions and 9 deletions

View File

@ -28,6 +28,8 @@ The codebase for Dependency Injection, Internationalization and localization and
## Fixes and Improvements
- New `httptest.Strict` option setter to enable the `httpexpect.RequireReporter` instead of the default `httpexpect.AssetReporter. Use that to enable complete test failure on the first error. As requested at: [#1722](https://github.com/kataras/iris/issues/1722).
- New `uuid` builtin path parameter type. Example:
```go

View File

@ -10,7 +10,7 @@ import (
// $ go test -v
func TestNewApp(t *testing.T) {
app := newApp()
e := httptest.New(t, app)
e := httptest.New(t, app, httptest.Strict(true))
// redirects to /admin without basic auth
e.GET("/").Expect().Status(httptest.StatusUnauthorized)

2
go.mod
View File

@ -20,7 +20,7 @@ require (
github.com/iris-contrib/schema v0.0.6
github.com/json-iterator/go v1.1.10
github.com/kataras/blocks v0.0.4
github.com/kataras/golog v0.1.6
github.com/kataras/golog v0.1.7
github.com/kataras/jwt v0.1.0
github.com/kataras/neffos v0.0.18
github.com/kataras/pio v0.0.10

View File

@ -40,6 +40,11 @@ type Configuration struct {
// LogLevel sets the application's log level.
// Defaults to "disable" when testing.
LogLevel string
// If true then the underline httpexpect report will be acquired by the NewRequireReporter
// call instead of the default NewAssertReporter.
// Defaults to false.
Strict bool // Note: if more reports are available in the future then add a Reporter interface as a field.
}
// Set implements the OptionSetter for the Configuration itself
@ -49,6 +54,7 @@ func (c Configuration) Set(main *Configuration) {
if c.LogLevel != "" {
main.LogLevel = c.LogLevel
}
main.Strict = c.Strict
}
var (
@ -74,6 +80,15 @@ var (
c.LogLevel = level
}
}
// Strict sets the Strict configuration field to "val".
// Applies the NewRequireReporter instead of the default one.
// Use this if you want the test to fail on first error, before all checks have been done.
Strict = func(val bool) OptionSet {
return func(c *Configuration) {
c.Strict = val
}
}
)
// DefaultConfiguration returns the default configuration for the httptest.
@ -82,7 +97,12 @@ func DefaultConfiguration() *Configuration {
}
// New Prepares and returns a new test framework based on the "app".
// You can find example on the https://github.com/kataras/iris/tree/master/_examples/testing/httptest
// Usage:
// httptest.New(t, app)
// With options:
// httptest.New(t, app, httptest.URL(...), httptest.Debug(true), httptest.LogLevel("debug"), httptest.Strict(true))
//
// Example at: https://github.com/kataras/iris/tree/master/_examples/testing/httptest.
func New(t *testing.T, app *iris.Application, setters ...OptionSetter) *httpexpect.Expect {
conf := DefaultConfiguration()
for _, setter := range setters {
@ -99,13 +119,21 @@ func New(t *testing.T, app *iris.Application, setters ...OptionSetter) *httpexpe
}
}
var reporter httpexpect.Reporter
if conf.Strict {
reporter = httpexpect.NewRequireReporter(t)
} else {
reporter = httpexpect.NewAssertReporter(t)
}
testConfiguration := httpexpect.Config{
BaseURL: conf.URL,
Client: &http.Client{
Transport: httpexpect.NewBinder(app),
Jar: httpexpect.NewJar(),
},
Reporter: httpexpect.NewAssertReporter(t),
Reporter: reporter,
}
if conf.Debug {

View File

@ -426,11 +426,11 @@ func TestUUIDEvaluatorRaw(t *testing.T) {
}{
{true, "978ad967-5fad-4c82-af99-580097ace662"}, // v4
{true, "c7067f9c-6d43-11eb-9439-0242ac130002"}, // v1
{false, "astring"}, // 0
{false, "astringwith_numb3rS_and_symbol$"}, // 1
{false, "32321"}, // 2
{false, "main.css"}, // 3
{false, "/assets/main.css"}, // 4
{false, "astring"}, // 2
{false, "astringwith_numb3rS_and_symbol$"}, // 3
{false, "32321"}, // 4
{false, "main.css"}, // 5
{false, "/assets/main.css"}, // 6
}
for i, tt := range tests {
testEvaluatorRaw(t, UUID, tt.input, reflect.String, tt.pass, i)