Skip to content

Commit

Permalink
add some shortcuts for using ContentType with accepts and contentType…
Browse files Browse the repository at this point in the history
… methods
  • Loading branch information
ryber committed Apr 3, 2024
1 parent 7375923 commit b335b7a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
11 changes: 11 additions & 0 deletions unirest-bdd-tests/src/test/java/BehaviorTests/HeaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,17 @@ public void onRequest(HttpRequest<?> request, Config config) {
assertContains(headers, "beatles", "ringo");
}

@Test
void passInMediaTypeAsAcceptsAndContentType() {
Unirest.post(MockServer.POST)
.accept(ContentType.IMAGE_JPEG)
.contentType(ContentType.APPLICATION_JSON)
.asObject(RequestCapture.class)
.getBody()
.assertAccepts(ContentType.IMAGE_JPEG)
.assertContentType(ContentType.APPLICATION_JSON);
}

private void assertContains(Collection<Header> headers, String key, String value) {
assertTrue(headers.stream().anyMatch(h -> key.equalsIgnoreCase(key) && value.equalsIgnoreCase(value)),
"Missing header " + key + " with value " + value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,10 @@ public void setStatus(int i) {
this.status = i;
}

public RequestCapture assertContentType(ContentType content) {
return assertContentType(content.getMimeType());
}

public RequestCapture assertContentType(String content) {
return assertHeader("Content-Type", content);
}
Expand Down Expand Up @@ -329,6 +333,10 @@ public RequestCapture assertBodyPart(String name, Consumer<MultiPart> validator)
return this;
}

public RequestCapture assertAccepts(ContentType type) {
return assertHeader("Accept", type.toString());
}

public static class MultiPart {
public ListMultimap<String, String> headers = LinkedListMultimap.create();
public String content;
Expand Down
4 changes: 4 additions & 0 deletions unirest/src/main/java/kong/unirest/core/ContentType.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,8 @@ public ContentType withCharset(Charset charset) {
public boolean isBinary() {
return isBinary;
}

public Charset getCharset() {
return encoding;
}
}
13 changes: 12 additions & 1 deletion unirest/src/main/java/kong/unirest/core/HttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.time.Instant;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
Expand Down Expand Up @@ -75,7 +76,17 @@ public interface HttpRequest<R extends HttpRequest> {
R basicAuth(String username, String password);

/**
* The Accept header to send (e.g. application/json
* The Accept header to send (e.g. application/json)
* @param value a valid mime type for the Accept header
* @return this request builder
*/
default R accept(ContentType value){
Objects.requireNonNull(value);
return accept(value.toString());
}

/**
* The Accept header to send (e.g. application/json)
* @param value a valid mime type for the Accept header
* @return this request builder
*/
Expand Down
10 changes: 10 additions & 0 deletions unirest/src/main/java/kong/unirest/core/HttpRequestWithBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;

/**
* A request Builder for POST and PUT operations with a body.
Expand Down Expand Up @@ -183,6 +184,15 @@ default HttpRequestWithBody noCharset() {
*/
Charset getCharset();

/**
* @param type The content mime type
* @return this request builder
*/
default HttpRequestWithBody contentType(ContentType type){
Objects.requireNonNull(type);
return contentType(type.toString()).charset(type.getCharset());
}

/**
* @param type The content mime type
* @return this request builder
Expand Down

0 comments on commit b335b7a

Please sign in to comment.