public void setStatusLine(final ProtocolVersion ver, final int code, final String reason) { // arguments checked in BasicStatusLine constructor this.statusline = new BasicStatusLine(ver, code, reason); }
/** * Instantiates a new status line. * Called from {@link #parseStatusLine}. * * @param ver the protocol version * @param status the status code * @param reason the reason phrase * * @return a new status line with the given data */ protected StatusLine createStatusLine(final ProtocolVersion ver, final int status, final String reason) { return new BasicStatusLine(ver, status, reason); }
/** * Creates a response from elements of a status line. * The response will not have a reason phrase catalog and * use the system default locale. * * @param ver the protocol version of the response * @param code the status code of the response * @param reason the reason phrase to the status code, or * <code>null</code> */ public BasicHttpResponse(final ProtocolVersion ver, final int code, final String reason) { this(new BasicStatusLine(ver, code, reason), null, null); }
public void setReasonPhrase(String reason) { if ((reason != null) && ((reason.indexOf('\n') >= 0) || (reason.indexOf('\r') >= 0)) ) { throw new IllegalArgumentException("Line break in reason phrase."); } this.statusline = new BasicStatusLine(this.statusline.getProtocolVersion(), this.statusline.getStatusCode(), reason); }
public void setStatusLine(final ProtocolVersion ver, final int code) { // arguments checked in BasicStatusLine constructor this.statusline = new BasicStatusLine(ver, code, getReason(code)); }
public void setLocale(Locale loc) { if (loc == null) { throw new IllegalArgumentException("Locale may not be null."); } this.locale = loc; final int code = this.statusline.getStatusCode(); this.statusline = new BasicStatusLine (this.statusline.getProtocolVersion(), code, getReason(code)); }
public void setStatusCode(int code) { // argument checked in BasicStatusLine constructor ProtocolVersion ver = this.statusline.getProtocolVersion(); this.statusline = new BasicStatusLine(ver, code, getReason(code)); }
private static BasicHttpResponse createResponse(int statusCode) { return new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, statusCode, null)); }
public HttpResponse newHttpResponse(final ProtocolVersion ver, final int status, HttpContext context) { if (ver == null) { throw new IllegalArgumentException("HTTP version may not be null"); } final Locale loc = determineLocale(context); final String reason = reasonCatalog.getReason(status, loc); StatusLine statusline = new BasicStatusLine(ver, status, reason); return new BasicHttpResponse(statusline, reasonCatalog, loc); }
public static HttpResponse mockHttpResponse(int statusCode, String message, String body) { HttpResponse response = Mockito.mock(HttpResponse.class); Mockito.when(response.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, statusCode, message)); Mockito.when(response.getEntity()).thenReturn(new StringEntity(body, StandardCharsets.UTF_8)); return response; }
@Before public void setUp() throws Exception { systemEnvironment = mock(SystemEnvironment.class); URLService urlService = mock(URLService.class); GoAgentServerHttpClient httpClient = mock(GoAgentServerHttpClient.class); jvmExitter = mock(AgentUpgradeService.JvmExitter.class); agentUpgradeService = spy(new AgentUpgradeService(urlService, httpClient, systemEnvironment, jvmExitter)); HttpGet httpMethod = mock(HttpGet.class); doReturn(httpMethod).when(agentUpgradeService).getAgentLatestStatusGetMethod(); closeableHttpResponse = mock(CloseableHttpResponse.class); when(closeableHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "OK")); when(httpClient.execute(httpMethod)).thenReturn(closeableHttpResponse); }
@Test public void connect() throws IOException { HttpResponse response = mock(HttpResponse.class); when(httpClient.execute(any(HttpUriRequest.class))).thenReturn(response); when(response.getStatusLine()).thenReturn(new BasicStatusLine(HTTP_1_1, 200, "OK")); client.connect(); }
@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); }
@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(); }
@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); }
@Test public void shouldGetTokenFromServer() 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("token-from-server")); when(httpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("https", 1, 2), SC_OK, null)); final String token = tokenRequester.getToken(); verify(httpClient).execute(argumentCaptor.capture()); final HttpRequestBase requestBase = argumentCaptor.getValue(); final List<NameValuePair> nameValuePairs = URLEncodedUtils.parse(requestBase.getURI(), StandardCharsets.UTF_8.name()); assertThat(token, is("token-from-server")); assertThat(findParam(nameValuePairs, "uuid").getValue(), is("agent-uuid")); }
@Override protected String sendAndReturn(final HttpUriRequest request) throws IOException { final HttpResponseFactory factory = new DefaultHttpResponseFactory(); final HttpResponse response = factory.newHttpResponse( new BasicStatusLine(HttpVersion.HTTP_1_1, this.status, null), null); final StringBuilder sb = new StringBuilder(); sb.append(String.format("%s = %s;", "METHOD", request.getMethod())); sb.append(String.format("%s = %s;", "URI", request.getURI())); final HttpEntity entity = ((HttpEntityEnclosingRequestBase) request).getEntity(); sb.append(String.format("%s = %s;", "BODY", EntityUtils.toString(entity))); response.setEntity(new StringEntity(sb.toString())); return parseResponse(response); }
@Test public void shouldSetTheAcceptHeaderWhilePostingProperties() throws Exception { HttpPost post = mock(HttpPost.class); when(httpClientFactory.createPost("url")).thenReturn(post); CloseableHttpResponse response = mock(CloseableHttpResponse.class); when(response.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "OK")); when(httpClient.execute(post)).thenReturn(response); ArgumentCaptor<UrlEncodedFormEntity> entityCaptor = ArgumentCaptor.forClass(UrlEncodedFormEntity.class); service.postProperty("url", "value"); verify(post).setHeader("Confirm","true"); verify(post).setEntity(entityCaptor.capture()); UrlEncodedFormEntity expected = new UrlEncodedFormEntity(Arrays.asList(new BasicNameValuePair("value", "value"))); UrlEncodedFormEntity actual = entityCaptor.getValue(); assertEquals(IOUtils.toString(expected.getContent()), IOUtils.toString(actual.getContent())); assertEquals(expected.getContentLength(), expected.getContentLength()); assertEquals(expected.getContentType(), expected.getContentType()); assertEquals(expected.getContentEncoding(), expected.getContentEncoding()); assertEquals(expected.isChunked(), expected.isChunked()); }
@Override public HttpResponse execute(HttpUriRequest request, HttpContext context) { requestExecuted = request; StatusLine statusLine = new BasicStatusLine( new ProtocolVersion("HTTP", 1, 1), mStatusCode, ""); HttpResponse response = new BasicHttpResponse(statusLine); response.setEntity(mResponseEntity); return response; }
@Override public HttpResponse execute(HttpUriRequest request, HttpContext context) { requestExecuted = request; StatusLine statusLine = new BasicStatusLine( new ProtocolVersion("HTTP", 1, 1), mStatusCode, ""); HttpResponse response = new BasicHttpResponse(statusLine); response.setEntity(mResponseEntity); return response; }