Skip to content
This repository has been archived by the owner on Sep 28, 2021. It is now read-only.

Commit

Permalink
Stop using deprecated method headers()
Browse files Browse the repository at this point in the history
  • Loading branch information
martina-if committed Apr 20, 2017
1 parent 1681da2 commit 1dbf28c
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.AbstractMap.SimpleEntry;
import java.util.Collections;
import java.util.Optional;
import java.util.function.BiConsumer;
Expand Down Expand Up @@ -109,7 +110,8 @@ public void testWrongMethod() throws Exception {
verify(ongoingRequest).reply(responseArgumentCaptor.capture());
Response<ByteString> reply = responseArgumentCaptor.getValue();
assertEquals(reply.status(), Status.METHOD_NOT_ALLOWED);
assertEquals(reply.headers(), Collections.singletonMap("Allow", "OPTIONS, POST"));
assertEquals(reply.headerEntries(),
Collections.singletonList(new SimpleEntry<>("Allow", "OPTIONS, POST")));
}

@Test
Expand All @@ -124,7 +126,8 @@ public void testWithMethodOptions() throws Exception {
verify(ongoingRequest).reply(responseArgumentCaptor.capture());
Response<ByteString> response = responseArgumentCaptor.getValue();
assertThat(response.status(), is(Status.NO_CONTENT));
assertThat(response.headers(), is(Collections.singletonMap("Allow", "OPTIONS, POST")));
assertThat(response.headerEntries(),
is(Collections.singletonList(new SimpleEntry<>("Allow", "OPTIONS, POST"))));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import static com.spotify.apollo.Status.NO_CONTENT;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.Matchers.equalToIgnoringWhiteSpace;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
Expand Down Expand Up @@ -168,8 +167,8 @@ class TestData {
public void httpShouldSetContentLengthFor200() throws Exception {
future.complete(Response.of(Status.OK, ByteString.of((byte) 14, (byte) 19)));

String header = getResult(Middlewares.httpPayloadSemantics(delegate)).headers().get(
"Content-Length");
String header = getResult(Middlewares.httpPayloadSemantics(delegate)).header(
"Content-Length").get();
assertThat(Integer.parseInt(header), equalTo(2));
}

Expand All @@ -192,9 +191,9 @@ public void httpShouldNotSetContentLengthOrAppendPayloadForInvalidStatusCodes()
future.complete(Response.of(status, ByteString.of((byte) 14, (byte) 19)));

Response<ByteString> result = getResult(Middlewares.httpPayloadSemantics(delegate));
String header = result.headers().get("Content-Length");
Optional<String> header = result.header("Content-Length");

assertThat("no content-length for " + status, header, is(nullValue()));
assertThat("no content-length for " + status, header, is(Optional.empty()));
assertThat("no payload for " + status, result.payload(), is(Optional.<ByteString>empty()));
}
}
Expand All @@ -204,8 +203,8 @@ public void httpShouldSetContentLengthForHeadAnd200() throws Exception {
when(request.method()).thenReturn("HEAD");
future.complete(Response.of(Status.OK, ByteString.of((byte) 14, (byte) 19)));

String header = getResult(Middlewares.httpPayloadSemantics(delegate)).headers().get(
"Content-Length");
String header = getResult(Middlewares.httpPayloadSemantics(delegate)).header(
"Content-Length").get();
assertThat(Integer.parseInt(header), equalTo(2));
}

Expand All @@ -224,8 +223,7 @@ public void contentTypeShouldAddToNonResponse() throws Exception {

String contentType = getResult(Middlewares.replyContentType("text/plain")
.apply(serializationDelegate()))
.headers()
.get("Content-Type");
.header("Content-Type").get();

assertThat(contentType, equalTo("text/plain"));
}
Expand All @@ -236,8 +234,7 @@ public void contentTypeShouldAddToResponse() throws Exception {

String contentType =
getResult(Middlewares.replyContentType("text/plain").apply(serializationDelegate()))
.headers()
.get("Content-Type");
.header("Content-Type").get();

assertThat(contentType, equalTo("text/plain"));
}
Expand Down Expand Up @@ -277,9 +274,9 @@ public void serializerShouldCopyHeadersFromResponses() throws Exception {

serializationFuture.complete(Response.forPayload(response).withHeader("X-Foo", "Fie"));

assertThat(getResult(Middlewares.serialize(serializer).apply(serializationDelegate)).headers()
.get("X-Foo"),
equalTo("Fie"));
Optional<String> header = getResult(Middlewares.serialize(serializer).apply(serializationDelegate))
.header("X-Foo");
assertThat(header.get(), equalTo("Fie"));
}

@Test
Expand All @@ -306,7 +303,7 @@ public void serializerShouldSetContentTypeIfPresent() throws Exception {
serializationFuture.complete(response);

String contentType = getResult(Middlewares.serialize(serializer).apply(serializationDelegate))
.headers().get("Content-Type");
.header("Content-Type").get();

assertThat(contentType, equalTo("coool stuff"));
}
Expand All @@ -320,10 +317,10 @@ public void serializerShouldNotSetContentTypeIfAbsent() throws Exception {

serializationFuture.complete(response);

String contentType = getResult(Middlewares.serialize(serializer).apply(serializationDelegate))
.headers().get("Content-Type");
Optional<String> contentType = getResult(Middlewares.serialize(serializer).apply(serializationDelegate))
.header("Content-Type");

assertThat(contentType, is(nullValue()));
assertThat(contentType, is(Optional.empty()));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void testException() throws Exception {

private void checkContentTypeAndBody(final Response<ByteString> response) {
assertEquals("<html>yo</html>\n", response.payload().get().utf8());
assertEquals("text/html; charset=UTF8", response.headers().get("Content-Type"));
assertEquals("text/html; charset=UTF8", response.header("Content-Type").get());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@

import org.junit.Test;

import okio.ByteString;

import java.util.Optional;

import okio.ByteString;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

Expand All @@ -46,7 +46,7 @@ private static void checkPayloadAndContentType(Response<ByteString> response) {
}

private static void checkContentType(Response<ByteString> response) {
assertThat(response.headers().get("Content-Type"), equalTo("application/json; charset=UTF8"));
assertThat(response.header("Content-Type").get(), equalTo("application/json; charset=UTF8"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void the_response_code_is(int statusCode) throws Throwable {
public void the_response_has_a_header_with_value(String headerName, String expectedValue) throws Throwable {
Response<ByteString> response = getResponseFuture();

assertThat(response.headers().get(headerName), equalTo(expectedValue));
assertThat(response.header(headerName).get(), equalTo(expectedValue));
}

@And("^the reason phrase is \"([^\"]*)\"$")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static <T> Matcher<Response<T>> hasNoHeaders() {
return new TypeSafeMatcher<Response<T>>() {
@Override
protected boolean matchesSafely(Response<T> item) {
return item.headers().isEmpty();
return !item.headerEntries().iterator().hasNext();
}

@Override
Expand All @@ -56,7 +56,7 @@ public void describeTo(Description description) {

@Override
protected void describeMismatchSafely(Response<T> item, Description mismatchDescription) {
mismatchDescription.appendText("it contained headers ").appendValue(item.headers());
mismatchDescription.appendText("it contained headers ").appendValue(item.headerEntries());
}
};
}
Expand All @@ -74,7 +74,7 @@ public static <T> Matcher<Response<T>> hasHeader(String header, Matcher<String>

@Override
protected String featureValueOf(Response<T> actual) {
return actual.headers().get(header);
return actual.header(header).orElse(null);
}
};
}
Expand All @@ -88,7 +88,7 @@ public static <T> Matcher<Response<T>> doesNotHaveHeader(String header) {
return new TypeSafeMatcher<Response<T>>() {
@Override
protected boolean matchesSafely(Response<T> item) {
return item.headers().get(header) == null;
return !item.header(header).isPresent();
}

@Override
Expand All @@ -99,7 +99,7 @@ public void describeTo(Description description) {
@Override
protected void describeMismatchSafely(Response<T> item, Description mismatchDescription) {
mismatchDescription.appendText("it contained the header ");
mismatchDescription.appendValueList("{", ":", "}", header, item.headers().get(header));
mismatchDescription.appendValueList("{", ":", "}", header, item.header(header));
}

};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,11 @@ public void shouldDisallowChangingDelayForResponseSource() throws Exception {
@Test
public void shouldSupportHeadersForResponses() throws Exception {
stubClient.respond(Response.<ByteString>ok()
.withHeader("foo", "bar"))
.withHeader("foo", "bar"))
.to("http://ping");

Response reply = getResponse("http://ping").toCompletableFuture().get();
assertThat(reply.headers().get("foo"), equalTo("bar"));
assertThat(reply.header("foo").get(), equalTo("bar"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ private void sendReply(Response<ByteString> response) {
final StatusType status = response.status();
httpResponse.setStatus(status.code(), status.reasonPhrase());

response.headers().forEach(httpResponse::addHeader);
response.headerEntries().forEach(entry ->
httpResponse.addHeader(entry.getKey(), entry.getValue()));

response.payload().ifPresent(payload -> {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public CompletionStage<com.spotify.apollo.Response<ByteString>> send(
});

Headers.Builder headersBuilder = new Headers.Builder();
apolloRequest.headers().forEach((k, v) -> headersBuilder.add(k, v));
apolloRequest.headerEntries().forEach((e) -> headersBuilder.add(e.getKey(), e.getValue()));

apolloIncomingRequest
.flatMap(req -> req.header(AUTHORIZATION_HEADER))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,17 @@

import java.net.SocketTimeoutException;
import java.time.Duration;
import java.util.AbstractMap.SimpleEntry;
import java.util.Optional;

import okio.ByteString;

import static java.lang.String.format;
import static java.util.Optional.empty;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.mockserver.model.HttpCallback.callback;
Expand Down Expand Up @@ -104,7 +105,7 @@ public void testSendWithCustomHeader() throws Exception {
.toCompletableFuture().get();

assertThat(response.status(), withCode(200));
assertThat(response.headers().get("x-got-the-special-header"), equalTo("yup"));
assertThat(response.header("x-got-the-special-header").get(), equalTo("yup"));
}

@Test
Expand Down Expand Up @@ -136,9 +137,9 @@ public void testSendWithBody() throws Exception {
.toCompletableFuture().get();

assertThat(response.status(), withCode(200));
assertThat(response.headers(), allOf(
hasEntry("Content-Type", "application/x-spotify-location"),
hasEntry("Vary", "Content-Type, Accept")
assertThat(response.headerEntries(), allOf(
hasItem(new SimpleEntry<>("Content-Type", "application/x-spotify-location")),
hasItem(new SimpleEntry<>("Vary", "Content-Type, Accept"))
));
assertThat(response.payload(), is(Optional.of(ByteString.encodeUtf8("world"))));
}
Expand Down Expand Up @@ -203,7 +204,7 @@ public void testAuthContextPropagation() throws Exception {
.send(request, Optional.of(originalRequest))
.toCompletableFuture().get();

assertThat(response.headers().get("x-auth-was-fine"), equalTo("yes"));
assertThat(response.header("x-auth-was-fine").get(), equalTo("yes"));
}

@Test
Expand Down

0 comments on commit 1dbf28c

Please sign in to comment.