19
19
import org .asynchttpclient .AsyncHttpClient ;
20
20
import org .asynchttpclient .AsyncHttpClientConfig ;
21
21
import org .asynchttpclient .Response ;
22
+ import org .junit .jupiter .api .AfterAll ;
22
23
import org .junit .jupiter .api .BeforeAll ;
23
24
import org .junit .jupiter .api .Test ;
24
25
import org .slf4j .Logger ;
28
29
import org .testcontainers .containers .output .Slf4jLogConsumer ;
29
30
import org .testcontainers .containers .wait .strategy .Wait ;
30
31
import org .testcontainers .images .builder .ImageFromDockerfile ;
31
- import org .testcontainers .junit .jupiter .Container ;
32
32
import org .testcontainers .junit .jupiter .Testcontainers ;
33
33
34
34
import java .nio .file .Path ;
47
47
public class HttpsProxyTestcontainersIntegrationTest {
48
48
49
49
private static final Logger LOGGER = LoggerFactory .getLogger (HttpsProxyTestcontainersIntegrationTest .class );
50
-
50
+
51
51
private static final int SQUID_HTTP_PORT = 3128 ;
52
52
private static final int SQUID_HTTPS_PORT = 3129 ;
53
-
53
+
54
54
private static final String TARGET_HTTP_URL = "http://httpbin.org/get" ;
55
55
private static final String TARGET_HTTPS_URL = "https://www.example.com/" ;
56
-
56
+
57
57
private static boolean dockerAvailable = false ;
58
-
58
+ private static GenericContainer <?> squidProxy ;
59
+
59
60
@ BeforeAll
60
61
static void checkDockerAvailability () {
61
62
try {
@@ -65,136 +66,126 @@ static void checkDockerAvailability() {
65
66
LOGGER .warn ("Failed to check Docker availability: {}" , e .getMessage ());
66
67
dockerAvailable = false ;
67
68
}
68
-
69
69
// Skip tests if Docker not available, unless force-enabled
70
70
if (!dockerAvailable && !"true" .equals (System .getProperty ("docker.tests" ))) {
71
71
assumeTrue (false , "Docker is not available - skipping integration tests. Use -Ddocker.tests=true to force run." );
72
72
}
73
-
74
73
// Allow force-disabling Docker tests
75
74
if ("true" .equals (System .getProperty ("no.docker.tests" ))) {
76
75
assumeTrue (false , "Docker tests disabled via -Dno.docker.tests=true" );
77
76
}
77
+ // Only start container if Docker is available
78
+ if (dockerAvailable ) {
79
+ squidProxy = new GenericContainer <>(
80
+ new ImageFromDockerfile ()
81
+ .withFileFromPath ("Dockerfile" , Path .of ("src/test/resources/squid/Dockerfile" ))
82
+ .withFileFromPath ("squid.conf" , Path .of ("src/test/resources/squid/squid.conf" ))
83
+ )
84
+ .withExposedPorts (SQUID_HTTP_PORT , SQUID_HTTPS_PORT )
85
+ .withLogConsumer (new Slf4jLogConsumer (LOGGER ).withPrefix ("SQUID" ))
86
+ .waitingFor (Wait .forLogMessage (".*Accepting HTTP.*" , 1 )
87
+ .withStartupTimeout (Duration .ofMinutes (2 )));
88
+ squidProxy .start ();
89
+ }
78
90
}
79
91
80
- /**
81
- * Self-contained Squid proxy container that generates its own certificates and configuration.
82
- * The container is automatically started before tests and stopped after tests.
83
- */
84
- @ Container
85
- static final GenericContainer <?> squidProxy = new GenericContainer <>(
86
- new ImageFromDockerfile ()
87
- .withFileFromPath ("Dockerfile" , Path .of ("src/test/resources/squid/Dockerfile" ))
88
- .withFileFromPath ("squid.conf" , Path .of ("src/test/resources/squid/squid.conf" ))
89
- )
90
- .withExposedPorts (SQUID_HTTP_PORT , SQUID_HTTPS_PORT )
91
- .withLogConsumer (new Slf4jLogConsumer (LOGGER ).withPrefix ("SQUID" ))
92
- .waitingFor (Wait .forLogMessage (".*Accepting HTTP.*" , 1 )
93
- .withStartupTimeout (Duration .ofMinutes (2 )));
92
+ @ AfterAll
93
+ static void stopContainer () {
94
+ if (squidProxy != null && squidProxy .isRunning ()) {
95
+ squidProxy .stop ();
96
+ }
97
+ }
94
98
95
99
@ RepeatedIfExceptionsTest (repeats = 3 )
96
100
public void testHttpProxyToHttpTarget () throws Exception {
101
+ assumeTrue (dockerAvailable , "Docker is not available - skipping test" );
97
102
LOGGER .info ("Testing HTTP proxy to HTTP target" );
98
-
99
103
AsyncHttpClientConfig config = config ()
100
- .setProxyServer (proxyServer ("localhost" , squidProxy .getMappedPort (SQUID_HTTP_PORT ))
101
- .setProxyType (ProxyType .HTTP )
102
- .build ())
103
- .setConnectTimeout (Duration .ofMillis (10000 ))
104
- .setRequestTimeout (Duration .ofMillis (30000 ))
105
- .build ();
106
-
104
+ .setProxyServer (proxyServer ("localhost" , squidProxy .getMappedPort (SQUID_HTTP_PORT ))
105
+ .setProxyType (ProxyType .HTTP )
106
+ .build ())
107
+ .setConnectTimeout (Duration .ofMillis (10000 ))
108
+ .setRequestTimeout (Duration .ofMillis (30000 ))
109
+ .build ();
107
110
try (AsyncHttpClient client = asyncHttpClient (config )) {
108
111
Response response = client .executeRequest (get (TARGET_HTTP_URL )).get (30 , TimeUnit .SECONDS );
109
-
110
112
assertEquals (200 , response .getStatusCode ());
111
113
assertTrue (response .getResponseBody ().contains ("httpbin" ));
112
-
113
114
LOGGER .info ("HTTP proxy to HTTP target test passed" );
114
115
}
115
116
}
116
-
117
+
117
118
@ RepeatedIfExceptionsTest (repeats = 3 )
118
119
public void testHttpsProxyToHttpTarget () throws Exception {
120
+ assumeTrue (dockerAvailable , "Docker is not available - skipping test" );
119
121
LOGGER .info ("Testing HTTPS proxy to HTTP target" );
120
-
121
122
AsyncHttpClientConfig config = config ()
122
- .setProxyServer (proxyServer ("localhost" , squidProxy .getMappedPort (SQUID_HTTPS_PORT ))
123
- .setProxyType (ProxyType .HTTPS )
124
- .build ())
125
- .setUseInsecureTrustManager (true ) // Accept self-signed proxy cert
126
- .setConnectTimeout (Duration .ofMillis (10000 ))
127
- .setRequestTimeout (Duration .ofMillis (30000 ))
128
- .build ();
129
-
123
+ .setProxyServer (proxyServer ("localhost" , squidProxy .getMappedPort (SQUID_HTTPS_PORT ))
124
+ .setProxyType (ProxyType .HTTPS )
125
+ .build ())
126
+ .setUseInsecureTrustManager (true )
127
+ .setConnectTimeout (Duration .ofMillis (10000 ))
128
+ .setRequestTimeout (Duration .ofMillis (30000 ))
129
+ .build ();
130
130
try (AsyncHttpClient client = asyncHttpClient (config )) {
131
131
Response response = client .executeRequest (get (TARGET_HTTP_URL )).get (30 , TimeUnit .SECONDS );
132
-
133
132
assertEquals (200 , response .getStatusCode ());
134
133
assertTrue (response .getResponseBody ().contains ("httpbin" ));
135
-
136
134
LOGGER .info ("HTTPS proxy to HTTP target test passed" );
137
135
}
138
136
}
139
-
137
+
140
138
@ RepeatedIfExceptionsTest (repeats = 3 )
141
139
public void testHttpProxyToHttpsTarget () throws Exception {
140
+ assumeTrue (dockerAvailable , "Docker is not available - skipping test" );
142
141
LOGGER .info ("Testing HTTP proxy to HTTPS target" );
143
-
144
142
AsyncHttpClientConfig config = config ()
145
- .setProxyServer (proxyServer ("localhost" , squidProxy .getMappedPort (SQUID_HTTP_PORT ))
146
- .setProxyType (ProxyType .HTTP )
147
- .build ())
148
- .setUseInsecureTrustManager (true ) // Accept any HTTPS target cert
149
- .setConnectTimeout (Duration .ofMillis (10000 ))
150
- .setRequestTimeout (Duration .ofMillis (30000 ))
151
- .build ();
152
-
143
+ .setProxyServer (proxyServer ("localhost" , squidProxy .getMappedPort (SQUID_HTTP_PORT ))
144
+ .setProxyType (ProxyType .HTTP )
145
+ .build ())
146
+ .setUseInsecureTrustManager (true )
147
+ .setConnectTimeout (Duration .ofMillis (10000 ))
148
+ .setRequestTimeout (Duration .ofMillis (30000 ))
149
+ .build ();
153
150
try (AsyncHttpClient client = asyncHttpClient (config )) {
154
151
Response response = client .executeRequest (get (TARGET_HTTPS_URL )).get (30 , TimeUnit .SECONDS );
155
-
156
152
assertEquals (200 , response .getStatusCode ());
157
- assertTrue (response .getResponseBody ().contains ("Example Domain" ) ||
158
- response .getResponseBody ().contains ("example" ));
159
-
153
+ assertTrue (response .getResponseBody ().contains ("Example Domain" ) ||
154
+ response .getResponseBody ().contains ("example" ));
160
155
LOGGER .info ("HTTP proxy to HTTPS target test passed" );
161
156
}
162
157
}
163
-
158
+
164
159
@ RepeatedIfExceptionsTest (repeats = 3 )
165
160
public void testHttpsProxyToHttpsTarget () throws Exception {
161
+ assumeTrue (dockerAvailable , "Docker is not available - skipping test" );
166
162
LOGGER .info ("Testing HTTPS proxy to HTTPS target - validates issue #1907 fix" );
167
-
168
163
AsyncHttpClientConfig config = config ()
169
- .setProxyServer (proxyServer ("localhost" , squidProxy .getMappedPort (SQUID_HTTPS_PORT ))
170
- .setProxyType (ProxyType .HTTPS )
171
- .build ())
172
- .setUseInsecureTrustManager (true ) // Accept self-signed proxy cert and any HTTPS target cert
173
- .setConnectTimeout (Duration .ofMillis (10000 ))
174
- .setRequestTimeout (Duration .ofMillis (30000 ))
175
- .build ();
176
-
164
+ .setProxyServer (proxyServer ("localhost" , squidProxy .getMappedPort (SQUID_HTTPS_PORT ))
165
+ .setProxyType (ProxyType .HTTPS )
166
+ .build ())
167
+ .setUseInsecureTrustManager (true )
168
+ .setConnectTimeout (Duration .ofMillis (10000 ))
169
+ .setRequestTimeout (Duration .ofMillis (30000 ))
170
+ .build ();
177
171
try (AsyncHttpClient client = asyncHttpClient (config )) {
178
172
Response response = client .executeRequest (get (TARGET_HTTPS_URL )).get (30 , TimeUnit .SECONDS );
179
-
180
173
assertEquals (200 , response .getStatusCode ());
181
- assertTrue (response .getResponseBody ().contains ("Example Domain" ) ||
182
- response .getResponseBody ().contains ("example" ));
183
-
174
+ assertTrue (response .getResponseBody ().contains ("Example Domain" ) ||
175
+ response .getResponseBody ().contains ("example" ));
184
176
LOGGER .info ("HTTPS proxy to HTTPS target test passed - core issue #1907 RESOLVED!" );
185
177
}
186
178
}
187
-
179
+
188
180
@ Test
189
181
public void testDockerInfrastructureReady () {
182
+ assumeTrue (dockerAvailable , "Docker is not available - skipping test" );
190
183
LOGGER .info ("Docker infrastructure test - validating container is ready" );
191
184
LOGGER .info ("Squid HTTP proxy available at: localhost:{}" , squidProxy .getMappedPort (SQUID_HTTP_PORT ));
192
185
LOGGER .info ("Squid HTTPS proxy available at: localhost:{}" , squidProxy .getMappedPort (SQUID_HTTPS_PORT ));
193
-
194
186
assertTrue (squidProxy .isRunning (), "Squid container should be running" );
195
187
assertTrue (squidProxy .getMappedPort (SQUID_HTTP_PORT ) > 0 , "HTTP port should be mapped" );
196
188
assertTrue (squidProxy .getMappedPort (SQUID_HTTPS_PORT ) > 0 , "HTTPS port should be mapped" );
197
-
198
189
LOGGER .info ("Docker infrastructure is ready and accessible" );
199
190
}
200
191
}
0 commit comments