List of usage examples for org.apache.http.client.methods HttpPost getEntity
public HttpEntity getEntity()
From source file:com.groupme.sdk.util.HttpUtilsTest.java
public void testSetHttpParamsPost() { MockHttpClient client = new MockHttpClient(); client.setContext(getContext());// ww w. j a v a 2 s. c o m String response = null; try { Bundle params = new Bundle(); params.putString("group", "mygroup"); params.putString("format", "json"); response = HttpUtils.openUrl(client, OK_REQUEST, HttpUtils.METHOD_POST, null, params, null); } catch (HttpResponseException e) { fail("Received a response exception: " + e.toString()); } try { HttpPost request = (HttpPost) client.getRequest(); InputStream in = request.getEntity().getContent(); BufferedReader r = new BufferedReader(new InputStreamReader(in), 1000); StringBuilder sb = new StringBuilder(); for (String line = r.readLine(); line != null; line = r.readLine()) { sb.append(line); } assertTrue(!sb.toString().equals("")); Bundle bodyParams = HttpUtils.decodeParams(sb.toString()); assertEquals("mygroup", bodyParams.getString("group")); assertEquals("json", bodyParams.getString("format")); } catch (IOException e) { fail("Error reading post body: " + e.toString()); } if (response == null) { fail("Unexpected empty response"); } }
From source file:org.plos.crepo.dao.buckets.impl.ContentRepoBucketDaoImplTest.java
@Test public void createBucketTest() throws IOException { Mockito.when(BucketUrlGenerator.getCreateBucketUrl(REPO_SERVER)).thenReturn(SOME_URL); ArgumentCaptor<HttpPost> httpPostArgument = ArgumentCaptor.forClass(HttpPost.class); mockCommonCalls(repoAccessConfig, HttpStatus.SC_CREATED); HttpResponse response = contentRepoBucketDaoImpl.createBucket(BUCKET_NAME); verifyCommonCalls(repoAccessConfig, httpPostArgument, statusLine, 1, 1); PowerMockito.verifyStatic();//from www. j a va2 s. com assertNotNull(response); assertEquals(mockResponse, response); HttpPost httpPost = httpPostArgument.getValue(); String params = EntityUtils.toString(httpPost.getEntity(), CharEncoding.UTF_8); assertTrue(params.contains("name=" + BUCKET_NAME)); }
From source file:org.plos.crepo.dao.buckets.impl.ContentRepoBucketDaoImplTest.java
@Test public void createBucketThrowsExcTest() throws IOException { when(BucketUrlGenerator.getCreateBucketUrl(REPO_SERVER)).thenReturn(SOME_URL); ArgumentCaptor<HttpPost> httpPostArgument = ArgumentCaptor.forClass(HttpPost.class); mockCommonCalls(repoAccessConfig, HttpStatus.SC_BAD_REQUEST); mockHttpResponseUtilCalls(mockResponse); HttpResponse response = null;/*from www . j a va 2 s. c o m*/ try { response = contentRepoBucketDaoImpl.createBucket(BUCKET_NAME); fail(EXCEPTION_EXPECTED); } catch (ContentRepoException ex) { verifyException(ex, response, ErrorType.ErrorCreatingBucket); } verifyCommonCalls(repoAccessConfig, httpPostArgument, statusLine, 1, 1); PowerMockito.verifyStatic(); HttpPost httpPost = httpPostArgument.getValue(); String params = EntityUtils.toString(httpPost.getEntity(), CharEncoding.UTF_8); assertTrue(params.contains("name=" + BUCKET_NAME)); }
From source file:com.seleritycorp.context.RequestUtilsTest.java
private String requestEntityToString(HttpPost post) throws IOException { ByteArrayOutputStream stream = new ByteArrayOutputStream(); post.getEntity().writeTo(stream); return stream.toString(); }
From source file:com.klinker.android.spotify.loader.OAuthTokenRefresherTest.java
@Test public void test_newPostRequest_noEntity() { String url = "test_url"; String entity = null;/*from ww w.j a v a 2 s . c om*/ HttpPost post = refresher.newPostRequest(url, entity); assertNull(post.getEntity()); }
From source file:com.klinker.android.spotify.loader.OAuthTokenRefresherTest.java
@Test public void test_newPostRequest() { String url = "test_url"; String entity = "test_entity"; HttpPost post = refresher.newPostRequest(url, entity); assertNotNull(post.getEntity()); }
From source file:com.gistlabs.mechanize.PageRequest.java
private void assertParameters(final HttpRequestBase request) throws ArrayComparisonFailure { if (request instanceof HttpPost) { HttpPost post = (HttpPost) request; HttpEntity entity = post.getEntity(); Parameters actualParameters = extractParameters(entity); String[] expectedNames = parameters.getNames(); String[] actualNames = actualParameters.getNames(); Arrays.sort(expectedNames); Arrays.sort(actualNames); Assert.assertArrayEquals("Expected and actual parameters should equal by available parameter names", expectedNames, actualNames); for (String name : expectedNames) { String[] expectedValue = parameters.get(name); String[] actualValue = actualParameters.get(name); Assert.assertArrayEquals("Expected parameter of next PageRequest '" + uri + "' must match", expectedValue, actualValue); }/*from ww w . j a v a 2 s.c o m*/ } }
From source file:com.gistlabs.mechanize.PageRequest.java
private void assertHeaders(final HttpRequestBase request) throws ArrayComparisonFailure { for (Header header : headers) { org.apache.http.Header[] requestHeaders = request.getHeaders(header.getName()); boolean foundMatch = false; for (org.apache.http.Header requestHeader : requestHeaders) if (header.getValues().contains(requestHeader.getValue())) foundMatch = true;/*from ww w . j a va 2s .c o m*/ if (!foundMatch) Assert.fail(String.format("Could not find request header matching: %s", header)); } if (request instanceof HttpPost) { HttpPost post = (HttpPost) request; HttpEntity entity = post.getEntity(); Parameters actualParameters = extractParameters(entity); String[] expectedNames = parameters.getNames(); String[] actualNames = actualParameters.getNames(); Arrays.sort(expectedNames); Arrays.sort(actualNames); Assert.assertArrayEquals("Expected and actual parameters should equal by available parameter names", expectedNames, actualNames); for (String name : expectedNames) { String[] expectedValue = parameters.get(name); String[] actualValue = actualParameters.get(name); Assert.assertArrayEquals("Expected parameter of next PageRequest '" + uri + "' must match", expectedValue, actualValue); } } }
From source file:com.socialize.net.AsyncHttpRequestProcessor.java
@Override protected AsyncHttpResponse doInBackground(AsyncHttpRequest... params) { AsyncHttpRequest request = params[0]; AsyncHttpResponse response = new AsyncHttpResponse(); response.setRequest(request);//from w w w .j av a 2s . c o m try { HttpUriRequest httpRequest = request.getRequest(); if (!clientFactory.isDestroyed()) { if (logger != null && logger.isDebugEnabled()) { logger.debug( "Request: " + httpRequest.getMethod() + " " + httpRequest.getRequestLine().getUri()); StringBuilder builder = new StringBuilder(); Header[] allHeaders = httpRequest.getAllHeaders(); for (Header header : allHeaders) { builder.append(header.getName()); builder.append(":"); builder.append(header.getValue()); builder.append("\n"); } logger.debug("REQUEST \nurl:[" + httpRequest.getURI().toString() + "] \nheaders:\n" + builder.toString()); if (httpRequest instanceof HttpPost) { HttpPost post = (HttpPost) httpRequest; HttpEntity entity = post.getEntity(); if (!(entity instanceof MultipartEntity)) { String requestData = ioUtils.readSafe(entity.getContent()); logger.debug("REQUEST \ndata:[" + requestData + "]"); } } } HttpClient client = clientFactory.getClient(); HttpResponse httpResponse = client.execute(httpRequest); response.setResponse(httpResponse); if (logger != null && logger.isDebugEnabled()) { logger.debug("RESPONSE CODE: " + httpResponse.getStatusLine().getStatusCode()); } HttpEntity entity = null; try { entity = httpResponse.getEntity(); if (httpUtils.isHttpError(httpResponse)) { String msg = ioUtils.readSafe(entity.getContent()); throw new SocializeApiError(httpUtils, httpResponse.getStatusLine().getStatusCode(), msg); } else { String responseData = ioUtils.readSafe(entity.getContent()); if (logger != null && logger.isDebugEnabled()) { logger.debug("RESPONSE: " + responseData); } response.setResponseData(responseData); } } finally { closeEntity(entity); } } else { throw new SocializeException("Cannot execute http request. HttpClient factory was destroyed."); } } catch (Exception e) { response.setError(e); } return response; }
From source file:com.fhc25.percepcion.osiris.mapviewer.common.restutils.RestClient.java
/** * Executes the requests./*from www .ja va 2 s . co m*/ * * @param method the type of method (POST, GET, DELETE, PUT). * @param url the url of the request. * @param headers the headers to include. * @param listener a listener for callbacks. * @throws Exception */ public static void Execute(final File file, final RequestMethod method, final String url, final ArrayList<NameValuePair> headers, final RestListener listener) throws Exception { new Thread() { @Override public void run() { switch (method) { case GET: // Do nothing break; case POST: { HttpPost request = new HttpPost(url); // add headers if (headers != null) { for (NameValuePair h : headers) request.addHeader(h.getName(), h.getValue()); } // code file MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); Log.d(TAG, "UPLOAD: file length = " + file.length()); Log.d(TAG, "UPLOAD: file exist = " + file.exists()); FileBody bodyPart = new FileBody(file); entity.addPart("file", bodyPart); request.setEntity(entity); Log.i(TAG, "Request with File:" + request.getEntity()); executeRequest(request, url, listener); } break; case PUT: { HttpPut request = new HttpPut(url); // add headers if (headers != null) { for (NameValuePair h : headers) request.addHeader(h.getName(), h.getValue()); } // code file MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); Log.d(TAG, "UPLOAD: file length = " + file.length()); Log.d(TAG, "UPLOAD: file exist = " + file.exists()); FileBody bodyPart = new FileBody(file); entity.addPart("file", bodyPart); request.setEntity(entity); Log.v(TAG, "Request " + request.toString()); executeRequest(request, url, listener); } break; case DELETE: // Do nothing break; } // switch end } }.start(); }