Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Mar 6, 2016
2 parents d100b21 + c54de62 commit 6810446
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
21 changes: 16 additions & 5 deletions src/main/java/org/takes/facets/fork/FkContentType.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
import lombok.EqualsAndHashCode;
import org.takes.Request;
import org.takes.Response;
import org.takes.Take;
import org.takes.misc.Opt;
import org.takes.rq.RqHeaders;
import org.takes.tk.TkFixed;

/**
* Fork by Content-type accepted by "Content-Type" HTTP header.
Expand All @@ -40,7 +42,7 @@
* @since 1.0
* @see RsFork
*/
@EqualsAndHashCode(of = { "type", "origin" })
@EqualsAndHashCode(of = { "type", "take" })
public final class FkContentType implements Fork {

/**
Expand All @@ -49,25 +51,34 @@ public final class FkContentType implements Fork {
private final transient MediaTypes type;

/**
* Response to return.
* Take to handle the request and dynamically return the response.
*/
private final transient Response origin;
private final transient Take take;

/**
* Ctor.
* @param atype Accepted type
* @param response Response to return
*/
public FkContentType(final String atype, final Response response) {
this(atype, new TkFixed(response));
}

/**
* Ctor.
* @param atype Accepted type
* @param take Take to handle the request dynamically.
*/
public FkContentType(final String atype, final Take take) {
this.type = new MediaTypes(atype);
this.origin = response;
this.take = take;
}

@Override
public Opt<Response> route(final Request req) throws IOException {
final Opt<Response> resp;
if (FkContentType.getType(req).contains(this.type)) {
resp = new Opt.Single<Response>(this.origin);
resp = new Opt.Single<Response>(this.take.act(req));
} else {
resp = new Opt.Empty<Response>();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/takes/facets/fork/TkConsumes.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public TkConsumes(final Take take, final String type) {
public Response act(final Request req) throws IOException {
return new RsFork(
req,
new FkContentType(type, take.act(req))
new FkContentType(type, take)
);
}
}
Expand Down
33 changes: 27 additions & 6 deletions src/test/java/org/takes/facets/fork/FkContentTypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.takes.Request;
import org.takes.Response;
import org.takes.Take;
import org.takes.rq.RqFake;
import org.takes.rq.RqWithHeader;
import org.takes.rs.RsEmpty;
Expand Down Expand Up @@ -78,7 +81,8 @@ public void matchesWithAnyTypes() throws IOException {
public void matchesDifferentTypes() throws IOException {
MatcherAssert.assertThat(
new FkContentType(
"application/json charset=utf-8", new RsEmpty()
"application/json charset=utf-8",
FkContentTypeTest.emptyResponse()
).route(
new RqWithHeader(
new RqFake(),
Expand All @@ -98,7 +102,8 @@ public void matchesDifferentTypes() throws IOException {
public void matchesIdenticalTypes() throws IOException {
MatcherAssert.assertThat(
new FkContentType(
FkContentTypeTest.CTYPE, new RsEmpty()
FkContentTypeTest.CTYPE,
FkContentTypeTest.emptyResponse()
).route(
new RqWithHeader(
new RqFake(),
Expand All @@ -117,12 +122,14 @@ FkContentTypeTest.CTYPE, new RsEmpty()
@Test
public void matchesEmptyType() throws IOException {
MatcherAssert.assertThat(
new FkContentType("*/*", new RsEmpty()).route(
new FkContentType(
"*/*",
FkContentTypeTest.emptyResponse()
).route(
new RqWithHeader(
new RqFake(), FkContentTypeTest.CONTENT_TYPE, ""
)
).has(),
Matchers.is(true)
).has(), Matchers.is(true)
);
}

Expand All @@ -134,7 +141,8 @@ public void matchesEmptyType() throws IOException {
public void matchesDifferentEncodingsTypes() throws IOException {
MatcherAssert.assertThat(
new FkContentType(
FkContentTypeTest.CTYPE, new RsEmpty()
FkContentTypeTest.CTYPE,
FkContentTypeTest.emptyResponse()
).route(
new RqWithHeader(
new RqFake(),
Expand All @@ -156,4 +164,17 @@ public void equalsAndHashCodeEqualTest() throws Exception {
.suppress(Warning.TRANSIENT_FIELDS)
.verify();
}

/**
* Create a Take instance with empty response.
* @return Take
*/
private static Take emptyResponse() {
return new Take() {
@Override
public Response act(final Request req) throws IOException {
return new RsEmpty();
}
};
}
}

0 comments on commit 6810446

Please sign in to comment.