Skip to content

Commit

Permalink
add a convience method for setting the content type
Browse files Browse the repository at this point in the history
  • Loading branch information
ryber committed Nov 18, 2020
1 parent 60affea commit 998ad0b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 0 deletions.
6 changes: 6 additions & 0 deletions unirest/src/main/java/kong/unirest/HttpRequestBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ public Charset getCharset() {
return charSet;
}

@Override
public HttpRequestWithBody contentType(String type) {
headers.add("Content-Type", type);
return this;
}

@Override
public Optional<Body> getBody() {
return Optional.empty();
Expand Down
6 changes: 6 additions & 0 deletions unirest/src/main/java/kong/unirest/HttpRequestUniBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ public RequestBodyEntity charset(Charset charset) {
return this;
}

@Override
public RequestBodyEntity contentType(String type) {
headers.add("Content-Type", type);
return this;
}

@Override
public Optional<Body> getBody() {
return Optional.of(this);
Expand Down
6 changes: 6 additions & 0 deletions unirest/src/main/java/kong/unirest/HttpRequestWithBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,10 @@ default HttpRequestWithBody noCharset() {
* @return the Charset
*/
Charset getCharset();

/**
* @param type The content mime type
* @return this request builder
*/
HttpRequestWithBody contentType(String type);
}
6 changes: 6 additions & 0 deletions unirest/src/main/java/kong/unirest/RequestBodyEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,10 @@ public interface RequestBodyEntity extends HttpRequest<RequestBodyEntity>, Body
default RequestBodyEntity noCharset() {
return charset(null);
}

/**
* @param type The content mime type
* @return this request builder
*/
RequestBodyEntity contentType(String type);
}
20 changes: 20 additions & 0 deletions unirest/src/test/java/BehaviorTests/UniBodyPostingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,26 @@

class UniBodyPostingTest extends BddTest {

@Test
void hasShortCutForContentHeader() {
Unirest.post(MockServer.POST)
.contentType("plain/text")
.body("Hi")
.asObject(RequestCapture.class)
.getBody()
.assertContentType("plain/text");
}

@Test
void contentTypeAfterTheBody() {
Unirest.post(MockServer.POST)
.body("Hi")
.contentType("plain/text")
.asObject(RequestCapture.class)
.getBody()
.assertContentType("plain/text");
}

@Test
void testDefaults_String(){
Unirest.post(MockServer.POST)
Expand Down

0 comments on commit 998ad0b

Please sign in to comment.