Package org.apache.commons.httpclient.methods

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


   @Test
   public void testEchoDataSourceSmallData() throws Exception
   {
      HttpClient client = new HttpClient();
      byte[] input = "Hello World!".getBytes("utf-8");
      PostMethod method = new PostMethod(TEST_URI + "/echo");
      method.setRequestEntity(new ByteArrayRequestEntity(input, MediaType.APPLICATION_OCTET_STREAM));
      int status = client.executeMethod(method);
      Assert.assertEquals(HttpServletResponse.SC_OK, status);

      InputStream ris = null;
      InputStream bis = null;
      try
      {
         ris = method.getResponseBodyAsStream();
         bis = new ByteArrayInputStream(input);
         int fi;
         int ri;
         do
         {
            fi = bis.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 (bis != null)
            bis.close();
      }

      method.releaseConnection();
   }
View Full Code Here


   @Test
   public void testPost() throws Exception
   {
      {
         HttpClient client = new HttpClient();
         PostMethod method = createPostMethod("");
         try
         {
            method.setRequestEntity(new StringRequestEntity(
                    "<?xml version=\"1.0\"?><person><name>bill</name></person>",
                    "application/xml", null));
            int status = client.executeMethod(method);
            Assert.assertEquals(status, 204);
         }
         catch (IOException e)
         {
            throw new RuntimeException(e);
         }
      }
      {
         HttpClient client = new HttpClient();
         PostMethod method = createPostMethod("/kunde");
         try
         {
            method.setRequestEntity(new StringRequestEntity(kundeXml, "application/xml", null));
            int status = client.executeMethod(method);
            Assert.assertEquals(status, 204);
         }
         catch (IOException e)
         {
View Full Code Here

   @Test
   public void testNotAcceptable()
   {
      HttpClient client = new HttpClient();
      PostMethod method = new PostMethod(generateBaseUrl());
      method.addRequestHeader(HttpHeaderNames.ACCEPT, "application/bar");
      try
      {
         method.setRequestEntity(new StringRequestEntity("content", "application/bar", null));
         int status = client.executeMethod(method);
         Assert.assertEquals(status, HttpResponseCodes.SC_NOT_ACCEPTABLE);
      }
      catch (IOException e)
      {
         method.releaseConnection();
         throw new RuntimeException(e);
      }
      method.releaseConnection();
   }
View Full Code Here

   @Test
   public void testNoContentPost()
   {
      HttpClient client = new HttpClient();
      PostMethod method = createPostMethod("/nocontent");
      try
      {
         method.setRequestEntity(new StringRequestEntity("content", "text/plain", null));
         int status = client.executeMethod(method);
         Assert.assertEquals(status, HttpResponseCodes.SC_NO_CONTENT);
      }
      catch (IOException e)
      {
         method.releaseConnection();
         throw new RuntimeException(e);
      }
      method.releaseConnection();
   }
View Full Code Here

   @Test
   public void testUnsupportedMediaType()
   {
      HttpClient client = new HttpClient();
      PostMethod method = new PostMethod(generateBaseUrl());
      method.addRequestHeader(HttpHeaderNames.ACCEPT, "application/foo");
      try
      {
         method.setRequestEntity(new StringRequestEntity("content", "text/plain", null));
         int status = client.executeMethod(method);
         Assert.assertEquals(status, HttpResponseCodes.SC_UNSUPPORTED_MEDIA_TYPE);
      }
      catch (IOException e)
      {
         method.releaseConnection();
         throw new RuntimeException(e);
      }
      method.releaseConnection();

   }
View Full Code Here

    server.start();
   
   
   
    HttpClient httpClient = new HttpClient();
    PostMethod meth = new PostMethod("http://localhost:" + server.getLocalPort() + "/");
    meth.setRequestBody("test123456");

    httpClient.executeMethod(meth);
   
    String body = meth.getResponseBodyAsString();
    Assert.assertEquals("Hello", body);
 
    meth.releaseConnection();
    server.close();
  }
View Full Code Here

    server.start();
   
   
   
    HttpClient httpClient = new HttpClient();
    PostMethod meth = new PostMethod("http://localhost:" + server.getLocalPort() + "/");
    meth.setRequestBody("test123456");

    httpClient.executeMethod(meth);
   
    String body = meth.getResponseBodyAsString();
    Assert.assertEquals("Hello", body);
 
    meth.releaseConnection();
    server.close();
  }
View Full Code Here

  @Test
  public void testApacheClient() throws Exception {

    org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
   
    PostMethod postMethod = new PostMethod(url);
    postMethod.addParameter(new org.apache.commons.httpclient.NameValuePair("file", new String(HttpUtils.encodeBase64(data), "US-ASCII")));

    httpClient.executeMethod(postMethod);
     
      Assert.assertEquals(200, postMethod.getStatusCode());
      Assert.assertTrue(QAUtil.isEquals(file, postMethod.getResponseBody()));
     
      postMethod.releaseConnection();
  }     
View Full Code Here

                                    final String[] userNameAndPassword,
                                    final Map<String, String> parameters)
        throws IOException
    {
        final HttpClient client = new HttpClient();
        final PostMethod method = new PostMethod(address);

        if (userNameAndPassword != null && userNameAndPassword.length == 2) {
            client.getState().setCredentials(null,
                                             null,
                                             new UsernamePasswordCredentials(userNameAndPassword[0],
                                                                             userNameAndPassword[1]));
        }

        if (parameters != null) {
            for (Map.Entry<String, String> e : parameters.entrySet()) {
                method.addParameter(e.getKey(), e.getValue());
            }
        }
        client.executeMethod(method);
        return method;
    }
View Full Code Here

     
     
      HttpServer server = new HttpServer(new RequestHandler());
      server.start();
             
      PostMethod filePost = new PostMethod("http://localhost:" + server.getLocalPort() + "/");
      org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
     
      File file = QAUtil.createTestfile_4k();
      Part part = new FilePart("file4k", file);
     
      HttpMethodParams params = new HttpMethodParams();
      params.setBooleanParameter("testb", false);
      filePost.setRequestEntity(new MultipartRequestEntity(new Part[] {part }, params));
     
      client.executeMethod(filePost);
     
      if (filePost.getStatusCode() != 200) {
          System.out.println(filePost.getResponseBodyAsString());
          Assert.fail("error occured");
        }
 
      file.delete();
      server.close();
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.