/
fixture.go
47 lines (41 loc) · 1.37 KB
/
fixture.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
package httpext_test
import (
"github.com/dbsystel/kewl/pkg/httpext"
"github.com/dbsystel/kewl/pkg/panicutils"
"github.com/go-logr/logr"
)
// Fixture is a fixture for testing httpext.Server
type Fixture struct {
// Sut is the system under test
Sut *httpext.Server
// Test is the TestFacade for testing requests
Test *TestFacade
}
// NewFixture creates a new Fixture for testing httpext.Server
func NewFixture() *Fixture {
return newFixture(false)
}
// NewFixtureHTTPS creates a new Fixture for testing httpext.Server using HTTPS
func NewFixtureHTTP() *Fixture {
return newFixture(true)
}
// newFixture creates a new Fixture with either HTTPS activated or deactivated
func newFixture(enableHTTPS bool) *Fixture {
config := TestConfig()
if enableHTTPS {
pemFilePair := MustNewTmpPemFilePair()
panicutils.PanicIfError(pemFilePair.Generate(CACertificate))
config.TLSConfig.MinVersion = "1.2"
config.TLSConfig.PublicKeyFile = pemFilePair.PublicKeyPath()
config.TLSConfig.PrivateKeyFile = pemFilePair.PrivateKeyPath()
}
server, err := httpext.NewServer(logr.DiscardLogger{}, config)
panicutils.PanicIfError(err)
return &Fixture{Sut: server, Test: &TestFacade{ServeMux: server.ServeMux}}
}
// TestConfig creates a new httpext.Config for testing
func TestConfig() *httpext.Config {
config := httpext.NewDefaultConfig()
config.TLSConfig = httpext.TLSConfig{}
return &config
}