From bfeabbd74e404db3f68becb226e86b21d8528cea Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Fri, 17 Feb 2017 01:34:45 +0200 Subject: [PATCH] Add a httptest simple example, I'm thinking to obselete the gitbook entirely, the feature/_example folder is easier for the users Former-commit-id: 6d4143e63838164a6b1bf1184b0c86a6b3fe15cd --- README.md | 7 +++--- httptest/_example/main.go | 41 ++++++++++++++++++++++++++++++++++ httptest/_example/main_test.go | 32 ++++++++++++++++++++++++++ httptest/httptest.go | 14 ++++++------ 4 files changed, 83 insertions(+), 11 deletions(-) create mode 100644 httptest/_example/main.go create mode 100644 httptest/_example/main_test.go diff --git a/README.md b/README.md index e190aaab..0cde33a7 100644 --- a/README.md +++ b/README.md @@ -116,8 +116,8 @@ package main import ( "gopkg.in/kataras/iris.v6" - "gopkg.in/kataras/iris.v6/adaptors/httprouter" "gopkg.in/kataras/iris.v6/adaptors/view" + "gopkg.in/kataras/iris.v6/adaptors/httprouter" ) func main() { @@ -208,9 +208,8 @@ Testing You can find RESTFUL test examples by navigating to the following links: -- [gavv/_examples/iris_test.go](https://github.com/gavv/httpexpect/blob/master/_examples/iris_test.go). -- [./http_test.go](https://github.com/kataras/iris/blob/v6/http_test.go). -- [./context_test.go](https://github.com/kataras/iris/blob/v6/context_test.go). +- [gavv/_examples/iris_test.go](https://github.com/gavv/httpexpect/blob/master/_examples/iris_test.go) +- [./httptest/_example/main_test.go](https://github.com/kataras/iris/blob/v6/httptest/_example/main_test.go) FAQ diff --git a/httptest/_example/main.go b/httptest/_example/main.go new file mode 100644 index 00000000..7302b800 --- /dev/null +++ b/httptest/_example/main.go @@ -0,0 +1,41 @@ +package main + +import ( + "gopkg.in/kataras/iris.v6" + "gopkg.in/kataras/iris.v6/adaptors/httprouter" + "gopkg.in/kataras/iris.v6/adaptors/sessions" +) + +func newApp() *iris.Framework { + app := iris.New() + app.Adapt(httprouter.New()) + app.Adapt(sessions.New(sessions.Config{Cookie: "mysessionid"})) + + app.Get("/hello", func(ctx *iris.Context) { + sess := ctx.Session() + if !sess.HasFlash() /* or sess.GetFlash("name") == "", same thing here */ { + ctx.HTML(iris.StatusUnauthorized, "

Unauthorized Page!

") + return + } + + ctx.JSON(iris.StatusOK, iris.Map{ + "Message": "Hello", + "From": sess.GetFlash("name"), + }) + }) + + app.Post("/login", func(ctx *iris.Context) { + sess := ctx.Session() + if !sess.HasFlash() { + sess.SetFlash("name", ctx.FormValue("name")) + } + // let's no redirect, just set the flash message, nothing more. + }) + + return app +} + +func main() { + app := newApp() + app.Listen(":8080") +} diff --git a/httptest/_example/main_test.go b/httptest/_example/main_test.go new file mode 100644 index 00000000..e93d319f --- /dev/null +++ b/httptest/_example/main_test.go @@ -0,0 +1,32 @@ +package main + +import ( + "testing" + + "gopkg.in/kataras/iris.v6/httptest" +) + +// $ cd _example +// $ go test -v +func TestNewApp(t *testing.T) { + app := newApp() + e := httptest.New(app, t) + + // test nauthorized + e.GET("/hello").Expect().Status(401).Body().Equal("

Unauthorized Page!

") + // test our login flash message + name := "myname" + e.POST("/login").WithFormField("name", name).Expect().Status(200) + // test the /hello again with the flash (a message which deletes itself after it has been shown to the user) + // setted on /login previously. + expectedResponse := map[string]interface{}{ + "Message": "Hello", + "From": name, + } + e.GET("/hello").Expect().Status(200).JSON().Equal(expectedResponse) + // test /hello nauthorized again, it should be return 401 now (flash should be removed) + e.GET("/hello").Expect().Status(401).Body().Equal("

Unauthorized Page!

") +} + +// for advanced test examples navigate there: +// https://github.com/gavv/httpexpect/blob/master/_examples/iris_test.go diff --git a/httptest/httptest.go b/httptest/httptest.go index f20fbf27..1e827474 100644 --- a/httptest/httptest.go +++ b/httptest/httptest.go @@ -63,7 +63,7 @@ func DefaultConfiguration() *Configuration { return &Configuration{ExplicitURL: false, Debug: false} } -// New Prepares and returns a new test framework based on the api +// New Prepares and returns a new test framework based on the app // is useful when you need to have more than one test framework for the same iris instance // usage: // iris.Default.Get("/mypath", func(ctx *iris.Context){ctx.Write("my body")}) @@ -72,19 +72,19 @@ func DefaultConfiguration() *Configuration { // e.GET("/mypath").Expect().Status(iris.StatusOK).Body().Equal("my body") // // You can find example on the https://github.com/kataras/iris/glob/master/context_test.go -func New(api *iris.Framework, t *testing.T, setters ...OptionSetter) *httpexpect.Expect { +func New(app *iris.Framework, t *testing.T, setters ...OptionSetter) *httpexpect.Expect { conf := DefaultConfiguration() for _, setter := range setters { setter.Set(conf) } - api.Set(iris.OptionDisableBanner(true)) - api.Adapt(iris.DevLogger()) + app.Set(iris.OptionDisableBanner(true)) + app.Adapt(iris.DevLogger()) baseURL := "" - api.Boot() + app.Boot() if !conf.ExplicitURL { - baseURL = api.Config.VScheme + api.Config.VHost + baseURL = app.Config.VScheme + app.Config.VHost // if it's still empty then set it to the default server addr if baseURL == "" { baseURL = iris.SchemeHTTP + iris.DefaultServerAddr @@ -95,7 +95,7 @@ func New(api *iris.Framework, t *testing.T, setters ...OptionSetter) *httpexpect testConfiguration := httpexpect.Config{ BaseURL: baseURL, Client: &http.Client{ - Transport: httpexpect.NewBinder(api), + Transport: httpexpect.NewBinder(app), Jar: httpexpect.NewJar(), }, Reporter: httpexpect.NewAssertReporter(t),