mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 10:27:06 +00:00
❤️ awesome and unique features for end-developers are coming...
total refactor of the hero and mvc packages, see README#Next (it's not completed yet) Former-commit-id: b85ae99cbfe5965ba919c1e15cf4989e787982c0
This commit is contained in:
123
hero/struct_test.go
Normal file
123
hero/struct_test.go
Normal file
@@ -0,0 +1,123 @@
|
||||
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()
|
||||
s := b.Struct(&testStruct{})
|
||||
|
||||
postHandler := s.MethodHandler("MyHandler") // fallbacks such as {path} and {string} should registered first when same path.
|
||||
app.Post("/{name:string}", postHandler)
|
||||
postHandler2 := s.MethodHandler("MyHandler2")
|
||||
app.Post("/{id:int}", postHandler2)
|
||||
postHandler3 := s.MethodHandler("MyHandler3")
|
||||
app.Post("/myHandler3", postHandler3)
|
||||
getHandler := s.MethodHandler("MyHandler4")
|
||||
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) {
|
||||
ctx.StatusCode(httptest.StatusConflict)
|
||||
ctx.WriteString(err.Error())
|
||||
ctx.StopExecution()
|
||||
}
|
||||
|
||||
func (s *testStructErrorHandler) Handle(errText string) error {
|
||||
return errors.New(errText)
|
||||
}
|
||||
|
||||
func TestStructErrorHandler(t *testing.T) {
|
||||
b := New()
|
||||
s := b.Struct(&testStructErrorHandler{})
|
||||
|
||||
app := iris.New()
|
||||
app.Get("/{errText:string}", s.MethodHandler("Handle"))
|
||||
|
||||
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})
|
||||
s := b.Struct(&testControllerDependenciesSorter{})
|
||||
|
||||
app := iris.New()
|
||||
app.Get("/", s.MethodHandler("Index"))
|
||||
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user