HTTPRequest { public void form(Map<String, String> form) { if (method != HTTPMethod.POST) throw new Error("form must be used with POST, method=" + method); this.form = form; var builder = new StringBuilder(256); HTTPRequestHelper.urlEncoding(builder, form); body(Strings.bytes(builder.toString()), ContentType.APPLICATION_FORM_URLENCODED); } HTTPRequest(HTTPMethod method, String uri); void accept(ContentType contentType); void basicAuth(String user, String password); void bearerAuth(String token); void body(byte[] body, ContentType contentType); void body(String body, ContentType contentType); void form(Map<String, String> form); String requestURI(); final HTTPMethod method; final Map<String, String> params; final Map<String, String> headers; public String uri; public byte[] body; public ContentType contentType; public Map<String, String> form; public Duration connectTimeout; public Duration timeout; public Duration slowOperationThreshold; }
@Test void form() { var request = new HTTPRequest(HTTPMethod.POST, "http: Map<String, String> form = new LinkedHashMap<>(); form.put("key1", "v1 v2"); form.put("key2", "v1+v2"); request.form(form); assertThat(request.contentType).isEqualTo(ContentType.APPLICATION_FORM_URLENCODED); assertThat(request.headers).containsEntry(HTTPHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded"); assertThat(new String(request.body, UTF_8)).isEqualTo("key1=v1+v2&key2=v1%2Bv2"); assertThatThrownBy(() -> new HTTPRequest(HTTPMethod.PUT, "http: .isInstanceOf(Error.class) .hasMessageContaining("form must be used with POST"); }