forked from cloudfoundry/cfhttp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cf_http_test.go
109 lines (91 loc) · 3 KB
/
cf_http_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
98
99
100
101
102
103
104
105
106
107
108
109
package cfhttp_test
import (
"crypto/tls"
"crypto/x509"
"net/http"
"time"
"code.cloudfoundry.org/cfhttp"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("CfHttp", func() {
var timeout time.Duration
BeforeEach(func() {
timeout = 1 * time.Second
})
JustBeforeEach(func() {
cfhttp.Initialize(timeout)
})
Describe("NewClient", func() {
It("returns an http client", func() {
client := cfhttp.NewClient()
Expect(client.Timeout).To(Equal(timeout))
transport := client.Transport.(*http.Transport)
Expect(transport.DialContext).NotTo(BeNil())
Expect(transport.DisableKeepAlives).To(BeFalse())
})
})
Describe("NewUnixClient", func() {
It("returns an http client", func() {
client := cfhttp.NewUnixClient("socketPath")
Expect(client.Timeout).To(Equal(timeout))
transport := client.Transport.(*http.Transport)
Expect(transport.Dial).NotTo(BeNil())
})
})
Describe("NewCustomTimeoutClient", func() {
It("returns an http client with specified timeout", func() {
client := cfhttp.NewCustomTimeoutClient(5 * time.Second)
Expect(client.Timeout).To(Equal(5 * time.Second))
transport := client.Transport.(*http.Transport)
Expect(transport.DialContext).NotTo(BeNil())
Expect(transport.DisableKeepAlives).To(BeFalse())
})
})
Describe("NewStreamingClient", func() {
It("returns an http client", func() {
client := cfhttp.NewStreamingClient()
Expect(client.Timeout).To(BeZero())
transport := client.Transport.(*http.Transport)
Expect(transport.DialContext).NotTo(BeNil())
Expect(transport.DisableKeepAlives).To(BeFalse())
})
})
Describe("NewTLSConfig", func() {
var certFixture, keyFixture, caCertFixture string
BeforeEach(func() {
certFixture = "fixtures/cert.crt"
keyFixture = "fixtures/cert.key"
caCertFixture = "fixtures/cacert.crt"
})
It("requires TLS Version 1.2", func() {
tlsConfig, err := cfhttp.NewTLSConfig(certFixture, keyFixture, caCertFixture)
Expect(err).NotTo(HaveOccurred())
Expect(tlsConfig.MinVersion).To(BeEquivalentTo(tls.VersionTLS12))
})
It("requires certain cipher suites", func() {
tlsConfig, err := cfhttp.NewTLSConfig(certFixture, keyFixture, caCertFixture)
Expect(err).NotTo(HaveOccurred())
Expect(tlsConfig.CipherSuites).To(Equal(cfhttp.SUPPORTED_CIPHER_SUITES))
})
})
Describe("NewTLSConfigWithCertPool", func() {
var certFixture, keyFixture string
var caCertPool *x509.CertPool
BeforeEach(func() {
certFixture = "fixtures/cert.crt"
keyFixture = "fixtures/cert.key"
caCertPool = x509.NewCertPool()
})
It("sets the Root and Client CAs", func() {
tlsConfig, err := cfhttp.NewTLSConfigWithCertPool(certFixture, keyFixture, caCertPool)
Expect(err).NotTo(HaveOccurred())
Expect(tlsConfig.RootCAs).To(Equal(caCertPool))
Expect(tlsConfig.ClientCAs).To(Equal(caCertPool))
})
It("should error when the caCert is nil", func() {
_, err := cfhttp.NewTLSConfigWithCertPool(certFixture, keyFixture, nil)
Expect(err).To(HaveOccurred())
})
})
})