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

Commit

Permalink
add Request.withHeaders and .clearHeaders methods
Browse files Browse the repository at this point in the history
Fixes #11
  • Loading branch information
pettermahlen committed Dec 2, 2015
1 parent 06a1b8e commit ea9ebff
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 1 deletion.
17 changes: 17 additions & 0 deletions apollo-api/src/main/java/com/spotify/apollo/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,23 @@ default Optional<String> header(String name) {
*/
Request withHeader(String name, String value);

/**
* Creates a new {@link Request} based on this, but with additional headers. If the current
* request has a header whose key is also included in the {@code additionalHeaders} map,
* then the new request will have the header value defined in the map.
*
* @param additionalHeaders map of headers to add
* @return A request with the added headers
*/
Request withHeaders(Map<String, String> additionalHeaders);

/**
* Creates a new {@link Request} based on this, but with no header information.
*
* @return A request without headers
*/
Request clearHeaders();

/**
* Creates a new {@link Request} based on this, but with a different payload.
*
Expand Down
13 changes: 12 additions & 1 deletion apollo-api/src/main/java/com/spotify/apollo/RequestValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package com.spotify.apollo;

import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableMap;

import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -75,11 +76,21 @@ public Request withService(String service) {

@Override
public Request withHeader(String name, String value) {
return withHeaders(ImmutableMap.of(name, value));
}

@Override
public Request withHeaders(Map<String, String> additionalHeaders) {
Map<String, String> headers = new LinkedHashMap<>(headers());
headers.put(name, value);
headers.putAll(additionalHeaders);
return create(method(), uri(), parameters(), headers, service(), payload());
}

@Override
public Request clearHeaders() {
return create(method(), uri(), parameters(), emptyMap(), service(), payload());
}

@Override
public Request withPayload(ByteString payload) {
return create(method(), uri(), parameters(), headers(), service(), of(payload));
Expand Down
27 changes: 27 additions & 0 deletions apollo-api/src/test/java/com/spotify/apollo/RequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
package com.spotify.apollo;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

import org.junit.Test;

import java.util.Map;
import java.util.Optional;

import okio.ByteString;
Expand Down Expand Up @@ -112,4 +114,29 @@ public void shouldReturnPayload() throws Exception {
public void shouldAllowModifyingUri() throws Exception {
assertThat(request("/foo").withUri("/fie").uri(), is("/fie"));
}

@Test
public void shouldMergeHeaders() throws Exception {
Map<String, String> newHeaders = ImmutableMap.of("newHeader", "value1", "newHeader2", "value2");

assertThat(requestWithHeader("/foo", "old", "value").withHeaders(newHeaders).headers(),
is(ImmutableMap.of("old", "value",
"newHeader", "value1",
"newHeader2", "value2")));
}

@Test
public void shouldReplaceExistingHeader() throws Exception {
Map<String, String> newHeaders = ImmutableMap.of("newHeader", "value1", "old", "value2");

assertThat(requestWithHeader("/foo", "old", "value").withHeaders(newHeaders).headers(),
is(ImmutableMap.of("old", "value2",
"newHeader", "value1")));
}

@Test
public void shouldClearHeaders() throws Exception {
assertThat(requestWithHeader("/foo", "old", "value").clearHeaders().headers(),
is(ImmutableMap.of()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import com.spotify.apollo.Request;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -58,6 +59,18 @@ public Request withPayload(ByteString payload) {
return create(method(), uri(), of(payload), service(), parameters(), headers());
}

@Override
public Request withHeaders(Map<String, String> additionalHeaders) {
Map<String, String> headers = new LinkedHashMap<>(headers());
headers.putAll(additionalHeaders);
return create(method(), uri(), payload(), service(), parameters(), headers);
}

@Override
public Request clearHeaders() {
return create(method(), uri(), payload(), service(), parameters(), ImmutableMap.of());
}

public static Request create(
String method,
String uri,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* -\-\-
* Spotify Apollo Jetty HTTP Server Module
* --
* Copyright (C) 2013 - 2015 Spotify AB
* --
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* -/-/-
*/
package com.spotify.apollo.http.server;

import com.google.common.collect.ImmutableMap;

import com.spotify.apollo.Request;

import org.junit.Test;

import java.util.Map;
import java.util.Optional;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class HttpRequestTest {

private Request requestWithHeader(String uri, String header, String value) {
return HttpRequest.create("GET", uri, Optional.empty(), Optional.empty(),
ImmutableMap.of(), ImmutableMap.of(header, value));
}

@Test
public void shouldMergeHeaders() throws Exception {
Map<String, String> newHeaders = ImmutableMap.of("newHeader", "value1", "newHeader2", "value2");

assertThat(requestWithHeader("/foo", "old", "value").withHeaders(newHeaders).headers(),
is(ImmutableMap.of("old", "value",
"newHeader", "value1",
"newHeader2", "value2")));
}

@Test
public void shouldReplaceExistingHeader() throws Exception {
Map<String, String> newHeaders = ImmutableMap.of("newHeader", "value1", "old", "value2");

assertThat(requestWithHeader("/foo", "old", "value").withHeaders(newHeaders).headers(),
is(ImmutableMap.of("old", "value2",
"newHeader", "value1")));
}

@Test
public void shouldClearHeaders() throws Exception {
assertThat(requestWithHeader("/foo", "old", "value").clearHeaders().headers(),
is(ImmutableMap.of()));
}
}

0 comments on commit ea9ebff

Please sign in to comment.