-
Notifications
You must be signed in to change notification settings - Fork 21
/
client_tls_test.go
97 lines (81 loc) · 2.32 KB
/
client_tls_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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package routing_api_test
import (
"bytes"
"encoding/json"
"net/http"
routing_api "code.cloudfoundry.org/routing-api"
"code.cloudfoundry.org/routing-api/models"
"code.cloudfoundry.org/routing-api/trace"
"github.com/vito/go-sse/sse"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/ghttp"
)
var _ = Describe("Client", func() {
const (
ROUTES_API_URL = "/routing/v1/routes"
EVENTS_SSE_URL = "/routing/v1/events"
)
var server *ghttp.Server
var client routing_api.Client
var stdout *bytes.Buffer
BeforeEach(func() {
stdout = bytes.NewBuffer([]byte{})
trace.SetStdout(stdout)
trace.Logger = trace.NewLogger("true")
})
BeforeEach(func() {
server = ghttp.NewTLSServer()
data, _ := json.Marshal([]models.Route{})
server.RouteToHandler("GET", ROUTES_API_URL,
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", ROUTES_API_URL),
ghttp.RespondWith(http.StatusOK, data),
),
)
event := sse.Event{
ID: "1",
Name: "Upsert",
Data: data,
}
headers := make(http.Header)
headers.Set("Content-Type", "text/event-stream; charset=utf-8")
server.RouteToHandler("GET", EVENTS_SSE_URL,
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", EVENTS_SSE_URL),
ghttp.RespondWith(http.StatusOK, event.Encode(), headers),
),
)
})
AfterEach(func() {
server.Close()
})
Context("without skip SSL validation", func() {
BeforeEach(func() {
client = routing_api.NewClient(server.URL(), false)
})
It("fails to connect to the Routing API", func() {
_, err := client.Routes()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("x509: certificate signed by unknown authority"))
})
It("fails to stream events from the Routing API", func() {
_, err := client.SubscribeToEventsWithMaxRetries(1)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("x509: certificate signed by unknown authority"))
})
})
Context("with skip SSL validation", func() {
BeforeEach(func() {
client = routing_api.NewClient(server.URL(), true)
})
It("successfully connect to the Routing API", func() {
_, err := client.Routes()
Expect(err).ToNot(HaveOccurred())
})
It("streams events from the Routing API", func() {
_, err := client.SubscribeToEventsWithMaxRetries(1)
Expect(err).NotTo(HaveOccurred())
})
})
})