iris/hero/container_test.go
Gerasimos (Makis) Maropoulos 5fc24812bc ❤️ 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
2020-02-29 14:18:15 +02:00

60 lines
1010 B
Go

package hero_test
import (
"fmt"
"reflect"
"testing"
"github.com/kataras/iris/v12"
. "github.com/kataras/iris/v12/hero"
"github.com/kataras/iris/v12/httptest"
)
var errTyp = reflect.TypeOf((*error)(nil)).Elem()
// isError returns true if "typ" is type of `error`.
func isError(typ reflect.Type) bool {
return typ.Implements(errTyp)
}
type (
testInput struct {
Name string `json:"name"`
}
testOutput struct {
ID int `json:"id"`
Name string `json:"name"`
}
)
var (
fn = func(id int, in testInput) testOutput {
return testOutput{
ID: id,
Name: in.Name,
}
}
expectedOutput = testOutput{
ID: 42,
Name: "makis",
}
input = testInput{
Name: "makis",
}
)
func TestHeroHandler(t *testing.T) {
app := iris.New()
b := New()
postHandler := b.Handler(fn)
app.Post("/{id:int}", postHandler)
e := httptest.New(t, app)
path := fmt.Sprintf("/%d", expectedOutput.ID)
e.POST(path).WithJSON(input).Expect().Status(httptest.StatusOK).JSON().Equal(expectedOutput)
}