mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 02:31:04 +01:00
Add Strict option to httptest as requested at: #1722
This commit is contained in:
parent
5fe233278a
commit
d26b9bfbed
|
@ -28,6 +28,8 @@ The codebase for Dependency Injection, Internationalization and localization and
|
||||||
|
|
||||||
## Fixes and Improvements
|
## 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:
|
- New `uuid` builtin path parameter type. Example:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
|
|
@ -10,7 +10,7 @@ import (
|
||||||
// $ go test -v
|
// $ go test -v
|
||||||
func TestNewApp(t *testing.T) {
|
func TestNewApp(t *testing.T) {
|
||||||
app := newApp()
|
app := newApp()
|
||||||
e := httptest.New(t, app)
|
e := httptest.New(t, app, httptest.Strict(true))
|
||||||
|
|
||||||
// redirects to /admin without basic auth
|
// redirects to /admin without basic auth
|
||||||
e.GET("/").Expect().Status(httptest.StatusUnauthorized)
|
e.GET("/").Expect().Status(httptest.StatusUnauthorized)
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -20,7 +20,7 @@ require (
|
||||||
github.com/iris-contrib/schema v0.0.6
|
github.com/iris-contrib/schema v0.0.6
|
||||||
github.com/json-iterator/go v1.1.10
|
github.com/json-iterator/go v1.1.10
|
||||||
github.com/kataras/blocks v0.0.4
|
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/jwt v0.1.0
|
||||||
github.com/kataras/neffos v0.0.18
|
github.com/kataras/neffos v0.0.18
|
||||||
github.com/kataras/pio v0.0.10
|
github.com/kataras/pio v0.0.10
|
||||||
|
|
|
@ -40,6 +40,11 @@ type Configuration struct {
|
||||||
// LogLevel sets the application's log level.
|
// LogLevel sets the application's log level.
|
||||||
// Defaults to "disable" when testing.
|
// Defaults to "disable" when testing.
|
||||||
LogLevel string
|
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
|
// Set implements the OptionSetter for the Configuration itself
|
||||||
|
@ -49,6 +54,7 @@ func (c Configuration) Set(main *Configuration) {
|
||||||
if c.LogLevel != "" {
|
if c.LogLevel != "" {
|
||||||
main.LogLevel = c.LogLevel
|
main.LogLevel = c.LogLevel
|
||||||
}
|
}
|
||||||
|
main.Strict = c.Strict
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -74,6 +80,15 @@ var (
|
||||||
c.LogLevel = level
|
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.
|
// 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".
|
// 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 {
|
func New(t *testing.T, app *iris.Application, setters ...OptionSetter) *httpexpect.Expect {
|
||||||
conf := DefaultConfiguration()
|
conf := DefaultConfiguration()
|
||||||
for _, setter := range setters {
|
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{
|
testConfiguration := httpexpect.Config{
|
||||||
BaseURL: conf.URL,
|
BaseURL: conf.URL,
|
||||||
Client: &http.Client{
|
Client: &http.Client{
|
||||||
Transport: httpexpect.NewBinder(app),
|
Transport: httpexpect.NewBinder(app),
|
||||||
Jar: httpexpect.NewJar(),
|
Jar: httpexpect.NewJar(),
|
||||||
},
|
},
|
||||||
Reporter: httpexpect.NewAssertReporter(t),
|
Reporter: reporter,
|
||||||
}
|
}
|
||||||
|
|
||||||
if conf.Debug {
|
if conf.Debug {
|
||||||
|
|
|
@ -426,11 +426,11 @@ func TestUUIDEvaluatorRaw(t *testing.T) {
|
||||||
}{
|
}{
|
||||||
{true, "978ad967-5fad-4c82-af99-580097ace662"}, // v4
|
{true, "978ad967-5fad-4c82-af99-580097ace662"}, // v4
|
||||||
{true, "c7067f9c-6d43-11eb-9439-0242ac130002"}, // v1
|
{true, "c7067f9c-6d43-11eb-9439-0242ac130002"}, // v1
|
||||||
{false, "astring"}, // 0
|
{false, "astring"}, // 2
|
||||||
{false, "astringwith_numb3rS_and_symbol$"}, // 1
|
{false, "astringwith_numb3rS_and_symbol$"}, // 3
|
||||||
{false, "32321"}, // 2
|
{false, "32321"}, // 4
|
||||||
{false, "main.css"}, // 3
|
{false, "main.css"}, // 5
|
||||||
{false, "/assets/main.css"}, // 4
|
{false, "/assets/main.css"}, // 6
|
||||||
}
|
}
|
||||||
for i, tt := range tests {
|
for i, tt := range tests {
|
||||||
testEvaluatorRaw(t, UUID, tt.input, reflect.String, tt.pass, i)
|
testEvaluatorRaw(t, UUID, tt.input, reflect.String, tt.pass, i)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user