-
Notifications
You must be signed in to change notification settings - Fork 0
/
container_test.go
60 lines (47 loc) · 1.03 KB
/
container_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package dependencyinjection
import (
"fmt"
"github.com/stretchr/testify/require"
"net"
"net/http"
"testing"
)
func TestContainer(t *testing.T) {
var HTTPBundle = Bundle(
Provide(ProvideAddr("0.0.0.0", "8080")),
Provide(NewMux, As(new(http.Handler))),
Provide(NewHTTPServer, Prototype(), WithName("server")),
)
c := New(HTTPBundle)
var server1 *http.Server
err := c.Extract(&server1, Name("server"))
require.NoError(t, err)
var server2 *http.Server
err = c.Extract(&server2, Name("server"))
require.NoError(t, err)
err = c.Invoke(PrintAddr)
require.NoError(t, err)
}
// Addr
type Addr string
// ProvideAddr
func ProvideAddr(host string, port string) func() Addr {
return func() Addr {
return Addr(net.JoinHostPort(host, port))
}
}
// NewHTTPServer
func NewHTTPServer(addr Addr, handler http.Handler) *http.Server {
return &http.Server{
Addr: string(addr),
Handler: handler,
}
}
// NewMux
func NewMux() *http.ServeMux {
return &http.ServeMux{}
}
// PrintAddr
func PrintAddr(addr Addr) {
fmt.Println(addr)
}