forked from Xzya/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
route_test.go
101 lines (85 loc) · 3.42 KB
/
route_test.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
package iris_test
import (
"strconv"
"testing"
"gopkg.in/kataras/iris.v6"
"gopkg.in/kataras/iris.v6/adaptors/gorillamux"
"gopkg.in/kataras/iris.v6/adaptors/httprouter"
"gopkg.in/kataras/iris.v6/httptest"
)
func testRouteStateSimple(t *testing.T, router iris.Policy, offlineRoutePath string) {
app := iris.New()
app.Adapt(router)
offlineRouteRequestedTestPath := "/api/user/42"
offlineBody := "user with id: 42"
offlineRoute := app.None(offlineRoutePath, func(ctx *iris.Context) {
userid := ctx.Param("userid")
if userid != "42" {
// we are expecting userid 42 always in this test so
t.Fatalf("what happened? expected userid to be 42 but got %s", userid)
}
ctx.Writef(offlineBody)
}).ChangeName("api.users") // or an empty (), required, in order to get the Route instance.
// change the "user.api" state from offline to online and online to offline
app.Get("/change", func(ctx *iris.Context) {
// here
if offlineRoute.IsOnline() {
// set to offline
app.Routes().Offline(offlineRoute)
} else {
// set to online if it was not online(so it was offline)
app.Routes().Online(offlineRoute, iris.MethodGet)
}
})
app.Get("/execute", func(ctx *iris.Context) {
// here
ctx.ExecRouteAgainst(offlineRoute, "/api/user/42")
})
// append the body and change the status code from an 'offline' route execution
app.Get("/execute_modified", func(ctx *iris.Context) {
ctx.Set("mykey", "myval")
// here
ctx.Record() // if we want to control the response
ctx.ExecRouteAgainst(offlineRoute, "/api/user/42")
ctx.Write([]byte("modified from status code: " + strconv.Itoa(ctx.StatusCode())))
ctx.SetStatusCode(iris.StatusUseProxy)
if ctx.Path() != "/execute_modified" {
t.Fatalf("Expected Request Path of this context NOT to change but got: '%s' ", ctx.Path())
}
if got := ctx.Get("mykey"); got != "myval" {
t.Fatalf("Expected Value 'mykey' of this context NOT to change('%s') but got: '%s' ", "myval", got)
}
ctx.Next()
}, func(ctx *iris.Context) {
ctx.Writef("-original_middleware_here")
})
hello := "Hello from index"
app.Get("/", func(ctx *iris.Context) {
ctx.Writef(hello)
})
e := httptest.New(app, t)
e.GET("/").Expect().Status(iris.StatusOK).Body().Equal(hello)
// here
// the status should be not found, the route is invisible from outside world
e.GET(offlineRouteRequestedTestPath).Expect().Status(iris.StatusNotFound)
// set the route online with the /change
e.GET("/change").Expect().Status(iris.StatusOK)
// try again, it should be online now
e.GET(offlineRouteRequestedTestPath).Expect().Status(iris.StatusOK).Body().Equal(offlineBody)
// change to offline again
e.GET("/change").Expect().Status(iris.StatusOK)
// and test again, it should be offline now
e.GET(offlineRouteRequestedTestPath).Expect().Status(iris.StatusNotFound)
// finally test the execute on the offline route
// it should be remains offline but execute the route like it is from client request.
e.GET("/execute").Expect().Status(iris.StatusOK).Body().Equal(offlineBody)
e.GET(offlineRouteRequestedTestPath).Expect().Status(iris.StatusNotFound)
e.GET("/execute_modified").Expect().Status(iris.StatusUseProxy).Body().
Equal(offlineBody + "modified from status code: 200-original_middleware_here")
}
func TestRouteStateSimple(t *testing.T) {
// httprouter adaptor
testRouteStateSimple(t, httprouter.New(), "/api/user/:userid")
// gorillamux adaptor
testRouteStateSimple(t, gorillamux.New(), "/api/user/{userid}")
}