1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-18 02:17:05 +00:00

next version preparation: hero: add a Container.Inject method to inject values outside of HTTP lifecycle, e.g. a database may be used by other services outside of Iris, the hero container (and API's Builder.GetContainer()) should provide it.

Former-commit-id: 89863055a3a3ab108a3f4b753072a35321a3a193
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-03-05 19:49:45 +02:00
parent 5ee06f9a92
commit b6445c7238
4 changed files with 133 additions and 14 deletions

View File

@@ -46,14 +46,54 @@ var (
}
)
func TestHeroHandler(t *testing.T) {
func TestContainerHandler(t *testing.T) {
app := iris.New()
b := New()
postHandler := b.Handler(fn)
c := New()
postHandler := c.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)
}
func TestContainerInject(t *testing.T) {
c := New()
expected := testInput{Name: "test"}
c.Register(expected)
c.Register(&expected)
// struct value.
var got1 testInput
if err := c.Inject(&got1); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(expected, got1) {
t.Fatalf("[struct value] expected: %#+v but got: %#+v", expected, got1)
}
// ptr.
var got2 *testInput
if err := c.Inject(&got2); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(&expected, got2) {
t.Fatalf("[ptr] expected: %#+v but got: %#+v", &expected, got2)
}
// register implementation, expect interface.
expected3 := &testServiceImpl{prefix: "prefix: "}
c.Register(expected3)
var got3 testService
if err := c.Inject(&got3); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(expected3, got3) {
t.Fatalf("[service] expected: %#+v but got: %#+v", expected3, got3)
}
}