-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
methodoverride_test.go
76 lines (61 loc) · 2.38 KB
/
methodoverride_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
package methodoverride_test
import (
"testing"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/httptest"
"github.com/kataras/iris/v12/middleware/methodoverride"
)
func TestMethodOverrideWrapper(t *testing.T) {
app := iris.New()
mo := methodoverride.New(
// Defaults to nil.
//
methodoverride.SaveOriginalMethod("_originalMethod"),
// Default values.
//
// methodoverride.Methods(http.MethodPost),
// methodoverride.Headers("X-HTTP-Method", "X-HTTP-Method-Override", "X-Method-Override"),
// methodoverride.FormField("_method"),
// methodoverride.Query("_method"),
)
// Register it with `WrapRouter`.
app.WrapRouter(mo)
var (
expectedDelResponse = "delete resp"
expectedPostResponse = "post resp"
)
app.Post("/path", func(ctx iris.Context) {
ctx.WriteString(expectedPostResponse)
})
app.Delete("/path", func(ctx iris.Context) {
ctx.WriteString(expectedDelResponse)
})
app.Delete("/path2", func(ctx iris.Context) {
_, err := ctx.Writef("%s%s", expectedDelResponse, ctx.Request().Context().Value("_originalMethod"))
if err != nil {
t.Fatal(err)
}
})
e := httptest.New(t, app)
// Test headers.
e.POST("/path").WithHeader("X-HTTP-Method", iris.MethodDelete).Expect().
Status(iris.StatusOK).Body().IsEqual(expectedDelResponse)
e.POST("/path").WithHeader("X-HTTP-Method-Override", iris.MethodDelete).Expect().
Status(iris.StatusOK).Body().IsEqual(expectedDelResponse)
e.POST("/path").WithHeader("X-Method-Override", iris.MethodDelete).Expect().
Status(iris.StatusOK).Body().IsEqual(expectedDelResponse)
// Test form field value.
e.POST("/path").WithFormField("_method", iris.MethodDelete).Expect().
Status(iris.StatusOK).Body().IsEqual(expectedDelResponse)
// Test URL Query (although it's the same as form field in this case).
e.POST("/path").WithQuery("_method", iris.MethodDelete).Expect().
Status(iris.StatusOK).Body().IsEqual(expectedDelResponse)
// Test saved original method and
// Test without registered "POST" route.
e.POST("/path2").WithQuery("_method", iris.MethodDelete).Expect().
Status(iris.StatusOK).Body().IsEqual(expectedDelResponse + iris.MethodPost)
// Test simple POST request without method override fields.
e.POST("/path").Expect().Status(iris.StatusOK).Body().IsEqual(expectedPostResponse)
// Test simple DELETE request.
e.DELETE("/path").Expect().Status(iris.StatusOK).Body().IsEqual(expectedDelResponse)
}