/** Test that the body can be set as a String without an explict content type */
  public void testStringBodyToBodyServlet() throws Exception {
    PostMethod method = new PostMethod("/");
    String stringBody = "pname1=pvalue1&pname2=pvalue2";

    method.setRequestEntity(new StringRequestEntity(stringBody, null, null));
    this.server.setHttpService(new EchoService());
    try {
      this.client.executeMethod(method);
      assertEquals(200, method.getStatusCode());
      String body = method.getResponseBodyAsString();
      assertEquals("pname1=pvalue1&pname2=pvalue2", body);
    } finally {
      method.releaseConnection();
    }
  }
  /** Test that parameters can be added. */
  public void testAddParametersToParamServlet() throws Exception {
    PostMethod method = new PostMethod("/");

    method.addParameter(new NameValuePair("pname1", "pvalue1"));
    method.addParameter(new NameValuePair("pname2", "pvalue2"));

    this.server.setHttpService(new EchoService());
    try {
      this.client.executeMethod(method);
      assertEquals(200, method.getStatusCode());
      String body = method.getResponseBodyAsString();
      assertEquals("pname1=pvalue1&pname2=pvalue2", body);
    } finally {
      method.releaseConnection();
    }
  }
コード例 #3
0
  protected void tearDown() {

    // Delete the content
    PostMethod post_delete = new PostMethod(SLING_URL + COLLECTION_URL + slug);
    NameValuePair[] data_delete = {
      new NameValuePair(":operation", "delete"),
    };

    post_delete.setDoAuthentication(true);
    post_delete.setRequestBody(data_delete);

    try {
      client.executeMethod(post_delete);
    } catch (HttpException e1) {
      e1.printStackTrace();
    } catch (IOException e1) {
      e1.printStackTrace();
    }
    post_delete.releaseConnection();
  }
コード例 #4
0
  protected void setUp() {

    Map<String, String> envs = System.getenv();
    Set<String> keys = envs.keySet();

    Iterator<String> it = keys.iterator();
    boolean hashost = false;
    while (it.hasNext()) {
      String key = (String) it.next();

      if (key.compareTo(HOSTVAR) == 0) {
        SLING_URL = SLING_URL + (String) envs.get(key);
        hashost = true;
      }
    }

    if (hashost == false) SLING_URL = SLING_URL + HOSTPREDEF;

    client = new HttpClient();
    title = "IT col";
    schema = "default";
    slug = "it_col";
    subtitle = "subtitle it";
    extern_storage = "on";

    authPrefs.add(AuthPolicy.DIGEST);
    authPrefs.add(AuthPolicy.BASIC);
    defaultcreds = new UsernamePasswordCredentials("admin", "admin");

    client.getParams().setAuthenticationPreemptive(true);
    client.getState().setCredentials(AuthScope.ANY, defaultcreds);
    client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);

    PostMethod post_create = new PostMethod(SLING_URL + COLLECTION_URL + slug);

    post_create.setDoAuthentication(true);

    NameValuePair[] data_create = {
      new NameValuePair("sling:resourceType", "gad/collection"),
      new NameValuePair("title", title),
      new NameValuePair("schema", schema),
      new NameValuePair("subtitle", subtitle),
      new NameValuePair("extern_storage", extern_storage),
      new NameValuePair("picture", ""),
      new NameValuePair("jcr:created", ""),
      new NameValuePair("jcr:createdBy", ""),
      new NameValuePair("jcr:lastModified", ""),
      new NameValuePair("jcr:lastModifiedBy", "")
    };
    post_create.setRequestBody(data_create);

    try {
      client.executeMethod(post_create);
    } catch (HttpException e1) {
      e1.printStackTrace();
    } catch (IOException e1) {
      e1.printStackTrace();
    }

    post_create.releaseConnection();
  }