Skip to content

Commit

Permalink
yegor256#865 More fixes as requested by reviewer
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriciofx committed Jan 24, 2019
1 parent d847792 commit ad2d0c7
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 37 deletions.
28 changes: 20 additions & 8 deletions src/test/java/org/takes/servlet/HttpServletRequestFakeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@

import java.util.Collections;
import java.util.NoSuchElementException;
import org.cactoos.list.ListOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.hamcrest.collection.IsIterableContainingInAnyOrder;
import org.hamcrest.core.IsCollectionContaining;
import org.hamcrest.core.IsEqual;
import org.junit.Rule;
import org.junit.jupiter.api.Test;
import org.junit.rules.ExpectedException;
import org.takes.rq.RqFake;
import org.takes.rq.RqMethod;
import org.takes.rq.RqWithHeaders;

/**
Expand All @@ -48,7 +51,7 @@ public final class HttpServletRequestFakeTest {
public final ExpectedException exception = ExpectedException.none();

@Test
public void headersNotFound() {
public void failsIfAHeaderIsNotFound() {
this.exception.expect(NoSuchElementException.class);
this.exception.expectMessage("Value of header foo not found");
new HttpServletRequestFake(
Expand All @@ -61,7 +64,7 @@ public void headersNotFound() {
}

@Test
public void headers() {
public void containsAHeaderAndItsValue() {
MatcherAssert.assertThat(
"Can't get the headers",
Collections.list(
Expand All @@ -73,12 +76,15 @@ public void headers() {
)
).getHeaders("testheader")
),
Matchers.hasItems("someValue")
new IsCollectionContaining<>(
new IsEqual<>("someValue")
)
);
}

@SuppressWarnings("unchecked")
@Test
public void headerNames() {
public void containsAllHeadersNames() {
MatcherAssert.assertThat(
"Can't get the header names",
Collections.list(
Expand All @@ -90,16 +96,22 @@ public void headerNames() {
)
).getHeaderNames()
),
Matchers.hasItems("host", "anyheader", "crazyheader")
new IsIterableContainingInAnyOrder<>(
new ListOf<>(
new IsEqual<>("host"),
new IsEqual<>("crazyheader"),
new IsEqual<>("anyheader")
)
)
);
}

@Test
public void method() {
public void defaultMethodIsGet() {
MatcherAssert.assertThat(
"Can't get the request method",
new HttpServletRequestFake(new RqFake()).getMethod(),
new IsEqual<>("GET")
new IsEqual<>(RqMethod.GET)
);
}
}
86 changes: 57 additions & 29 deletions src/test/java/org/takes/servlet/RqFromTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,32 @@
* @since 1.15
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public final class RqFromTest {

/**
* Takes default local address.
*/
private static final String LOCAL_ADDRESS =
"X-Takes-LocalAddress: 127.0.0.1";

/**
* Takes default remote address.
*/
private static final String REMOTE_ADDRESS =
"X-Takes-RemoteAddress: 127.0.0.1";

/**
* End Of Line for HTTP protocol.
*/
private static final String EOL = "\r\n";

/**
* Default GET method.
*/
private static final String GET_METHOD = "GET /";

@Test
public void method() throws IOException {
public void defaultMethodForAFakeResquestIsGet() throws IOException {
MatcherAssert.assertThat(
"Can't add a method to a servlet request",
new RqPrint(
Expand All @@ -53,21 +75,23 @@ public void method() throws IOException {
)
)
).printHead(),
new StringStartsWith("GET /")
new StringStartsWith(RqFromTest.GET_METHOD)
);
}

@Test
public void header() throws IOException {
public void containsMethodAndHeader() throws IOException {
final String method = "GET /a-test";
final String header = "foo: bar";
MatcherAssert.assertThat(
"Can't add a header to a servlet request",
new RqPrint(
new RqFrom(
new HttpServletRequestFake(
new RqFake(
new ListOf<>(
"GET /a-test",
"foo: bar"
method,
header
),
""
)
Expand All @@ -76,28 +100,30 @@ public void header() throws IOException {
).printHead(),
new StringStartsWith(
new JoinedText(
"\r\n",
"GET /a-test",
RqFromTest.EOL,
method,
"Host: localhost",
"foo: bar",
"X-Takes-LocalAddress: 127.0.0.1",
"X-Takes-RemoteAddress: 127.0.0.1"
header,
RqFromTest.LOCAL_ADDRESS,
RqFromTest.REMOTE_ADDRESS
).asString()
)
);
}

@Test
public void hostInHeader() throws IOException {
public void containsHostHeaderInHeader() throws IOException {
final String method = "GET /one-more-test";
final String header = "Host: www.thesite.com";
MatcherAssert.assertThat(
"Can't set a host in a servlet request",
new RqPrint(
new RqFrom(
new HttpServletRequestFake(
new RqFake(
new ListOf<>(
"GET /one-more-test",
"Host: www.thesite.com"
method,
header
),
""
)
Expand All @@ -106,27 +132,29 @@ public void hostInHeader() throws IOException {
).printHead(),
new StringStartsWith(
new JoinedText(
"\r\n",
"GET /one-more-test",
"host: www.thesite.com",
"X-Takes-LocalAddress: 127.0.0.1",
"X-Takes-RemoteAddress: 127.0.0.1"
RqFromTest.EOL,
method,
header,
RqFromTest.LOCAL_ADDRESS,
RqFromTest.REMOTE_ADDRESS
).asString()
)
);
}

@Test
public void hostAndPortInHeader() throws IOException {
public void containsHostAndPortInHeader() throws IOException {
final String method = "GET /b-test";
final String header = "Host: 192.168.0.1:12345";
MatcherAssert.assertThat(
"Can't set a host and port in a servlet request",
new RqPrint(
new RqFrom(
new HttpServletRequestFake(
new RqFake(
new ListOf<>(
"GET /b-test",
"Host: 192.168.0.1:12345"
method,
header
),
""
)
Expand All @@ -135,26 +163,26 @@ public void hostAndPortInHeader() throws IOException {
).printHead(),
new StringStartsWith(
new JoinedText(
"\r\n",
"GET /b-test",
"host: 192.168.0.1:12345",
"X-Takes-LocalAddress: 127.0.0.1",
"X-Takes-RemoteAddress: 127.0.0.1"
RqFromTest.EOL,
method,
header,
RqFromTest.LOCAL_ADDRESS,
RqFromTest.REMOTE_ADDRESS
).asString()
)
);
}

@Test
public void body() throws IOException {
public void containsContentInRequestBody() throws IOException {
final String content = "My name is neo!";
MatcherAssert.assertThat(
"Can't add a body to servlet request",
new RqPrint(
new RqFrom(
new HttpServletRequestFake(
new RqFake(
new ListOf<>("GET /"),
new ListOf<>(RqFromTest.EOL),
content
)
)
Expand Down

0 comments on commit ad2d0c7

Please sign in to comment.