예제 #1
0
  /**
   * Adds many query parameters at once. Although it accepts objects, each value will be converted
   * to string.
   */
  public HttpRequest query(String name1, Object value1, Object... parameters) {
    query(name1, value1 == null ? null : value1.toString());

    for (int i = 0; i < parameters.length; i += 2) {
      String name = parameters[i].toString();

      String value = parameters[i + 1].toString();
      query.add(name, value == null ? null : value);
    }
    return this;
  }
예제 #2
0
  @Test
  public void testSet() {
    HttpRequest httpRequest = new HttpRequest();
    httpRequest.set("GET http://jodd.org:173/index.html?light=true");

    assertEquals("GET", httpRequest.method());
    assertEquals("http", httpRequest.protocol());
    assertEquals("jodd.org", httpRequest.host());
    assertEquals(173, httpRequest.port());
    assertEquals("/index.html", httpRequest.path());
    assertEquals("true", httpRequest.query().get("light")[0]);

    httpRequest = new HttpRequest();
    httpRequest.set("http://jodd.org:173/index.html?light=true");

    assertEquals("GET", httpRequest.method());
    assertEquals("http", httpRequest.protocol());
    assertEquals("jodd.org", httpRequest.host());
    assertEquals(173, httpRequest.port());
    assertEquals("/index.html", httpRequest.path());
    assertEquals("true", httpRequest.query().get("light")[0]);

    httpRequest = new HttpRequest();
    httpRequest.set("jodd.org:173/index.html?light=true");

    assertEquals("GET", httpRequest.method());
    assertEquals("http", httpRequest.protocol());
    assertEquals("jodd.org", httpRequest.host());
    assertEquals(173, httpRequest.port());
    assertEquals("/index.html", httpRequest.path());
    assertEquals("true", httpRequest.query().get("light")[0]);

    httpRequest = new HttpRequest();
    httpRequest.set("jodd.org/index.html?light=true");

    assertEquals("GET", httpRequest.method());
    assertEquals("http", httpRequest.protocol());
    assertEquals("jodd.org", httpRequest.host());
    assertEquals(80, httpRequest.port());
    assertEquals("/index.html", httpRequest.path());
    assertEquals("true", httpRequest.query().get("light")[0]);

    httpRequest = new HttpRequest();
    httpRequest.set("/index.html?light=true");

    assertEquals("GET", httpRequest.method());
    assertEquals("http", httpRequest.protocol());
    assertEquals("localhost", httpRequest.host());
    assertEquals(80, httpRequest.port());
    assertEquals("/index.html", httpRequest.path());
    assertEquals("true", httpRequest.query().get("light")[0]);

    httpRequest = new HttpRequest();
    httpRequest.set("http://jodd.org");

    assertEquals("GET", httpRequest.method());
    assertEquals("http", httpRequest.protocol());
    assertEquals("jodd.org", httpRequest.host());
    assertEquals(80, httpRequest.port());
    assertEquals("/", httpRequest.path());
  }
예제 #3
0
  @Test
  public void testQueryParameters() {
    HttpRequest httpRequest = new HttpRequest();

    httpRequest.path("");
    assertEquals("/", httpRequest.path());

    httpRequest.path("jodd");
    assertEquals("/jodd", httpRequest.path());
    assertNotNull(httpRequest.query());
    assertEquals(0, httpRequest.query().size());

    httpRequest.queryString("one=two");
    assertEquals("/jodd", httpRequest.path());

    Map<String, Object[]> params = httpRequest.query();
    assertEquals(1, params.size());
    assertEquals("two", params.get("one")[0]);

    httpRequest.queryString("one");
    assertEquals("one", httpRequest.queryString());
    params = httpRequest.query();
    assertEquals(1, params.size());
    assertNull(params.get("one"));

    httpRequest.queryString("one=");
    assertEquals("one=", httpRequest.queryString());
    params = httpRequest.query();
    assertEquals(1, params.size());
    assertEquals("", params.get("one")[0]);

    httpRequest.queryString("one=aaa&two=bbb");
    assertEquals("one=aaa&two=bbb", httpRequest.queryString());
    params = httpRequest.query();
    assertEquals(2, params.size());
    assertEquals("aaa", params.get("one")[0]);
    assertEquals("bbb", params.get("two")[0]);

    httpRequest.queryString("one=&two=aaa");
    assertEquals("one=&two=aaa", httpRequest.queryString());
    params = httpRequest.query();
    assertEquals(2, params.size());
    assertEquals("", params.get("one")[0]);
    assertEquals("aaa", params.get("two")[0]);

    httpRequest.clearQueries();
    httpRequest.queryString("one=Супер");
    assertEquals("one=%D0%A1%D1%83%D0%BF%D0%B5%D1%80", httpRequest.queryString());
    params = httpRequest.query();
    assertEquals(1, params.size());
    assertEquals("Супер", params.get("one")[0]);

    httpRequest.queryString("one=Sуp");
    assertEquals("one=S%D1%83p", httpRequest.queryString());

    httpRequest.queryString("one=1&one=2");
    assertEquals("one=1&one=2", httpRequest.queryString());
    params = httpRequest.query();
    assertEquals(1, params.size());
    assertEquals("1", params.get("one")[0]);
    assertEquals("2", params.get("one")[1]);

    httpRequest.query("one", Integer.valueOf(3));
    assertEquals("one=1&one=2&one=3", httpRequest.queryString());
  }