1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-22 19:36:00 +00:00

create a new package, name it as hero, I was thinking super or superb but hero is better name for what it does - the goal is to split the new 'mvc handlers' from the mvc system because they are not the same, users should know that they can use these type of rich binded handlers without controllers as well, like a normal handler and that I implemented here, the old files exist on the mvc package but will be removed at the next commit, I have to decide if we want type aliases for Result or no

Former-commit-id: cb775edc72bedc88aeab4c5a6de6bfc6bd56fae2
This commit is contained in:
Gerasimos (Makis) Maropoulos
2017-12-25 20:05:32 +02:00
parent 4ab889da5f
commit 46505f62db
43 changed files with 2680 additions and 20 deletions

152
hero/func_result_test.go Normal file
View File

@@ -0,0 +1,152 @@
package hero_test
import (
"errors"
"testing"
"github.com/kataras/iris"
"github.com/kataras/iris/context"
"github.com/kataras/iris/httptest"
. "github.com/kataras/iris/hero"
)
func GetText() string {
return "text"
}
func GetStatus() int {
return iris.StatusBadGateway
}
func GetTextWithStatusOk() (string, int) {
return "OK", iris.StatusOK
}
// tests should have output arguments mixed
func GetStatusWithTextNotOkBy(first string, second string) (int, string) {
return iris.StatusForbidden, "NOT_OK_" + first + second
}
func GetTextAndContentType() (string, string) {
return "<b>text</b>", "text/html"
}
type testCustomResult struct {
HTML string
}
// The only one required function to make that a custom Response dispatcher.
func (r testCustomResult) Dispatch(ctx context.Context) {
ctx.HTML(r.HTML)
}
func GetCustomResponse() testCustomResult {
return testCustomResult{"<b>text</b>"}
}
func GetCustomResponseWithStatusOk() (testCustomResult, int) {
return testCustomResult{"<b>OK</b>"}, iris.StatusOK
}
func GetCustomResponseWithStatusNotOk() (testCustomResult, int) {
return testCustomResult{"<b>internal server error</b>"}, iris.StatusInternalServerError
}
type testCustomStruct struct {
Name string `json:"name" xml:"name"`
Age int `json:"age" xml:"age"`
}
func GetCustomStruct() testCustomStruct {
return testCustomStruct{"Iris", 2}
}
func GetCustomStructWithStatusNotOk() (testCustomStruct, int) {
return testCustomStruct{"Iris", 2}, iris.StatusInternalServerError
}
func GetCustomStructWithContentType() (testCustomStruct, string) {
return testCustomStruct{"Iris", 2}, "text/xml"
}
func GetCustomStructWithError(ctx iris.Context) (s testCustomStruct, err error) {
s = testCustomStruct{"Iris", 2}
if ctx.URLParamExists("err") {
err = errors.New("omit return of testCustomStruct and fire error")
}
// it should send the testCustomStruct as JSON if error is nil
// otherwise it should fire the default error(BadRequest) with the error's text.
return
}
func TestFuncResult(t *testing.T) {
app := iris.New()
h := New()
// for any 'By', by is not required but we use this suffix here, like controllers
// to make it easier for the future to resolve if any bug.
// add the binding for path parameters.
app.Get("/text", h.Handler(GetText))
app.Get("/status", h.Handler(GetStatus))
app.Get("/text/with/status/ok", h.Handler(GetTextWithStatusOk))
app.Get("/status/with/text/not/ok/{first}/{second}", h.Handler(GetStatusWithTextNotOkBy))
app.Get("/text/and/content/type", h.Handler(GetTextAndContentType))
//
app.Get("/custom/response", h.Handler(GetCustomResponse))
app.Get("/custom/response/with/status/ok", h.Handler(GetCustomResponseWithStatusOk))
app.Get("/custom/response/with/status/not/ok", h.Handler(GetCustomResponseWithStatusNotOk))
//
app.Get("/custom/struct", h.Handler(GetCustomStruct))
app.Get("/custom/struct/with/status/not/ok", h.Handler(GetCustomStructWithStatusNotOk))
app.Get("/custom/struct/with/content/type", h.Handler(GetCustomStructWithContentType))
app.Get("/custom/struct/with/error", h.Handler(GetCustomStructWithError))
e := httptest.New(t, app)
e.GET("/text").Expect().Status(iris.StatusOK).
Body().Equal("text")
e.GET("/status").Expect().Status(iris.StatusBadGateway)
e.GET("/text/with/status/ok").Expect().Status(iris.StatusOK).
Body().Equal("OK")
e.GET("/status/with/text/not/ok/first/second").Expect().Status(iris.StatusForbidden).
Body().Equal("NOT_OK_firstsecond")
// Author's note: <-- if that fails means that the last binder called for both input args,
// see path_param_binder.go
e.GET("/text/and/content/type").Expect().Status(iris.StatusOK).
ContentType("text/html", "utf-8").
Body().Equal("<b>text</b>")
e.GET("/custom/response").Expect().Status(iris.StatusOK).
ContentType("text/html", "utf-8").
Body().Equal("<b>text</b>")
e.GET("/custom/response/with/status/ok").Expect().Status(iris.StatusOK).
ContentType("text/html", "utf-8").
Body().Equal("<b>OK</b>")
e.GET("/custom/response/with/status/not/ok").Expect().Status(iris.StatusInternalServerError).
ContentType("text/html", "utf-8").
Body().Equal("<b>internal server error</b>")
expectedResultFromCustomStruct := map[string]interface{}{
"name": "Iris",
"age": 2,
}
e.GET("/custom/struct").Expect().Status(iris.StatusOK).
JSON().Equal(expectedResultFromCustomStruct)
e.GET("/custom/struct/with/status/not/ok").Expect().Status(iris.StatusInternalServerError).
JSON().Equal(expectedResultFromCustomStruct)
e.GET("/custom/struct/with/content/type").Expect().Status(iris.StatusOK).
ContentType("text/xml", "utf-8")
e.GET("/custom/struct/with/error").Expect().Status(iris.StatusOK).
JSON().Equal(expectedResultFromCustomStruct)
e.GET("/custom/struct/with/error").WithQuery("err", true).Expect().
Status(iris.StatusBadRequest). // the default status code if error is not nil
// the content should be not JSON it should be the status code's text
// it will fire the error's text
Body().Equal("omit return of testCustomStruct and fire error")
}