-
Notifications
You must be signed in to change notification settings - Fork 0
/
options_test.go
146 lines (118 loc) · 4.12 KB
/
options_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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package servex_test
import (
"crypto/tls"
"net/http"
"reflect"
"testing"
"time"
"github.com/maxbolgarin/servex"
)
// TestWithCertificate tests whether the WithCertificate option sets the TLS certificate correctly.
func TestWithCertificate(t *testing.T) {
cert := tls.Certificate{}
option := servex.WithCertificate(cert)
options := servex.Options{}
option(&options)
if !reflect.DeepEqual(options.Certificate, &cert) {
t.Errorf("expected certificate to be %v, got %v", cert, options.Certificate)
}
}
// TestWithReadTimeout verifies the WithReadTimeout sets the ReadTimeout properly.
func TestWithReadTimeout(t *testing.T) {
timeout := 30 * time.Second
option := servex.WithReadTimeout(timeout)
options := servex.Options{}
option(&options)
// It should set to the provided timeout or default if timeout <= 0
if options.ReadTimeout != timeout {
t.Errorf("expected timeout %v, got %v", timeout, options.ReadTimeout)
}
}
// TestWithReadHeaderTimeout verifies the WithReadHeaderTimeout sets the ReadHeaderTimeout.
func TestWithReadHeaderTimeout(t *testing.T) {
timeout := 15 * time.Second
option := servex.WithReadHeaderTimeout(timeout)
options := servex.Options{}
option(&options)
// It should set to the provided timeout
if options.ReadHeaderTimeout != timeout {
t.Errorf("expected timeout %v, got %v", timeout, options.ReadHeaderTimeout)
}
}
// TestWithIdleTimeout verifies the WithIdleTimeout sets the IdleTimeout properly.
func TestWithIdleTimeout(t *testing.T) {
timeout := 90 * time.Second
option := servex.WithIdleTimeout(timeout)
options := servex.Options{}
option(&options)
if options.IdleTimeout != timeout {
t.Errorf("expected idle timeout %v, got %v", timeout, options.IdleTimeout)
}
}
// TestWithAuthToken verifies the WithAuthToken sets the token correctly.
func TestWithAuthToken(t *testing.T) {
token := "securetoken"
option := servex.WithAuthToken(token)
options := servex.Options{}
option(&options)
if options.AuthToken != token {
t.Errorf("expected token %q, got %q", token, options.AuthToken)
}
}
// TestWithMetrics verifies that the WithMetrics option sets the Metrics interface.
func TestWithMetrics(t *testing.T) {
metrics := &mockMetrics{}
option := servex.WithMetrics(metrics)
options := servex.Options{}
option(&options)
if options.Metrics != metrics {
t.Errorf("expected metrics %v, got %v", metrics, options.Metrics)
}
}
// TestWithLogger verifies that the WithLogger option sets the Logger.
func TestWithLogger(t *testing.T) {
logger := &mockLogger{}
option := servex.WithLogger(logger)
options := servex.Options{}
option(&options)
if options.Logger != logger {
t.Errorf("expected logger %v, got %v", logger, options.Logger)
}
}
// TestWithRequestLogger verifies that the WithRequestLogger option sets the RequestLogger.
func TestWithRequestLogger(t *testing.T) {
reqLogger := &mockRequestLogger{}
option := servex.WithRequestLogger(reqLogger)
options := servex.Options{}
option(&options)
if options.RequestLogger != reqLogger {
t.Errorf("expected request logger %v, got %v", reqLogger, options.RequestLogger)
}
}
func TestBaseConfigValidate(t *testing.T) {
tests := []struct {
config servex.BaseConfig
hasError bool
}{
{servex.BaseConfig{HTTP: "", HTTPS: ""}, true},
{servex.BaseConfig{HTTP: ":8080", HTTPS: ""}, false},
{servex.BaseConfig{HTTP: "", HTTPS: ":8443", CertFile: "cert.pem", KeyFile: "key.pem"}, false},
{servex.BaseConfig{HTTP: "invalid", HTTPS: ""}, true},
{servex.BaseConfig{HTTP: "", HTTPS: "invalid"}, true},
{servex.BaseConfig{HTTP: "", HTTPS: ":8443"}, false},
}
for _, tt := range tests {
err := tt.config.Validate()
if (err != nil) != tt.hasError {
t.Errorf("Validate() with config %v expected error: %v, got: %v", tt.config, tt.hasError, err)
}
}
}
type mockMetrics struct{}
func (m *mockMetrics) HandleRequest(r *http.Request) {}
type mockLogger struct{}
func (m *mockLogger) Error(msg string, args ...interface{}) {}
func (m *mockLogger) Info(msg string, args ...interface{}) {}
func (m *mockLogger) Debug(msg string, args ...interface{}) {}
type mockRequestLogger struct{}
func (m *mockRequestLogger) Log(servex.RequestLogBundle) {}