HTTPCLIENT-2065: Simplify additon of content type parameters in MultipartEntityBuilder

This closes #320
This commit is contained in:
Arturo Bernal 2021-11-01 09:41:58 +01:00 committed by Michael Osipov
parent af2cc82e82
commit d2c59fd5e4
2 changed files with 25 additions and 0 deletions

View File

@ -112,6 +112,17 @@ public class MultipartEntityBuilder {
this.contentType = contentType;
return this;
}
/**
* Add parameter to the current {@link ContentType}.
*
* @param parameter The name-value pair parameter to add to the {@link ContentType}.
* @return the {@link MultipartEntityBuilder} instance.
* @since 5.2
*/
public MultipartEntityBuilder addParameter(final BasicNameValuePair parameter) {
this.contentType = contentType.withParameters(parameter);
return this;
}
public MultipartEntityBuilder setCharset(final Charset charset) {
this.charset = charset;

View File

@ -120,6 +120,20 @@ public class TestMultipartEntityBuilder {
entity.getContentType());
}
@Test
public void testMultipartCustomContentTypeUsingAddParameter() {
final MultipartEntityBuilder eb = MultipartEntityBuilder.create();
eb.setMimeSubtype("related");
eb.addParameter(new BasicNameValuePair("boundary", "yada-yada"));
eb.addParameter(new BasicNameValuePair("charset", "ascii"));
eb.addParameter(new BasicNameValuePair("my", "stuff"));
eb.buildEntity();
final MultipartFormEntity entity = eb.buildEntity();
Assert.assertNotNull(entity);
Assert.assertEquals("multipart/related; boundary=yada-yada; charset=US-ASCII; my=stuff",
entity.getContentType());
}
@Test
public void testMultipartWriteTo() throws Exception {
final String helloWorld = "hello world";