1
0
mirror of https://github.com/oauth2-proxy/oauth2-proxy.git synced 2025-04-19 12:12:39 +02:00
oauth2-proxy/pkg/http/http_suite_test.go

62 lines
1.6 KiB
Go
Raw Normal View History

2021-02-14 13:50:35 +00:00
package http
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"net/http"
"testing"
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/util"
2021-02-14 13:50:35 +00:00
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var certData []byte
var certDataSource, keyDataSource options.SecretSource
var client *http.Client
func TestHTTPSuite(t *testing.T) {
logger.SetOutput(GinkgoWriter)
logger.SetErrOutput(GinkgoWriter)
RegisterFailHandler(Fail)
RunSpecs(t, "HTTP")
}
var _ = BeforeSuite(func() {
By("Generating a self-signed cert for TLS tests", func() {
certBytes, keyBytes, err := util.GenerateCert()
2021-02-14 13:50:35 +00:00
Expect(err).ToNot(HaveOccurred())
certData = certBytes
certOut := new(bytes.Buffer)
2021-02-14 13:50:35 +00:00
Expect(pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: certBytes})).To(Succeed())
certDataSource.Value = certOut.Bytes()
keyOut := new(bytes.Buffer)
Expect(pem.Encode(keyOut, &pem.Block{Type: "PRIVATE KEY", Bytes: keyBytes})).To(Succeed())
keyDataSource.Value = keyOut.Bytes()
2021-02-14 13:50:35 +00:00
})
By("Setting up a http client", func() {
cert, err := tls.X509KeyPair(certDataSource.Value, keyDataSource.Value)
Expect(err).ToNot(HaveOccurred())
certificate, err := x509.ParseCertificate(cert.Certificate[0])
Expect(err).ToNot(HaveOccurred())
certpool := x509.NewCertPool()
certpool.AddCert(certificate)
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.TLSClientConfig.RootCAs = certpool
client = &http.Client{
Transport: transport,
}
})
})