diff --git a/HISTORY.md b/HISTORY.md index 19236ce7..08568fa1 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -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 diff --git a/_examples/testing/httptest/main_test.go b/_examples/testing/httptest/main_test.go index 9c3723bc..1846541d 100644 --- a/_examples/testing/httptest/main_test.go +++ b/_examples/testing/httptest/main_test.go @@ -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) diff --git a/go.mod b/go.mod index 1c103db3..0ba63b98 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/httptest/httptest.go b/httptest/httptest.go index 804f64b7..9467ca95 100644 --- a/httptest/httptest.go +++ b/httptest/httptest.go @@ -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 { diff --git a/macro/macro_test.go b/macro/macro_test.go index 6ae452b5..df2d344d 100644 --- a/macro/macro_test.go +++ b/macro/macro_test.go @@ -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)