Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "Invalid cookie header" warnings #343

Merged
merged 2 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.AuthenticationException;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.Registry;
Expand Down Expand Up @@ -109,7 +110,8 @@ CloseableHttpClient getHttpClient(PrintStream logger, URI stashServer, boolean i
RequestConfig.Builder requestBuilder = RequestConfig.custom()
.setSocketTimeout(timeoutInMilliseconds)
.setConnectTimeout(timeoutInMilliseconds)
.setConnectionRequestTimeout(timeoutInMilliseconds);
.setConnectionRequestTimeout(timeoutInMilliseconds)
.setCookieSpec(CookieSpecs.STANDARD);

HttpClientBuilder clientBuilder = HttpClients.custom();
clientBuilder.setDefaultRequestConfig(requestBuilder.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.AuthenticationException;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.Registry;
Expand Down Expand Up @@ -530,7 +531,8 @@ protected CloseableHttpClient getHttpClient(PrintStream logger, Run<?, ?> run, S
RequestConfig.Builder requestBuilder = RequestConfig.custom()
.setSocketTimeout(timeoutInMilliseconds)
.setConnectTimeout(timeoutInMilliseconds)
.setConnectionRequestTimeout(timeoutInMilliseconds);
.setConnectionRequestTimeout(timeoutInMilliseconds)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to usage search, the function isn't used anymore and deprecated. Thus, instead of adding tests to these changes, I can drop them. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems low risk so I think we can leave it. If there is an issue we can revert.

.setCookieSpec(CookieSpecs.STANDARD);

HttpClientBuilder clientBuilder = HttpClients.custom();
clientBuilder.setDefaultRequestConfig(requestBuilder.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import net.sf.json.JSONObject;
import org.acegisecurity.Authentication;
import org.apache.http.StatusLine;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
Expand All @@ -24,6 +26,7 @@
import org.apache.http.impl.client.HttpClientBuilder;
import org.jenkinsci.plugins.tokenmacro.TokenMacro;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

Expand All @@ -33,6 +36,8 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.mockito.ArgumentCaptor;
import org.mockito.MockedStatic;

import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -43,6 +48,7 @@
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class DefaultApacheHttpNotifierTest {
Expand All @@ -52,17 +58,18 @@ public class DefaultApacheHttpNotifierTest {
private static MockedStatic<Jenkins> mockedJenkins;
private static MockedStatic<CredentialsProvider> mockedCredentialsProvider;
private static MockedStatic<Secret> mockedSecret;
private static MockedStatic<HttpClientBuilder> mockedHttpClientBuilder;
private static MockedStatic<HttpClientBuilder> mockedStaticHttpClientBuilder;
private static MockedStatic<TokenMacro> mockedTokenMacro;
private final HttpNotifier httpNotifier = new DefaultApacheHttpNotifier();

private static BuildListener buildListener;
private HttpClientBuilder httpClientBuilder;

@BeforeClass
public static void setUp() throws Exception {
mockedSecret = mockStatic(Secret.class);
mockedJenkins = mockStatic(Jenkins.class);
mockedHttpClientBuilder = mockStatic(HttpClientBuilder.class);
mockedStaticHttpClientBuilder = mockStatic(HttpClientBuilder.class);
mockedTokenMacro = mockStatic(TokenMacro.class);
mockedCredentialsProvider = mockStatic(com.cloudbees.plugins.credentials.CredentialsProvider.class);

Expand All @@ -80,7 +87,7 @@ public static void setUp() throws Exception {
EnvVars environment = mock(EnvVars.class);
PrintStream logger = System.out;
Secret secret = mock(Secret.class);
HttpClientBuilder httpClientBuilder = mock(HttpClientBuilder.class);

client = mock(CloseableHttpClient.class);
CloseableHttpResponse resp = mock(CloseableHttpResponse.class);
StatusLine statusLine = mock(StatusLine.class);
Expand All @@ -104,8 +111,6 @@ public static void setUp() throws Exception {
when(Secret.fromString("tiger")).thenReturn(secret);
when(Secret.toString(secret)).thenReturn("tiger");
when(secret.getPlainText()).thenReturn("tiger");
when(HttpClientBuilder.create()).thenReturn(httpClientBuilder);
when(httpClientBuilder.build()).thenReturn(client);
when(client.execute(any(HttpUriRequest.class))).thenReturn(resp);
when(resp.getStatusLine()).thenReturn(statusLine);
when(statusLine.getStatusCode()).thenReturn(204);
Expand All @@ -126,10 +131,17 @@ public static void close() {
mockedJenkins.close();
mockedCredentialsProvider.close();
mockedSecret.close();
mockedHttpClientBuilder.close();
mockedStaticHttpClientBuilder.close();
mockedTokenMacro.close();
}

@Before
public void before() {
httpClientBuilder = mock(HttpClientBuilder.class);
when(HttpClientBuilder.create()).thenReturn(httpClientBuilder);
when(httpClientBuilder.build()).thenReturn(client);
}

private NotificationResult notifyStash(int statusCode) throws Exception {
PrintStream logger = mock(PrintStream.class);
URI uri = BuildStatusUriFactory.create("http://localhost", "df02f57eea1cda72fa2412102f061dd7f6188e98");
Expand All @@ -154,4 +166,18 @@ public void notifyStash_fail() throws Exception {
NotificationResult notificationResult = notifyStash(400);
assertThat(notificationResult.indicatesSuccess, is(false));
}

@Test
public void notifyStashUsesRequestParameters() throws Exception {
offa marked this conversation as resolved.
Show resolved Hide resolved
notifyStash(204);

final ArgumentCaptor<RequestConfig> captor = ArgumentCaptor.forClass(RequestConfig.class);
verify(httpClientBuilder).setDefaultRequestConfig(captor.capture());

final RequestConfig config = captor.getValue();
assertThat(config.getSocketTimeout(), is(60_000));
assertThat(config.getConnectTimeout(), is(60_000));
assertThat(config.getConnectionRequestTimeout(), is(60_000));
assertThat(config.getCookieSpec(), is(CookieSpecs.STANDARD));
}
}