Skip to content

Commit

Permalink
Add CookieSpecs test
Browse files Browse the repository at this point in the history
  • Loading branch information
offa authored and sghill committed Aug 22, 2023
1 parent e6bfd3e commit 2023583
Showing 1 changed file with 32 additions and 6 deletions.
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 {
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));
}
}

0 comments on commit 2023583

Please sign in to comment.