7
7
package router_test
8
8
9
9
import (
10
+ "fmt"
10
11
"testing"
11
12
12
13
"github.com/kataras/iris/v12"
13
- "github.com/kataras/iris/v12/context"
14
-
15
14
"github.com/kataras/iris/v12/httptest"
16
15
)
17
16
18
17
// test registering of below handlers
19
18
// with a different order but the route's final
20
19
// response should be the same at all cases.
21
20
var (
22
- mainResponse = "main"
23
- mainHandler = func (ctx * context.Context ) {
24
- ctx .WriteString (mainResponse )
25
- ctx .Next ()
21
+ writeHandler = func (s string ) iris.Handler {
22
+ return func (ctx iris.Context ) {
23
+ ctx .WriteString (s )
24
+ ctx .Next ()
25
+ }
26
26
}
27
27
28
+ mainResponse = "main"
29
+ mainHandler = writeHandler (mainResponse )
30
+
28
31
firstUseResponse = "use1"
29
- firstUseHandler = func (ctx * context.Context ) {
30
- ctx .WriteString (firstUseResponse )
31
- ctx .Next ()
32
- }
32
+ firstUseHandler = writeHandler (firstUseResponse )
33
33
34
34
secondUseResponse = "use2"
35
- secondUseHandler = func (ctx * context.Context ) {
36
- ctx .WriteString (secondUseResponse )
37
- ctx .Next ()
38
- }
35
+ secondUseHandler = writeHandler (secondUseResponse )
36
+
37
+ firstUseRouterResponse = "userouter1"
38
+ firstUseRouterHandler = writeHandler (firstUseRouterResponse )
39
+
40
+ secondUseRouterResponse = "userouter2"
41
+ secondUseRouterHandler = writeHandler (secondUseRouterResponse )
39
42
40
43
firstUseGlobalResponse = "useglobal1"
41
- firstUseGlobalHandler = func (ctx * context.Context ) {
42
- ctx .WriteString (firstUseGlobalResponse )
43
- ctx .Next ()
44
- }
44
+ firstUseGlobalHandler = writeHandler (firstUseGlobalResponse )
45
45
46
46
secondUseGlobalResponse = "useglobal2"
47
- secondUseGlobalHandler = func (ctx * context.Context ) {
48
- ctx .WriteString (secondUseGlobalResponse )
49
- ctx .Next ()
50
- }
47
+ secondUseGlobalHandler = writeHandler (secondUseGlobalResponse )
51
48
52
49
firstDoneResponse = "done1"
53
- firstDoneHandler = func (ctx * context.Context ) {
54
- ctx .WriteString (firstDoneResponse )
55
- ctx .Next ()
56
- }
50
+ firstDoneHandler = writeHandler (firstDoneResponse )
57
51
58
52
secondDoneResponse = "done2"
59
- secondDoneHandler = func (ctx * context .Context ) {
53
+ secondDoneHandler = func (ctx iris .Context ) {
60
54
ctx .WriteString (secondDoneResponse )
61
55
}
62
56
63
- finalResponse = firstUseGlobalResponse + secondUseGlobalResponse +
57
+ finalResponse = firstUseRouterResponse + secondUseRouterResponse + firstUseGlobalResponse + secondUseGlobalResponse +
64
58
firstUseResponse + secondUseResponse + mainResponse + firstDoneResponse + secondDoneResponse
65
59
66
60
testResponse = func (t * testing.T , app * iris.Application , path string ) {
73
67
74
68
func TestMiddlewareByRouteDef (t * testing.T ) {
75
69
app := iris .New ()
70
+ app .UseRouter (firstUseRouterHandler )
71
+ app .UseRouter (secondUseRouterHandler )
72
+
76
73
app .Get ("/mypath" , firstUseGlobalHandler , secondUseGlobalHandler , firstUseHandler , secondUseHandler ,
77
74
mainHandler , firstDoneHandler , secondDoneHandler )
78
75
@@ -81,6 +78,7 @@ func TestMiddlewareByRouteDef(t *testing.T) {
81
78
82
79
func TestMiddlewareByUseAndDoneDef (t * testing.T ) {
83
80
app := iris .New ()
81
+ app .UseRouter (firstUseRouterHandler , secondUseRouterHandler )
84
82
app .Use (firstUseGlobalHandler , secondUseGlobalHandler , firstUseHandler , secondUseHandler )
85
83
app .Done (firstDoneHandler , secondDoneHandler )
86
84
@@ -91,19 +89,22 @@ func TestMiddlewareByUseAndDoneDef(t *testing.T) {
91
89
92
90
func TestMiddlewareByUseUseGlobalAndDoneDef (t * testing.T ) {
93
91
app := iris .New ()
92
+
94
93
app .Use (firstUseHandler , secondUseHandler )
95
94
// if failed then UseGlobal didnt' registered these handlers even before the
96
95
// existing middleware.
97
96
app .UseGlobal (firstUseGlobalHandler , secondUseGlobalHandler )
98
97
app .Done (firstDoneHandler , secondDoneHandler )
99
98
99
+ app .UseRouter (firstUseRouterHandler , secondUseRouterHandler )
100
100
app .Get ("/mypath" , mainHandler )
101
101
102
102
testResponse (t , app , "/mypath" )
103
103
}
104
104
105
105
func TestMiddlewareByUseDoneAndUseGlobalDef (t * testing.T ) {
106
106
app := iris .New ()
107
+ app .UseRouter (firstUseRouterHandler , secondUseRouterHandler )
107
108
108
109
app .Use (firstUseHandler , secondUseHandler )
109
110
app .Done (firstDoneHandler , secondDoneHandler )
@@ -123,6 +124,8 @@ func TestMiddlewareByUseDoneAndUseGlobalDef(t *testing.T) {
123
124
124
125
func TestMiddlewareByUseGlobalUseAndDoneGlobalDef (t * testing.T ) {
125
126
app := iris .New ()
127
+ app .UseRouter (firstUseRouterHandler )
128
+ app .UseRouter (secondUseRouterHandler )
126
129
127
130
app .UseGlobal (firstUseGlobalHandler )
128
131
app .UseGlobal (secondUseGlobalHandler )
@@ -137,6 +140,7 @@ func TestMiddlewareByUseGlobalUseAndDoneGlobalDef(t *testing.T) {
137
140
138
141
func TestMiddlewareByDoneUseAndUseGlobalDef (t * testing.T ) {
139
142
app := iris .New ()
143
+ app .UseRouter (firstUseRouterHandler , secondUseRouterHandler )
140
144
app .Done (firstDoneHandler , secondDoneHandler )
141
145
142
146
app .Use (firstUseHandler , secondUseHandler )
@@ -148,3 +152,29 @@ func TestMiddlewareByDoneUseAndUseGlobalDef(t *testing.T) {
148
152
149
153
testResponse (t , app , "/mypath" )
150
154
}
155
+
156
+ func TestUseRouterStopExecution (t * testing.T ) {
157
+ app := iris .New ()
158
+ app .UseRouter (func (ctx iris.Context ) {
159
+ ctx .WriteString ("stop" )
160
+ // no ctx.Next, so the router has not even the chance to work.
161
+ })
162
+ app .Get ("/" , writeHandler ("index" ))
163
+
164
+ e := httptest .New (t , app )
165
+ e .GET ("/" ).Expect ().Status (iris .StatusOK ).Body ().Equal ("stop" )
166
+
167
+ app = iris .New ()
168
+ app .OnErrorCode (iris .StatusForbidden , func (ctx iris.Context ) {
169
+ ctx .Writef ("err: %v" , ctx .GetErr ())
170
+ })
171
+ app .UseRouter (func (ctx iris.Context ) {
172
+ ctx .StopWithPlainError (iris .StatusForbidden , fmt .Errorf ("custom error" ))
173
+ // stopped but not data written yet, the error code handler
174
+ // should be responsible of it (use StopWithError to write and close).
175
+ })
176
+ app .Get ("/" , writeHandler ("index" ))
177
+
178
+ e = httptest .New (t , app )
179
+ e .GET ("/" ).Expect ().Status (iris .StatusForbidden ).Body ().Equal ("err: custom error" )
180
+ }
0 commit comments