mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 18:37:05 +00:00
Added support for third party packages on httptest package with tests & example. (#1992)
* Added support for third party packages on httptest package with tests. * Update httptest.go Co-authored-by: Gerasimos (Makis) Maropoulos <kataras2006@hotmail.com>
This commit is contained in:
committed by
GitHub
parent
bdceab9342
commit
c1fa064d98
53
_examples/testing/ginkgotest/main.go
Normal file
53
_examples/testing/ginkgotest/main.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12"
|
||||
"github.com/kataras/iris/v12/middleware/basicauth"
|
||||
)
|
||||
|
||||
func newApp() *iris.Application {
|
||||
app := iris.New()
|
||||
|
||||
opts := basicauth.Options{
|
||||
Allow: basicauth.AllowUsers(map[string]string{"myusername": "mypassword"}),
|
||||
}
|
||||
|
||||
authentication := basicauth.New(opts) // or just: basicauth.Default(map...)
|
||||
|
||||
app.Get("/", func(ctx iris.Context) { ctx.Redirect("/admin") })
|
||||
|
||||
// to party
|
||||
|
||||
needAuth := app.Party("/admin", authentication)
|
||||
{
|
||||
//http://localhost:8080/admin
|
||||
needAuth.Get("/", h)
|
||||
// http://localhost:8080/admin/profile
|
||||
needAuth.Get("/profile", h)
|
||||
|
||||
// http://localhost:8080/admin/settings
|
||||
needAuth.Get("/settings", h)
|
||||
}
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
func h(ctx iris.Context) {
|
||||
username, password, _ := ctx.Request().BasicAuth()
|
||||
// third parameter it will be always true because the middleware
|
||||
// makes sure for that, otherwise this handler will not be executed.
|
||||
// OR:
|
||||
//
|
||||
// user := ctx.User().(*myUserType)
|
||||
// ctx.Writef("%s %s:%s", ctx.Path(), user.Username, user.Password)
|
||||
// OR if you don't have registered custom User structs:
|
||||
//
|
||||
// ctx.User().GetUsername()
|
||||
// ctx.User().GetPassword()
|
||||
ctx.Writef("%s %s:%s", ctx.Path(), username, password)
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := newApp()
|
||||
app.Listen(":8080")
|
||||
}
|
||||
Reference in New Issue
Block a user