@@ -15,32 +15,77 @@ import (
15
15
"time"
16
16
17
17
"github.com/nginx/agent/v3/internal/config"
18
+ "github.com/stretchr/testify/assert"
18
19
"github.com/stretchr/testify/require"
19
20
)
20
21
21
- func TestDialViaHTTPProxy_NoProxy (t * testing.T ) {
22
- // This test attempts to connect directly to a known open port (localhost:80)
23
- ctx , cancel := context .WithTimeout (context .Background (), 2 * time .Second )
24
- defer cancel ()
25
-
26
- proxyConf := & config.Proxy {
27
- URL : "" ,
28
- Timeout : 2 * time .Second ,
22
+ func TestDialViaHTTPProxy_ErrorScenarios (t * testing.T ) {
23
+ tests := []struct {
24
+ expectedErr func (error ) bool
25
+ proxyConf * config.Proxy
26
+ name string
27
+ dialAddress string
28
+ timeout time.Duration
29
+ }{
30
+ {
31
+ name : "Test 1: Invalid CA Path" ,
32
+ proxyConf : & config.Proxy {
33
+ URL : "https://localhost:9999" ,
34
+ TLS : & config.TLSConfig {Ca : "/invalid/path/to/ca.pem" },
35
+ },
36
+ dialAddress : "example.com:443" ,
37
+ expectedErr : func (err error ) bool { return err != nil },
38
+ timeout : 1 * time .Second ,
39
+ },
40
+ {
41
+ name : "Test 2: Missing TLS Cert/Key" ,
42
+ proxyConf : & config.Proxy {
43
+ URL : "https://localhost:9999" ,
44
+ TLS : & config.TLSConfig {},
45
+ },
46
+ dialAddress : "example.com:443" ,
47
+ expectedErr : func (err error ) bool { return err != nil },
48
+ timeout : 1 * time .Second ,
49
+ },
50
+ {
51
+ name : "Test 3: Invalid Proxy URL Format" ,
52
+ proxyConf : & config.Proxy {
53
+ URL : "://bad-url" ,
54
+ },
55
+ dialAddress : "example.com:443" ,
56
+ expectedErr : func (err error ) bool { return err != nil },
57
+ timeout : 1 * time .Second ,
58
+ },
59
+ {
60
+ name : "Test 4: No Proxy URL (Direct connection expected to fail for invalid address)" ,
61
+ proxyConf : & config.Proxy {
62
+ URL : "" ,
63
+ },
64
+ dialAddress : "localhost:80" ,
65
+ expectedErr : func (err error ) bool { return err != nil },
66
+ timeout : 2 * time .Second ,
67
+ },
68
+ {
69
+ name : "Test 5: Invalid Proxy Address (Unresolvable/Unavailable Host)" ,
70
+ proxyConf : & config.Proxy {
71
+ URL : "http://invalid:9999" ,
72
+ },
73
+ dialAddress : "localhost:80" ,
74
+ expectedErr : func (err error ) bool { return err != nil },
75
+ timeout : 2 * time .Second ,
76
+ },
29
77
}
30
- _ , err := DialViaHTTPProxy (ctx , proxyConf , "localhost:80" )
31
- require .Error (t , err , "expected failure with empty proxy URL" )
32
- }
33
78
34
- func TestDialViaHTTPProxy_InvalidProxy (t * testing.T ) {
35
- ctx , cancel := context .WithTimeout (context .Background (), 2 * time .Second )
36
- defer cancel ()
79
+ for _ , test := range tests {
80
+ t .Run (test .name , func (t * testing.T ) {
81
+ ctx , cancel := context .WithTimeout (context .Background (), test .timeout )
82
+ defer cancel ()
37
83
38
- proxyConf := & config.Proxy {
39
- URL : "http://invalid:9999" ,
40
- Timeout : 2 * time .Second ,
84
+ _ , err := DialViaHTTPProxy (ctx , test .proxyConf , test .dialAddress )
85
+ require .Error (t , err , "expected error for scenario: %s" , test .name )
86
+ assert .True (t , test .expectedErr (err ), "error did not match expected criteria: %v" , err )
87
+ })
41
88
}
42
- _ , err := DialViaHTTPProxy (ctx , proxyConf , "localhost:80" )
43
- require .Error (t , err , "expected failure with invalid proxy" )
44
89
}
45
90
46
91
// To fully test with a real proxy, set the env var TEST_HTTP_PROXY_URL and have a proxy listening.
@@ -157,38 +202,3 @@ func hasBearerHeader(headerLines []string, token string) bool {
157
202
158
203
return false
159
204
}
160
-
161
- func TestDialViaHTTPProxy_InvalidCAPath (t * testing.T ) {
162
- proxyConf := & config.Proxy {
163
- URL : "https://localhost:9999" ,
164
- TLS : & config.TLSConfig {Ca : "/invalid/path/to/ca.pem" },
165
- Timeout : 1 * time .Second ,
166
- }
167
- ctx , cancel := context .WithTimeout (context .Background (), 1 * time .Second )
168
- defer cancel ()
169
- _ , err := DialViaHTTPProxy (ctx , proxyConf , "example.com:443" )
170
- require .Error (t , err , "expected error for invalid CA path" )
171
- }
172
-
173
- func TestDialViaHTTPProxy_MissingCertKey (t * testing.T ) {
174
- proxyConf := & config.Proxy {
175
- URL : "https://localhost:9999" ,
176
- TLS : & config.TLSConfig {}, // No cert/key
177
- Timeout : 1 * time .Second ,
178
- }
179
- ctx , cancel := context .WithTimeout (context .Background (), 1 * time .Second )
180
- defer cancel ()
181
- _ , err := DialViaHTTPProxy (ctx , proxyConf , "example.com:443" )
182
- require .Error (t , err , "expected error for missing cert" )
183
- }
184
-
185
- func TestDialViaHTTPProxy_InvalidProxyURL (t * testing.T ) {
186
- proxyConf := & config.Proxy {
187
- URL : "://bad-url" ,
188
- Timeout : 1 * time .Second ,
189
- }
190
- ctx , cancel := context .WithTimeout (context .Background (), 1 * time .Second )
191
- defer cancel ()
192
- _ , err := DialViaHTTPProxy (ctx , proxyConf , "example.com:443" )
193
- require .Error (t , err , "expected error for invalid proxy URL" )
194
- }
0 commit comments