-
Notifications
You must be signed in to change notification settings - Fork 51
/
client_test.go
77 lines (58 loc) · 1.79 KB
/
client_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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package gokong
import (
"context"
"fmt"
"log"
"net/http"
"os"
"testing"
"github.com/phayes/freeport"
"github.com/kevholditch/gokong/containers"
"github.com/stretchr/testify/assert"
)
const defaultKongVersion = "2.5.0-ubuntu"
const kong401Server = "KONG_401_SERVER"
func Test_Newclient(t *testing.T) {
result := NewClient(NewDefaultConfig())
assert.NotNil(t, result)
assert.Equal(t, os.Getenv(EnvKongAdminHostAddress), result.config.HostAddress)
assert.Equal(t, os.Getenv(EnvKongAdminUsername), result.config.Username)
assert.Equal(t, os.Getenv(EnvKongAdminPassword), result.config.Password)
}
func TestMain(m *testing.M) {
testContext := containers.StartKong(GetEnvVarOrDefault("KONG_VERSION", defaultKongVersion))
err := os.Setenv(EnvKongAdminHostAddress, testContext.KongHostAddress)
if err != nil {
log.Fatalf("Could not set kong host address env variable: %v", err)
}
stopSignal := make(chan bool)
serverPort, _ := freeport.GetFreePort()
err = os.Setenv(kong401Server, fmt.Sprintf("http://localhost:%d", serverPort))
if err != nil {
log.Fatalf("Could not set kong api host address env variable: %v", err)
}
go func() { StartServer(serverPort, stopSignal) }()
code := m.Run()
containers.StopKong(testContext)
os.Exit(code)
}
type serveHttp struct {
code int
}
func (s *serveHttp) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(s.code)
}
func StartServer(port int, ch <-chan bool) {
address := fmt.Sprintf(":%d", port)
server := &http.Server{Addr: address, Handler: &serveHttp{code: 401}}
go func() {
fmt.Printf("Server started on %s \n", address)
<-ch
fmt.Printf("Shutting down \n")
server.Shutdown(context.Background())
}()
fmt.Printf("listening on localhost:%d \n", port)
if err := server.ListenAndServe(); err != nil {
log.Fatal(err)
}
}