Skip to content

Commit ec4b511

Browse files
ricksbrowndbmeneses
authored andcommitted
The HTTP client will honor Set-Cookie headers on response
1 parent f1d8eb6 commit ec4b511

4 files changed

Lines changed: 61 additions & 1 deletion

File tree

api/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
<groupId>com.squareup.okhttp3</groupId>
1616
<artifactId>okhttp</artifactId>
1717
</dependency>
18+
<dependency>
19+
<groupId>com.squareup.okhttp3</groupId>
20+
<artifactId>okhttp-urlconnection</artifactId>
21+
</dependency>
1822
<dependency>
1923
<groupId>com.eclipsesource.minimal-json</groupId>
2024
<artifactId>minimal-json</artifactId>

api/src/main/java/org/sonarsource/scanner/api/internal/OkHttpClientFactory.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import java.io.FileInputStream;
2323
import java.io.IOException;
24+
import java.net.CookieManager;
25+
import java.net.CookiePolicy;
2426
import java.net.HttpURLConnection;
2527
import java.security.GeneralSecurityException;
2628
import java.security.KeyStore;
@@ -41,6 +43,7 @@
4143
import okhttp3.ConnectionSpec;
4244
import okhttp3.Credentials;
4345
import okhttp3.OkHttpClient;
46+
import okhttp3.JavaNetCookieJar;
4447
import org.sonarsource.scanner.api.internal.cache.Logger;
4548

4649
import static java.util.Arrays.asList;
@@ -52,12 +55,20 @@ public class OkHttpClientFactory {
5255
static final int DEFAULT_READ_TIMEOUT_SEC = (int) Duration.ofMinutes(5).getSeconds();
5356
static final String NONE = "NONE";
5457
static final String P11KEYSTORE = "PKCS11";
58+
static final CookieManager COOKIE_MANAGER;
5559
private static final String PROXY_AUTHORIZATION = "Proxy-Authorization";
60+
private static final JavaNetCookieJar COOKIE_JAR; // use the same cookie jar for all instances
5661

5762
private OkHttpClientFactory() {
5863
// only statics
5964
}
6065

66+
static {
67+
COOKIE_MANAGER = new CookieManager();
68+
COOKIE_MANAGER.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
69+
COOKIE_JAR = new JavaNetCookieJar(COOKIE_MANAGER);
70+
}
71+
6172
static OkHttpClient create(Logger logger) {
6273
OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();
6374

@@ -68,6 +79,7 @@ static OkHttpClient create(Logger logger) {
6879

6980
okHttpClientBuilder.connectTimeout(CONNECT_TIMEOUT_MILLISECONDS, TimeUnit.MILLISECONDS);
7081
okHttpClientBuilder.readTimeout(readTimeoutSec, TimeUnit.SECONDS);
82+
okHttpClientBuilder.cookieJar(COOKIE_JAR);
7183

7284
ConnectionSpec tls = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
7385
.allEnabledTlsVersions()

api/src/test/java/org/sonarsource/scanner/api/internal/OkHttpClientFactoryTest.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public class OkHttpClientFactoryTest {
6868
private static final String KEYSTORE_FILE = "/server.jks";
6969
private static final Logger logger = mock(Logger.class);
7070
private static final String SONAR_WS_TIMEOUT = "sonar.ws.timeout";
71+
private static final String COOKIE = "BIGipServerpool_sonarqube.example.com_8443=123456789.12345.0000";
7172

7273
@Rule
7374
public ExpectedException expectedException = ExpectedException.none();
@@ -175,6 +176,33 @@ public void test_with_custom_https_server(String clientKeyStore) throws Exceptio
175176
}
176177
}
177178

179+
@Theory
180+
public void test_with_cookie(String clientKeyStore) throws Exception {
181+
try (MockWebServer server = buildTLSServer()) {
182+
String url = format("https://localhost:%d/", server.getPort());
183+
184+
// Add the truststore
185+
Path clientTruststore = Paths.get(getClass().getResource(clientKeyStore).toURI()).toAbsolutePath();
186+
System.setProperty("javax.net.ssl.trustStore", clientTruststore.toString());
187+
System.setProperty("javax.net.ssl.trustStorePassword", KEYSTORE_PASSWORD);
188+
189+
OkHttpClientFactory.COOKIE_MANAGER.getCookieStore().removeAll(); // Clear any existing cookies
190+
191+
Response response = call(url);
192+
assertThat(response.header("Set-Cookie")).isEqualTo(COOKIE); // The server should have asked us to set a cookie
193+
assertThat(response.body().string()).doesNotContain(COOKIE);
194+
195+
response = call(url);
196+
assertThat(response.body().string()).contains(COOKIE);
197+
198+
} finally {
199+
// Ensure to not keeping this property for other tests
200+
System.clearProperty("javax.net.ssl.trustStore");
201+
System.clearProperty("javax.net.ssl.trustStorePassword");
202+
}
203+
}
204+
205+
178206
private static Response call(String url) throws IOException {
179207
return OkHttpClientFactory.create(logger).newCall(
180208
new Request.Builder()
@@ -193,7 +221,18 @@ private MockWebServer buildTLSServer() throws Exception {
193221
server.setDispatcher(new Dispatcher() {
194222
@Override
195223
public MockResponse dispatch(RecordedRequest request) {
196-
return new MockResponse().setResponseCode(200).setBody("OK");
224+
String responseBody = "OK";
225+
MockResponse response = new MockResponse().setResponseCode(200);
226+
String cookie = request.getHeader("Cookie");
227+
if (cookie == null || cookie.isEmpty()) {
228+
// Only set the cookie if it is not already set
229+
response.addHeader("Set-Cookie", COOKIE);
230+
} else {
231+
// dump the cookie into the response body to aid in test inspection
232+
responseBody += "\nCookie: " + cookie;
233+
}
234+
response.setBody(responseBody);
235+
return response;
197236
}
198237
});
199238

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@
105105
<artifactId>okhttp</artifactId>
106106
<version>${okhttp.version}</version>
107107
</dependency>
108+
<dependency>
109+
<groupId>com.squareup.okhttp3</groupId>
110+
<artifactId>okhttp-urlconnection</artifactId>
111+
<version>${okhttp.version}</version>
112+
</dependency>
108113
<dependency>
109114
<groupId>com.eclipsesource.minimal-json</groupId>
110115
<artifactId>minimal-json</artifactId>

0 commit comments

Comments
 (0)