コード例 #1
0
  @Test
  public void postOneParameter() throws Exception {
    String value =
        "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKQLMNOPQRSTUVWXYZ1234567809`~!@#$%^&*()_+-=,.<>/?;:'\"[]{}\\| ";

    final AtomicReference<String> param1Value = new AtomicReference<>();

    integrationServer.addServlet(
        new HttpServlet() {
          @Override
          protected void doPost(HttpServletRequest req, HttpServletResponse resp)
              throws ServletException, IOException {
            param1Value.set(req.getParameter("param1"));
            resp.getWriter().write(okBody);
            resp.getWriter().flush();
          }
        },
        "/testOk");

    BlockingHttpCallback httpCallback = new BlockingHttpCallback();
    httpClient
        .createPost("http://localhost:" + port + "/testOk")
        .addFormParameter("param1", value)
        .build()
        .execute(httpCallback);

    httpCallback.waitForCompletion();

    assertEquals(okBody, httpCallback.getBody());
    assertEquals(200, httpCallback.getStatusCode());

    assertEquals(value, param1Value.get());
  }
コード例 #2
0
  @Test
  public void postMapOfParameters() throws Exception {
    final Map<String, String> parameters = new HashMap<>();
    parameters.put("param1", "value1");
    parameters.put("param2", "value2");
    parameters.put("param3", "value3");

    final Map<String, String> postedValues = new HashMap<>();

    integrationServer.addServlet(
        new HttpServlet() {
          @Override
          protected void doPost(HttpServletRequest req, HttpServletResponse resp)
              throws ServletException, IOException {

            for (String key : parameters.keySet()) {
              postedValues.put(key, req.getParameter(key));
            }

            resp.getWriter().write(okBody);
            resp.getWriter().flush();
          }
        },
        "/testOk");

    BlockingHttpCallback httpCallback = new BlockingHttpCallback();
    httpClient
        .createPost("http://localhost:" + port + "/testOk")
        .addFormParameters(parameters)
        .build()
        .execute(httpCallback);

    httpCallback.waitForCompletion();

    assertEquals(okBody, httpCallback.getBody());
    assertEquals(200, httpCallback.getStatusCode());

    assertEquals(parameters, postedValues);
  }