Skip to content

Commit

Permalink
Ability to replace a map of headers Fixes #419
Browse files Browse the repository at this point in the history
  • Loading branch information
haroon-sheikh committed Nov 16, 2021
1 parent 472f7d0 commit 863e1d1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 0 deletions.
6 changes: 6 additions & 0 deletions unirest/src/main/java/kong/unirest/BaseRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ public R headers(Map<String, String> headerMap) {
return (R) this;
}

@Override
public R headersReplace(Map<String, String> headerMap) {
this.headers.replace(headerMap);
return (R) this;
}

@Override
public R responseEncoding(String encoding) {
this.responseEncoding = encoding;
Expand Down
1 change: 1 addition & 0 deletions unirest/src/main/java/kong/unirest/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,7 @@ public Config retryAfter(boolean value) {
* Default is false
*
* @param value a bool is its true or not.
* @param maxRetryAttempts max retry attempts
* @return this config object
*/
public Config retryAfter(boolean value, int maxRetryAttempts) {
Expand Down
10 changes: 10 additions & 0 deletions unirest/src/main/java/kong/unirest/Headers.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,16 @@ public void add(Map<String, String> headerMap) {
}
}

/**
* Replace all headers from a given map.
* @param headerMap the map of headers
*/
public void replace(Map<String, String> headerMap) {
if (headerMap != null) {
headerMap.forEach(this::replace);
}
}

static class Entry implements Header {

private final String name;
Expand Down
7 changes: 7 additions & 0 deletions unirest/src/main/java/kong/unirest/HttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ public interface HttpRequest<R extends HttpRequest> {
*/
R headers(Map<String, String> headerMap);

/**
* Replace headers as a map
* @param headerMap a map of headers
* @return this request builder
*/
R headersReplace(Map<String, String> headerMap);

/**
* Add a simple cookie header
* @param name the name of the cookie
Expand Down
13 changes: 13 additions & 0 deletions unirest/src/test/java/BehaviorTests/HeaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.junit.jupiter.api.Test;

import java.util.Base64;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static BehaviorTests.TestUtils.mapOf;
Expand Down Expand Up @@ -346,4 +347,16 @@ void nullTests() {
.getBody()
.assertHeader("foo","bar");
}

@Test
void replaceHeadersTests() {
Unirest.config().addDefaultHeader("foo", "default");
Unirest.get(MockServer.GET)
.header("foo","bar")
.headersReplace(mapOf("foo", "replace", "two", "bar"))
.asObject(RequestCapture.class)
.getBody()
.assertHeader("foo","replace")
.assertHeader("two","bar");
}
}

0 comments on commit 863e1d1

Please sign in to comment.