forked from gavv/httpexpect
/
iris.go
101 lines (83 loc) · 2.06 KB
/
iris.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// +build go1.8
package examples
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"gopkg.in/kataras/iris.v6"
"gopkg.in/kataras/iris.v6/adaptors/httprouter"
"gopkg.in/kataras/iris.v6/adaptors/sessions"
"gopkg.in/kataras/iris.v6/middleware/basicauth"
)
// IrisHandler tests iris v6's handler
func IrisHandler() http.Handler {
app := iris.New(iris.Configuration{VHost: "example.com"})
app.Adapt(iris.DevLogger())
app.Adapt(httprouter.New())
app.Adapt(sessions.New(sessions.Config{Cookie: "irissessionid"}))
app.Get("/things", func(c *iris.Context) {
c.JSON(iris.StatusOK, []interface{}{
iris.Map{
"name": "foo",
"description": "foo thing",
},
iris.Map{
"name": "bar",
"description": "bar thing",
},
})
})
app.Post("/redirect", func(c *iris.Context) {
c.Redirect("/things", iris.StatusFound)
})
app.Post("/params/:x/:y", func(c *iris.Context) {
c.JSON(iris.StatusOK, iris.Map{
"x": c.Param("x"),
"y": c.Param("y"),
"q": c.URLParam("q"),
"p1": c.FormValue("p1"),
"p2": c.FormValue("p2"),
})
})
auth := basicauth.Default(map[string]string{
"ford": "betelgeuse7",
})
app.Get("/auth", auth, func(c *iris.Context) {
c.Writef("authenticated!")
})
app.Post("/session/set", func(c *iris.Context) {
sess := iris.Map{}
if err := c.ReadJSON(&sess); err != nil {
panic(err.Error())
}
c.Session().Set("name", sess["name"])
})
app.Get("/session/get", func(c *iris.Context) {
name := c.Session().GetString("name")
c.JSON(iris.StatusOK, iris.Map{
"name": name,
})
})
app.Get("/stream", func(c *iris.Context) {
c.StreamWriter(func(w io.Writer) bool {
for i := 0; i < 10; i++ {
fmt.Fprintf(w, "%d", i)
}
// return true to continue, return false to stop and flush
return false
})
// if we had to write here then the StreamWriter callback should
// return true
})
app.Post("/stream", func(c *iris.Context) {
body, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
c.EmitError(iris.StatusBadRequest)
return
}
c.Write(body)
})
app.Boot()
return app.Router
}