Search in sources :

Example 66 with BasicStatusLine

use of org.apache.http.message.BasicStatusLine in project gocd by gocd.

the class HttpServiceTest method shouldDownloadArtifact.

@Test
public void shouldDownloadArtifact() throws IOException {
    String url = "http://blah";
    FetchHandler fetchHandler = mock(FetchHandler.class);
    HttpGet mockGetMethod = mock(HttpGet.class);
    CloseableHttpResponse response = mock(CloseableHttpResponse.class);
    when(response.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "OK"));
    when(httpClient.execute(mockGetMethod)).thenReturn(response);
    when(httpClientFactory.createGet(url)).thenReturn(mockGetMethod);
    service.download(url, fetchHandler);
    verify(httpClient).execute(mockGetMethod);
    verify(fetchHandler).handle(null);
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) FetchHandler(com.thoughtworks.go.domain.FetchHandler) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Example 67 with BasicStatusLine

use of org.apache.http.message.BasicStatusLine in project gocd by gocd.

the class HttpServiceTest method shouldPostArtifactsAlongWithMD5.

@Test
public void shouldPostArtifactsAlongWithMD5() throws IOException {
    File uploadingFile = mock(File.class);
    java.util.Properties checksums = new java.util.Properties();
    String uploadUrl = "url";
    HttpPost mockPostMethod = mock(HttpPost.class);
    CloseableHttpResponse response = mock(CloseableHttpResponse.class);
    when(response.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "OK"));
    when(httpClient.execute(mockPostMethod)).thenReturn(response);
    when(uploadingFile.exists()).thenReturn(true);
    when(httpClientFactory.createPost(uploadUrl)).thenReturn(mockPostMethod);
    service.upload(uploadUrl, 100L, uploadingFile, checksums);
    verify(mockPostMethod).setHeader(GO_ARTIFACT_PAYLOAD_SIZE, "100");
    verify(mockPostMethod).setHeader("Confirm", "true");
    verify(httpClientFactory).createMultipartRequestEntity(uploadingFile, checksums);
    verify(httpClient).execute(mockPostMethod);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) File(java.io.File) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Example 68 with BasicStatusLine

use of org.apache.http.message.BasicStatusLine in project gocd by gocd.

the class RemoteRegistrationRequesterTest method shouldPassAllParametersToPostForRegistrationOfElasticAgent.

@Test
public void shouldPassAllParametersToPostForRegistrationOfElasticAgent() throws IOException, ClassNotFoundException {
    String url = "http://cruise.com/go";
    GoAgentServerHttpClient httpClient = mock(GoAgentServerHttpClient.class);
    final CloseableHttpResponse response = mock(CloseableHttpResponse.class);
    final ProtocolVersion protocolVersion = new ProtocolVersion("https", 1, 2);
    when(response.getStatusLine()).thenReturn(new BasicStatusLine(protocolVersion, HttpStatus.OK.value(), null));
    when(response.getEntity()).thenReturn(new StringEntity(RegistrationJSONizer.toJson(createRegistration())));
    when(httpClient.execute(argThat(isA(HttpUriRequest.class)))).thenReturn(response);
    final DefaultAgentRegistry defaultAgentRegistry = new DefaultAgentRegistry();
    Properties properties = new Properties();
    properties.put(AgentAutoRegistrationPropertiesImpl.AGENT_AUTO_REGISTER_KEY, "t0ps3cret");
    properties.put(AgentAutoRegistrationPropertiesImpl.AGENT_AUTO_REGISTER_RESOURCES, "linux, java");
    properties.put(AgentAutoRegistrationPropertiesImpl.AGENT_AUTO_REGISTER_ENVIRONMENTS, "uat, staging");
    properties.put(AgentAutoRegistrationPropertiesImpl.AGENT_AUTO_REGISTER_HOSTNAME, "agent01.example.com");
    properties.put(AgentAutoRegistrationPropertiesImpl.AGENT_AUTO_REGISTER_ELASTIC_AGENT_ID, "42");
    properties.put(AgentAutoRegistrationPropertiesImpl.AGENT_AUTO_REGISTER_ELASTIC_PLUGIN_ID, "tw.go.elastic-agent.docker");
    remoteRegistryRequester(url, httpClient, defaultAgentRegistry, 200).requestRegistration("cruise.com", new AgentAutoRegistrationPropertiesImpl(null, properties));
    verify(httpClient).execute(argThat(hasAllParams(defaultAgentRegistry.uuid(), "42", "tw.go.elastic-agent.docker")));
}
Also used : StringEntity(org.apache.http.entity.StringEntity) AgentAutoRegistrationPropertiesImpl(com.thoughtworks.go.agent.AgentAutoRegistrationPropertiesImpl) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) GoAgentServerHttpClient(com.thoughtworks.go.agent.common.ssl.GoAgentServerHttpClient) ProtocolVersion(org.apache.http.ProtocolVersion) DefaultAgentRegistry(com.thoughtworks.go.config.DefaultAgentRegistry) Properties(java.util.Properties) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Example 69 with BasicStatusLine

use of org.apache.http.message.BasicStatusLine in project gocd by gocd.

the class SslInfrastructureServiceTest method shouldDeleteTokenFromDiskWhenServerRejectsTheRegistrationRequestWithForbiddenErrorCode.

@Test
public void shouldDeleteTokenFromDiskWhenServerRejectsTheRegistrationRequestWithForbiddenErrorCode() throws Exception {
    final CloseableHttpResponse httpResponseForbidden = mock(CloseableHttpResponse.class);
    final ProtocolVersion protocolVersion = new ProtocolVersion("https", 1, 2);
    when(agentRegistry.uuid()).thenReturn("some-uuid");
    when(agentRegistry.tokenPresent()).thenReturn(true);
    when(httpResponse.getStatusLine()).thenReturn(new BasicStatusLine(protocolVersion, HttpStatus.OK.value(), null));
    when(httpResponseForbidden.getStatusLine()).thenReturn(new BasicStatusLine(protocolVersion, HttpStatus.FORBIDDEN.value(), null));
    when(httpResponse.getEntity()).thenReturn(new StringEntity(RegistrationJSONizer.toJson(createRegistration())));
    when(httpResponseForbidden.getEntity()).thenReturn(new StringEntity("Not a valid token."));
    when(httpClient.execute(any(HttpUriRequest.class))).thenReturn(httpResponseForbidden).thenReturn(httpResponse);
    sslInfrastructureService.createSslInfrastructure();
    sslInfrastructureService.registerIfNecessary(new AgentAutoRegistrationPropertiesImpl(new File("foo", "bar")));
    assertThat(GoAgentServerClientBuilder.AGENT_CERTIFICATE_FILE, exists());
    verify(agentRegistry, times(1)).deleteToken();
    verify(httpClient, times(2)).execute(any(HttpUriRequest.class));
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) StringEntity(org.apache.http.entity.StringEntity) AgentAutoRegistrationPropertiesImpl(com.thoughtworks.go.agent.AgentAutoRegistrationPropertiesImpl) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) ProtocolVersion(org.apache.http.ProtocolVersion) File(java.io.File) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Example 70 with BasicStatusLine

use of org.apache.http.message.BasicStatusLine in project gocd by gocd.

the class TokenRequesterTest method shouldErrorOutIfServerRejectTheRequest.

@Test
public void shouldErrorOutIfServerRejectTheRequest() throws Exception {
    final ArgumentCaptor<HttpRequestBase> argumentCaptor = ArgumentCaptor.forClass(HttpRequestBase.class);
    final CloseableHttpResponse httpResponse = mock(CloseableHttpResponse.class);
    when(agentRegistry.uuid()).thenReturn("agent-uuid");
    when(httpClient.execute(any(HttpRequestBase.class))).thenReturn(httpResponse);
    when(httpResponse.getEntity()).thenReturn(new StringEntity("A token has already been issued for this agent."));
    when(httpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("https", 1, 2), SC_UNPROCESSABLE_ENTITY, null));
    thrown.expect(RuntimeException.class);
    thrown.expectMessage("A token has already been issued for this agent.");
    tokenRequester.getToken();
}
Also used : StringEntity(org.apache.http.entity.StringEntity) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) ProtocolVersion(org.apache.http.ProtocolVersion) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Aggregations

BasicStatusLine (org.apache.http.message.BasicStatusLine)70 ProtocolVersion (org.apache.http.ProtocolVersion)54 StatusLine (org.apache.http.StatusLine)44 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)37 Test (org.junit.Test)27 Header (org.apache.http.Header)17 StringEntity (org.apache.http.entity.StringEntity)17 BasicHeader (org.apache.http.message.BasicHeader)17 URL (java.net.URL)16 HttpResponse (org.apache.http.HttpResponse)16 HttpURLConnection (java.net.HttpURLConnection)15 List (java.util.List)15 HashMap (java.util.HashMap)12 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)12 ByteArrayInputStream (java.io.ByteArrayInputStream)10 IOException (java.io.IOException)9 HttpEntity (org.apache.http.HttpEntity)9 HttpHost (org.apache.http.HttpHost)6 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)6 AgentAutoRegistrationPropertiesImpl (com.thoughtworks.go.agent.AgentAutoRegistrationPropertiesImpl)5