2020-02-29 13:18:15 +01:00
|
|
|
package hero_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/kataras/iris/v12"
|
|
|
|
. "github.com/kataras/iris/v12/hero"
|
|
|
|
"github.com/kataras/iris/v12/httptest"
|
|
|
|
)
|
|
|
|
|
|
|
|
type testStruct struct {
|
|
|
|
Ctx iris.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *testStruct) MyHandler(name string) testOutput {
|
|
|
|
return fn(42, testInput{Name: name})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *testStruct) MyHandler2(id int, in testInput) testOutput {
|
|
|
|
return fn(id, in)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *testStruct) MyHandler3(in testInput) testOutput {
|
|
|
|
return fn(42, in)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *testStruct) MyHandler4() {
|
|
|
|
c.Ctx.WriteString("MyHandler4")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStruct(t *testing.T) {
|
|
|
|
app := iris.New()
|
|
|
|
|
|
|
|
b := New()
|
2020-03-02 18:48:53 +01:00
|
|
|
s := b.Struct(&testStruct{}, 0)
|
2020-02-29 13:18:15 +01:00
|
|
|
|
2020-03-02 18:48:53 +01:00
|
|
|
postHandler := s.MethodHandler("MyHandler", 0) // fallbacks such as {path} and {string} should registered first when same path.
|
2020-02-29 13:18:15 +01:00
|
|
|
app.Post("/{name:string}", postHandler)
|
2020-03-02 18:48:53 +01:00
|
|
|
postHandler2 := s.MethodHandler("MyHandler2", 0)
|
2020-02-29 13:18:15 +01:00
|
|
|
app.Post("/{id:int}", postHandler2)
|
2020-03-02 18:48:53 +01:00
|
|
|
postHandler3 := s.MethodHandler("MyHandler3", 0)
|
2020-02-29 13:18:15 +01:00
|
|
|
app.Post("/myHandler3", postHandler3)
|
2020-03-02 18:48:53 +01:00
|
|
|
getHandler := s.MethodHandler("MyHandler4", 0)
|
2020-02-29 13:18:15 +01:00
|
|
|
app.Get("/myHandler4", getHandler)
|
|
|
|
|
|
|
|
e := httptest.New(t, app)
|
|
|
|
e.POST("/" + input.Name).Expect().Status(httptest.StatusOK).JSON().Equal(expectedOutput)
|
|
|
|
path := fmt.Sprintf("/%d", expectedOutput.ID)
|
|
|
|
e.POST(path).WithJSON(input).Expect().Status(httptest.StatusOK).JSON().Equal(expectedOutput)
|
|
|
|
e.POST("/myHandler3").WithJSON(input).Expect().Status(httptest.StatusOK).JSON().Equal(expectedOutput)
|
|
|
|
e.GET("/myHandler4").Expect().Status(httptest.StatusOK).Body().Equal("MyHandler4")
|
|
|
|
}
|
|
|
|
|
|
|
|
type testStructErrorHandler struct{}
|
|
|
|
|
|
|
|
func (s *testStructErrorHandler) HandleError(ctx iris.Context, err error) {
|
2020-05-17 23:25:38 +02:00
|
|
|
ctx.StopWithError(httptest.StatusConflict, err)
|
2020-02-29 13:18:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *testStructErrorHandler) Handle(errText string) error {
|
|
|
|
return errors.New(errText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStructErrorHandler(t *testing.T) {
|
|
|
|
b := New()
|
2020-03-02 18:48:53 +01:00
|
|
|
s := b.Struct(&testStructErrorHandler{}, 0)
|
2020-02-29 13:18:15 +01:00
|
|
|
|
|
|
|
app := iris.New()
|
2020-03-02 18:48:53 +01:00
|
|
|
app.Get("/{errText:string}", s.MethodHandler("Handle", 0))
|
2020-02-29 13:18:15 +01:00
|
|
|
|
|
|
|
expectedErrText := "an error"
|
|
|
|
e := httptest.New(t, app)
|
|
|
|
e.GET("/" + expectedErrText).Expect().Status(httptest.StatusConflict).Body().Equal(expectedErrText)
|
|
|
|
}
|
|
|
|
|
|
|
|
type (
|
|
|
|
testServiceInterface1 interface {
|
|
|
|
Parse() string
|
|
|
|
}
|
|
|
|
|
|
|
|
testServiceImpl1 struct {
|
|
|
|
inner string
|
|
|
|
}
|
|
|
|
|
|
|
|
testServiceInterface2 interface {
|
|
|
|
}
|
|
|
|
|
|
|
|
testServiceImpl2 struct {
|
|
|
|
tf int
|
|
|
|
}
|
|
|
|
|
|
|
|
testControllerDependenciesSorter struct {
|
|
|
|
Service2 testServiceInterface2
|
|
|
|
Service1 testServiceInterface1
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *testServiceImpl1) Parse() string {
|
|
|
|
return s.inner
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *testControllerDependenciesSorter) Index() string {
|
|
|
|
return fmt.Sprintf("%#+v | %#+v", c.Service1, c.Service2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStructFieldsSorter(t *testing.T) { // see https://github.com/kataras/iris/issues/1343
|
|
|
|
b := New()
|
|
|
|
b.Register(&testServiceImpl1{"parser"})
|
|
|
|
b.Register(&testServiceImpl2{24})
|
2020-03-02 18:48:53 +01:00
|
|
|
s := b.Struct(&testControllerDependenciesSorter{}, 0)
|
2020-02-29 13:18:15 +01:00
|
|
|
|
|
|
|
app := iris.New()
|
2020-03-02 18:48:53 +01:00
|
|
|
app.Get("/", s.MethodHandler("Index", 0))
|
2020-02-29 13:18:15 +01:00
|
|
|
|
|
|
|
e := httptest.New(t, app)
|
|
|
|
|
|
|
|
expectedBody := `&hero_test.testServiceImpl1{inner:"parser"} | &hero_test.testServiceImpl2{tf:24}`
|
|
|
|
e.GET("/").Expect().Status(httptest.StatusOK).Body().Equal(expectedBody)
|
|
|
|
}
|