(t *testing.T)
| 93 | } |
| 94 | |
| 95 | func TestPartyOnErrorCode(t *testing.T) { |
| 96 | app := iris.New() |
| 97 | app.Configure(iris.WithFireMethodNotAllowed) |
| 98 | |
| 99 | globalNotFoundResponse := "custom not found" |
| 100 | app.OnErrorCode(iris.StatusNotFound, func(ctx iris.Context) { |
| 101 | ctx.WriteString(globalNotFoundResponse) |
| 102 | }) |
| 103 | |
| 104 | globalMethodNotAllowedResponse := "global: method not allowed" |
| 105 | app.OnErrorCode(iris.StatusMethodNotAllowed, func(ctx iris.Context) { |
| 106 | ctx.WriteString(globalMethodNotAllowedResponse) |
| 107 | }) |
| 108 | |
| 109 | app.Get("/path", h) |
| 110 | |
| 111 | usersResponse := "users: method allowed" |
| 112 | users := app.Party("/users") |
| 113 | users.OnErrorCode(iris.StatusMethodNotAllowed, func(ctx iris.Context) { |
| 114 | ctx.WriteString(usersResponse) |
| 115 | }) |
| 116 | users.Get("/", h) |
| 117 | write400 := func(ctx iris.Context) { ctx.StatusCode(iris.StatusBadRequest) } |
| 118 | // test setting the error code from a handler. |
| 119 | users.Get("/badrequest", write400) |
| 120 | |
| 121 | usersuserResponse := "users:user: method allowed" |
| 122 | user := users.Party("/{id:int}") |
| 123 | user.OnErrorCode(iris.StatusMethodNotAllowed, func(ctx iris.Context) { |
| 124 | ctx.WriteString(usersuserResponse) |
| 125 | }) |
| 126 | usersuserNotFoundResponse := "users:user: not found" |
| 127 | user.OnErrorCode(iris.StatusNotFound, func(ctx iris.Context) { |
| 128 | ctx.WriteString(usersuserNotFoundResponse) |
| 129 | }) |
| 130 | user.Get("/", h) |
| 131 | user.Get("/ab/badrequest", write400) |
| 132 | |
| 133 | friends := users.Party("/friends") |
| 134 | friends.Get("/{id:int}", h) |
| 135 | |
| 136 | e := httptest.New(t, app) |
| 137 | |
| 138 | e.GET("/notfound").Expect().Status(iris.StatusNotFound).Body().IsEqual(globalNotFoundResponse) |
| 139 | e.POST("/path").Expect().Status(iris.StatusMethodNotAllowed).Body().IsEqual(globalMethodNotAllowedResponse) |
| 140 | e.GET("/path").Expect().Status(iris.StatusOK).Body().IsEqual("/path") |
| 141 | |
| 142 | e.POST("/users").Expect().Status(iris.StatusMethodNotAllowed). |
| 143 | Body().IsEqual(usersResponse) |
| 144 | |
| 145 | e.POST("/users/42").Expect().Status(iris.StatusMethodNotAllowed). |
| 146 | Body().IsEqual(usersuserResponse) |
| 147 | |
| 148 | e.GET("/users/42").Expect().Status(iris.StatusOK). |
| 149 | Body().IsEqual("/users/42") |
| 150 | e.GET("/users/ab").Expect().Status(iris.StatusNotFound).Body().IsEqual(usersuserNotFoundResponse) |
| 151 | // inherit the parent. |
| 152 | e.GET("/users/42/friends/dsa").Expect().Status(iris.StatusNotFound).Body().IsEqual(usersuserNotFoundResponse) |
nothing calls this directly
no test coverage detected
searching dependent graphs…