5
5
package proxytest
6
6
7
7
import (
8
+ "crypto/tls"
8
9
"fmt"
9
10
"io"
10
11
"log"
@@ -42,8 +43,9 @@ type options struct {
42
43
rewriteHost func (string ) string
43
44
rewriteURL func (u * url.URL )
44
45
// logFn if set will be used to log every request.
45
- logFn func (format string , a ... any )
46
- verbose bool
46
+ logFn func (format string , a ... any )
47
+ verbose bool
48
+ serverTLSConfig * tls.Config
47
49
}
48
50
49
51
// WithAddress will set the address the server will listen on. The format is as
@@ -90,6 +92,12 @@ func WithRewriteFn(f func(u *url.URL)) Option {
90
92
}
91
93
}
92
94
95
+ func WithServerTLSConfig (tc * tls.Config ) Option {
96
+ return func (o * options ) {
97
+ o .serverTLSConfig = tc
98
+ }
99
+ }
100
+
93
101
// New returns a new Proxy ready for use. Use:
94
102
// - WithAddress to set the proxy's address,
95
103
// - WithRewrite or WithRewriteFn to rewrite the URL before forwarding the request.
@@ -114,10 +122,8 @@ func New(t *testing.T, optns ...Option) *Proxy {
114
122
115
123
p := Proxy {opts : opts }
116
124
117
- p .Server = & httptest.Server {
118
- Listener : l ,
119
- //nolint:gosec,nolintlint // it's a test
120
- Config : & http.Server {Handler : http .HandlerFunc (func (ww http.ResponseWriter , r * http.Request ) {
125
+ p .Server = httptest .NewUnstartedServer (
126
+ http .HandlerFunc (func (ww http.ResponseWriter , r * http.Request ) {
121
127
w := & statusResponseWriter {w : ww }
122
128
123
129
requestID := uuid .New ().String ()
@@ -128,8 +134,13 @@ func New(t *testing.T, optns ...Option) *Proxy {
128
134
129
135
opts .logFn (fmt .Sprintf ("[%s] DONE %d - %s %s %s %s\n " ,
130
136
requestID , w .statusCode , r .Method , r .URL , r .Proto , r .RemoteAddr ))
131
- })}}
132
- p .Start ()
137
+ }),
138
+ )
139
+ p .Server .Listener = l
140
+
141
+ if opts .serverTLSConfig != nil {
142
+ p .Server .TLS = opts .serverTLSConfig
143
+ }
133
144
134
145
u , err := url .Parse (p .URL )
135
146
if err != nil {
@@ -143,6 +154,34 @@ func New(t *testing.T, optns ...Option) *Proxy {
143
154
return & p
144
155
}
145
156
157
+ func (p * Proxy ) Start () error {
158
+ p .Server .Start ()
159
+ u , err := url .Parse (p .URL )
160
+ if err != nil {
161
+ return fmt .Errorf ("could not parse fleet-server URL: %w" , err )
162
+ }
163
+
164
+ p .Port = u .Port ()
165
+ p .LocalhostURL = "http://localhost:" + p .Port
166
+
167
+ p .opts .logFn ("running on %s -> %s" , p .URL , p .LocalhostURL )
168
+ return nil
169
+ }
170
+
171
+ func (p * Proxy ) StartTLS () error {
172
+ p .Server .StartTLS ()
173
+ u , err := url .Parse (p .URL )
174
+ if err != nil {
175
+ return fmt .Errorf ("could not parse fleet-server URL: %w" , err )
176
+ }
177
+
178
+ p .Port = u .Port ()
179
+ p .LocalhostURL = "http://localhost:" + p .Port
180
+
181
+ p .opts .logFn ("running on %s -> %s" , p .URL , p .LocalhostURL )
182
+ return nil
183
+ }
184
+
146
185
func (p * Proxy ) ServeHTTP (w http.ResponseWriter , r * http.Request ) {
147
186
origURL := r .URL .String ()
148
187
0 commit comments