forked from kataras/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_test.go
61 lines (49 loc) · 1.83 KB
/
handler_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
// black-box
package iris_test
import (
"testing"
"gopkg.in/kataras/iris.v6"
"gopkg.in/kataras/iris.v6/httptest"
)
const testCustomHandlerParamName = "myparam"
type myTestHandlerData struct {
Sysname string // this will be the same for all requests
Version int // this will be the same for all requests
URLParameter string // this will be different for each request
}
type myTestCustomHandler struct {
data myTestHandlerData
}
func (m *myTestCustomHandler) Serve(ctx *iris.Context) {
data := &m.data
data.URLParameter = ctx.URLParam(testCustomHandlerParamName)
ctx.JSON(iris.StatusOK, data)
}
func TestCustomHandler(t *testing.T) {
app := iris.New()
app.Adapt(newTestNativeRouter())
myData := myTestHandlerData{
Sysname: "Redhat",
Version: 1,
}
app.Handle("GET", "/custom_handler_1", &myTestCustomHandler{myData})
app.Handle("GET", "/custom_handler_2", &myTestCustomHandler{myData})
e := httptest.New(app, t)
// two times per testRoute
param1 := "thisimyparam1"
expectedData1 := myData
expectedData1.URLParameter = param1
e.GET("/custom_handler_1/").WithQuery(testCustomHandlerParamName, param1).Expect().Status(iris.StatusOK).JSON().Equal(expectedData1)
param2 := "thisimyparam2"
expectedData2 := myData
expectedData2.URLParameter = param2
e.GET("/custom_handler_1/").WithQuery(testCustomHandlerParamName, param2).Expect().Status(iris.StatusOK).JSON().Equal(expectedData2)
param3 := "thisimyparam3"
expectedData3 := myData
expectedData3.URLParameter = param3
e.GET("/custom_handler_2/").WithQuery(testCustomHandlerParamName, param3).Expect().Status(iris.StatusOK).JSON().Equal(expectedData3)
param4 := "thisimyparam4"
expectedData4 := myData
expectedData4.URLParameter = param4
e.GET("/custom_handler_2/").WithQuery(testCustomHandlerParamName, param4).Expect().Status(iris.StatusOK).JSON().Equal(expectedData4)
}