Skip to content

Commit

Permalink
Add Strict option to httptest as requested at: #1722
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Feb 13, 2021
1 parent 5fe2332 commit d26b9bf
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 9 deletions.
2 changes: 2 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion _examples/testing/httptest/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 30 additions & 2 deletions httptest/httptest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -49,6 +54,7 @@ func (c Configuration) Set(main *Configuration) {
if c.LogLevel != "" {
main.LogLevel = c.LogLevel
}
main.Strict = c.Strict
}

var (
Expand All @@ -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.
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions macro/macro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d26b9bf

Please sign in to comment.