mirror of
https://github.com/kataras/iris.git
synced 2025-02-02 07:20:35 +01:00
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
This commit is contained in:
parent
fa5b529634
commit
bfeabbd74e
|
@ -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
|
||||
|
|
41
httptest/_example/main.go
Normal file
41
httptest/_example/main.go
Normal file
|
@ -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, "<h1> Unauthorized Page! </h1>")
|
||||
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")
|
||||
}
|
32
httptest/_example/main_test.go
Normal file
32
httptest/_example/main_test.go
Normal file
|
@ -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("<h1> Unauthorized Page! </h1>")
|
||||
// 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("<h1> Unauthorized Page! </h1>")
|
||||
}
|
||||
|
||||
// for advanced test examples navigate there:
|
||||
// https://github.com/gavv/httpexpect/blob/master/_examples/iris_test.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),
|
||||
|
|
Loading…
Reference in New Issue
Block a user