Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing a broken build #1321

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 72 additions & 1 deletion src/test/java/org/takes/tk/TkProxyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,22 @@
*/
package org.takes.tk;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Map;
import org.cactoos.iterable.IterableOf;
import org.cactoos.map.MapEntry;
import org.cactoos.map.MapOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.llorllale.cactoos.matchers.HasString;
import org.takes.Request;
import org.takes.Take;
import org.takes.facets.fork.FkMethods;
import org.takes.facets.fork.TkFork;
Expand All @@ -56,7 +63,26 @@ final class TkProxyTest {
* A {@code Take} implementation that returns the content of the request
* as body of its response.
*/
private static final Take ECHO = req -> new RsText(new RqPrint(req).print());
private static final Take ECHO =
req -> new RsText(
new RqPrint(
TkProxyTest.createEchoRequest(req)
).print()
);

/**
* Collection of HTTP method matches for which nobody is returned.
*/
private static final Map<String, Factory> NOBODIES =
new MapOf<>(
new IterableOf<>(
new MapEntry<>(RqMethod.GET, RqWithoutBody::new),
new MapEntry<>(RqMethod.HEAD, RqWithoutBody::new),
new MapEntry<>(RqMethod.DELETE, RqWithoutBody::new),
new MapEntry<>(RqMethod.OPTIONS, RqWithoutBody::new),
new MapEntry<>(RqMethod.TRACE, RqWithoutBody::new)
)
);

/**
* Http methods for testing.
Expand Down Expand Up @@ -249,4 +275,49 @@ void addsAllInitialHeaders() throws Exception {
)
);
}

private static Request createEchoRequest(final Request req) throws IOException {
final String method = new RqMethod.Base(req).method();
return NOBODIES.getOrDefault(method, rq -> req).create(req);
}

/**
* Local interface for creating a request with an empty body.
*
* @since 1.24.4
*/
interface Factory {
Request create(Request req);
}

/**
* Wrapper for a request with an empty body.
*
* @since 1.24.4
*/
private static final class RqWithoutBody implements Request {

/**
* Original request.
*/
private final Request origin;

/**
* Ctor.
* @param req Original request.
*/
RqWithoutBody(final Request req) {
this.origin = req;
}

@Override
public Iterable<String> head() throws IOException {
return this.origin.head();
}

@Override
public InputStream body() {
return new ByteArrayInputStream(new byte[0]);
}
}
}
Loading