Package org.apache.commons.httpclient.methods

Examples of org.apache.commons.httpclient.methods.PostMethod


   @Test
   public void testFormResource() throws Exception
   {
      HttpClient client = new HttpClient();
      PostMethod method = new PostMethod(TEST_URI);
      method.addRequestHeader("custom-header", "42");
      method.addParameter(BOOLEAN_VALUE_FIELD, "true");
      method.addParameter(NAME_FIELD, "This is My Name");
      method.addParameter(DOUBLE_VALUE_FIELD, "123.45");
      method.addParameter(LONG_VALUE_FIELD, "566780");
      method.addParameter(INTEGER_VALUE_FIELD, "3");
      method.addParameter(SHORT_VALUE_FIELD, "12345");
      int status = client.executeMethod(method);
      Assert.assertEquals(HttpServletResponse.SC_OK, status);
      InputStream response = method.getResponseBodyAsStream();
      BufferedInputStream in = new BufferedInputStream(response);
      String contentType = method.getResponseHeader("content-type").getValue();
      Assert.assertEquals("application/x-www-form-urlencoded", contentType);
      String formData = readString(in);
      String[] keys = formData.split("&");
      Map<String, String> values = new HashMap<String, String>();
      for (String pair : keys)
View Full Code Here


   @Test
   public void test534() throws Exception
   {
      HttpClient client = new HttpClient();
      PostMethod post = new PostMethod(generateURL("/inputstream/test/json"));
      ByteArrayRequestEntity entity = new ByteArrayRequestEntity("hello world".getBytes(), null);
      post.setRequestEntity(entity);
      int status = client.executeMethod(post);
      Assert.assertEquals(204, status);
      post.releaseConnection();
   }
View Full Code Here

   {
      HttpClient client = new HttpClient();
      //File file = new File(SRC_ROOT + "harper.jpg");
      File file = LocateTestData.getTestData("harper.jpg");
      Assert.assertTrue(file.exists());
      PostMethod method = new PostMethod(TEST_URI);
      method.setRequestEntity(new FileRequestEntity(file, "image/jpeg"));
      int status = client.executeMethod(method);
      Assert.assertEquals(HttpServletResponse.SC_OK, status);
      InputStream response = method.getResponseBodyAsStream();
      BufferedInputStream in = new BufferedInputStream(response);
      String contentType = method.getResponseHeader("content-type").getValue();
      Assert.assertEquals("image/png", contentType);

      ByteArrayOutputStream fromServer = new ByteArrayOutputStream();
      writeTo(in, fromServer);
      method.releaseConnection();

      File savedPng = LocateTestData.getTestData("harper.png");
      FileInputStream fis = new FileInputStream(savedPng);

      ByteArrayOutputStream fromTestData = new ByteArrayOutputStream();
View Full Code Here

   {
      HttpClient client = new HttpClient();
      //File file = new File(SRC_ROOT + "harper.wdp");
      File file = LocateTestData.getTestData("harper.wdp");
      Assert.assertTrue(file.exists());
      PostMethod method = new PostMethod(TEST_URI);
      method.setRequestEntity(new FileRequestEntity(file, "image/vnd.ms-photo"));
      int status = client.executeMethod(method);
      Assert.assertEquals(HttpServletResponse.SC_NOT_ACCEPTABLE, status);
   }
View Full Code Here

   @Test
   public void testResteasy109()
   {
      HttpClient client = new HttpClient();
      {
         PostMethod method = createPostMethod("/RESTEASY-109");
         try
         {
            method.setRequestEntity(new StringRequestEntity("name=jon&address1=123+Main+St&address2=&zip=12345",
                    MediaType.APPLICATION_FORM_URLENCODED, null));
            int status = client.executeMethod(method);
            Assert.assertEquals(status, 204);
         }
         catch (IOException e)
View Full Code Here

   @Test
   public void testQueryParamIsNull()
   {
      HttpClient client = new HttpClient();
      {
         PostMethod method = createPostMethod("/simple");
         NameValuePair[] params =
                 {new NameValuePair("hello", "world")};
         method.setRequestBody(params);
         try
         {
            int status = client.executeMethod(method);
            Assert.assertEquals(status, HttpResponseCodes.SC_OK);
            String body = method.getResponseBodyAsString();
            Assert.assertEquals("hello=world", body);
         }
         catch (IOException e)
         {
            throw new RuntimeException(e);
View Full Code Here

   @Test
   public void testPost()
   {
      HttpClient client = new HttpClient();
      {
         PostMethod method = createPostMethod("/form");
         NameValuePair[] params =
                 {new NameValuePair("hello", "world")};
         method.setRequestBody(params);
         try
         {
            int status = client.executeMethod(method);
            Assert.assertEquals(status, HttpResponseCodes.SC_OK);
            String body = method.getResponseBodyAsString();
            Assert.assertEquals("hello=world", body);
         }
         catch (IOException e)
         {
            throw new RuntimeException(e);
View Full Code Here

   @Test
   public void testPostTwoParameters()
   {
      HttpClient client = new HttpClient();
      {
         PostMethod method = createPostMethod("/form/twoparams");
         NameValuePair[] params =
                 {new NameValuePair("hello", "world"), new NameValuePair("yo", "mama")};
         method.setRequestBody(params);
         try
         {
            int status = client.executeMethod(method);
            Assert.assertEquals(status, HttpResponseCodes.SC_OK);
            String body = method.getResponseBodyAsString();
            Assert.assertTrue(body.indexOf("hello=world") != -1);
            Assert.assertTrue(body.indexOf("yo=mama") != -1);
         }
         catch (IOException e)
         {
View Full Code Here

   {
      HttpClient client = new HttpClient();
      //File file = new File("./src/test/test-data/harper.jpg");
      File file = LocateTestData.getTestData("harper.jpg");
      Assert.assertTrue(file.exists());
      PostMethod method = new PostMethod(TEST_URI);
      method.setRequestEntity(new FileRequestEntity(file, "image/jpeg"));
      int status = client.executeMethod(method);
      Assert.assertEquals(HttpServletResponse.SC_OK, status);
      Assert.assertEquals("image/jpeg", method.getResponseBodyAsString());
      method.releaseConnection();
   }
View Full Code Here

   public void testEchoDataSourceBigData() throws Exception
   {
      HttpClient client = new HttpClient();
      File file = LocateTestData.getTestData("harper.jpg");
      Assert.assertTrue(file.exists());
      PostMethod method = new PostMethod(TEST_URI + "/echo");
      method.setRequestEntity(new FileRequestEntity(file, "image/jpeg"));
      int status = client.executeMethod(method);
      Assert.assertEquals(HttpServletResponse.SC_OK, status);

      InputStream ris = null;
      InputStream fis = null;
      try
      {
         ris = method.getResponseBodyAsStream();
         fis = new FileInputStream(file);
         int fi;
         int ri;
         do
         {
            fi = fis.read();
            ri = ris.read();
            if (fi != ri)
               Assert.fail("The sent and recived stream is not identical.");
         } while (fi != -1);
      }
      finally
      {
         if (ris != null)
            ris.close();
         if (fis != null)
            fis.close();
      }

      method.releaseConnection();
   }
View Full Code Here

TOP

Related Classes of org.apache.commons.httpclient.methods.PostMethod

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.