private static HttpResponse transformResponse(Response response) { int code = response.code(); String message = response.message(); BasicHttpResponse httpResponse = new BasicHttpResponse(HTTP_1_1, code, message); ResponseBody body = response.body(); InputStreamEntity entity = new InputStreamEntity(body.byteStream(), body.contentLength()); httpResponse.setEntity(entity); Headers headers = response.headers(); for (int i = 0, size = headers.size(); i < size; i++) { String name = headers.name(i); String value = headers.value(i); httpResponse.addHeader(name, value); if ("Content-Type".equalsIgnoreCase(name)) { entity.setContentType(value); } else if ("Content-Encoding".equalsIgnoreCase(name)) { entity.setContentEncoding(value); } } return httpResponse; }
private BasicHttpResponse createOkPropfindResponse() throws UnsupportedEncodingException { BasicHttpResponse okPropfindResponse = createOkResponse(); HttpEntity propfindResponseEntity = new StringEntity("" + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<D:multistatus xmlns:D=\"DAV:\" xmlns:e=\"urn:schemas:httpmail:\">\n" + " <D:response><e:inbox>http://example.org/Exchange/user/Inbox</e:inbox></D:response>\n" + "</D:multistatus>"); okPropfindResponse.setEntity(propfindResponseEntity); return okPropfindResponse; }
connection.getResponseCode(), connection.getResponseMessage()); BasicHttpResponse response = new BasicHttpResponse(responseStatus); response.setEntity(entityFromConnection(connection)); for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) { if (header.getKey() != null) {
BasicHttpResponse response = new BasicHttpResponse(responseStatus); if (hasResponseBody(request.getMethod(), responseStatus.getStatusCode())) { response.setEntity(entityFromConnection(connection));
BasicHttpResponse response = new BasicHttpResponse(responseStatus); if (hasResponseBody(request.getMethod(), responseStatus.getStatusCode())) { response.setEntity(entityFromConnection(connection));
private BasicHttpResponse createOkSearchResponse() throws UnsupportedEncodingException { BasicHttpResponse okSearchResponse = createOkResponse(); HttpEntity searchResponseEntity = new StringEntity("" + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + " <D:multistatus xmlns:D=\"DAV:\"\n" + " xmlns:R=\"http://example.org/propschema\">\n" + " <D:response>" + " <D:propstat>\n" + " <uid>Inbox</uid>" + " <href>http://example.org/Exchange/user/Inbox</href>\n" + " </D:propstat></D:response>\n" + " <D:response>" + " <D:propstat>\n" + " <uid>Drafts</uid>" + " <href>http://example.org/Exchange/user/Drafts</href>\n" + " </D:propstat></D:response>\n" + " <D:response>" + " <D:propstat>\n" + " <uid>Folder2</uid>" + " <href>http://example.org/Exchange/user/Folder2</href>\n" + " </D:propstat></D:response>\n" + " </D:multistatus>"); okSearchResponse.setEntity(searchResponseEntity); return okSearchResponse; }
private BasicHttpResponse createOkResponseWithForm() { BasicHttpResponse okayResponseWithForm = createOkResponse(); BasicHttpEntity okayResponseWithFormEntity = new BasicHttpEntity(); String form = "<form action=\"owaauth.dll\"></form>"; okayResponseWithFormEntity.setContent(new ByteArrayInputStream(form.getBytes())); okayResponseWithForm.setEntity(okayResponseWithFormEntity); return okayResponseWithForm; }
response.setEntity(entityFromOkHttpResponse(okHttpResponse));
BasicHttpResponse fakeResponse = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), 200, "OK"); fakeResponse.setEntity(new StringEntity("foobar")); mockHttpStack.setResponseToReturn(fakeResponse); BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
BasicHttpResponse fakeResponse = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), 200, "OK"); fakeResponse.setEntity(new StringEntity("foobar")); mockHttpStack.setResponseToReturn(fakeResponse); BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
/** * {@inheritDoc} */ @Override public void setEntity(HttpEntity entity) { if (response != null) { response.setEntity(entity); } else { super.setEntity(entity); } }
public static BasicHttpResponse okWith(String str) { BasicHttpResponse response = new BasicHttpResponse(StandardHttpLine.OK); response.setEntity(new StringEntity(str, ContentType.APPLICATION_JSON)); return response; }
@Override public HttpResponse head() { BasicHttpResponse response = new BasicHttpResponse(this.response.getStatusLine()); Header[] allHeaders = this.response.getAllHeaders(); for (Header allHeader : allHeaders) response.addHeader(allHeader); response.setEntity(new ByteArrayEntity(new byte[]{})); return response; }
@Override public HttpResponse getResponse(Request request) { BasicHttpResponse response = new BasicHttpResponse(new ProtocolVersion("http", 1, 1), statusCode, "ok"); response.setEntity(new StringEntity(this.response, this.charset)); return response; } }
/** * Build the HTTP response using all data previously set on this builder and/or use defaults. * * @return The HTTP response */ public CloseableHttpResponse build() { BasicHttpResponse response = new BasicHttpResponse(this.protocolVersion, this.status, this.reason); for (Header h : this.headers) { response.addHeader(h.getName(), h.getValue()); } if (this.entity != null) { response.setEntity(this.entity); } return BasicCloseableHttpResponse.adapt(response); }
private static BasicHttpResponse makeApacheResponse(int status, String body, Headers headers) { try { BasicHttpResponse apacheResponse = new BasicHttpResponse(createStatus(status)); apacheResponse.setEntity(new StringEntity(body)); setHeaders(apacheResponse, headers); return apacheResponse; } catch (UnsupportedEncodingException e) { throw new HttpClientException(e); } }
protected HttpResponse createResponse(int statusCode, ContentType contentType, String content) { final String reason = reasonPhraseCatalog.getReason(statusCode, Locale.ENGLISH); final BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, statusCode, reason); response.setHeader(HTTP.CONTENT_TYPE, contentType.toString()); final StringEntity entity = new StringEntity(content, contentType); response.setEntity(entity); return response; } }
ExecuteBuilder<T> withResponse(int code, String status, String mimeType, String body) { BasicHttpResponse r = new BasicHttpResponse( new BasicStatusLine(HttpVersion.HTTP_1_1, code, status) ); StringEntity e = new StringEntity(body, ContentType.create(mimeType, Charset.forName("UTF-8"))); r.setEntity(e); return withResponse(r); }
private static HttpResponse transformResponse(Response response) throws IOException { int code = response.code(); String message = response.message(); BasicHttpResponse httpResponse = new BasicHttpResponse(HTTP_1_1, code, message); ResponseBody body = response.body(); InputStreamEntity entity = new InputStreamEntity(body.byteStream(), body.contentLength()); httpResponse.setEntity(entity); Headers headers = response.headers(); for (int i = 0, size = headers.size(); i < size; i++) { String name = headers.name(i); String value = headers.value(i); httpResponse.addHeader(name, value); if ("Content-Type".equalsIgnoreCase(name)) { entity.setContentType(value); } else if ("Content-Encoding".equalsIgnoreCase(name)) { entity.setContentEncoding(value); } } return httpResponse; }
@Test public void headersAndPostParams() throws Exception { MockHttpStack mockHttpStack = new MockHttpStack(); BasicHttpResponse fakeResponse = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), 200, "OK"); fakeResponse.setEntity(new StringEntity("foobar")); mockHttpStack.setResponseToReturn(fakeResponse); BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack); Request<String> request = buildRequest(); httpNetwork.performRequest(request); assertEquals("foo", mockHttpStack.getLastHeaders().get("requestheader")); assertEquals("requestpost=foo&", new String(mockHttpStack.getLastPostBody())); }