-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Custom Header Sender so users can send any special headers they…
… need to.
- Loading branch information
Showing
4 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
#!/usr/bin/make -f | ||
|
||
SOURCE_VERSION := 3.2 | ||
SOURCE_VERSION := 3.3 | ||
|
||
clean: | ||
mvn clean | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/com/smartystreets/api/CustomHeaderSender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.smartystreets.api; | ||
|
||
import com.smartystreets.api.exceptions.SmartyException; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
public class CustomHeaderSender implements Sender { | ||
private Map<String, String> headers; | ||
private Sender inner; | ||
|
||
public CustomHeaderSender(Map<String, String> headers, Sender inner){ | ||
this.headers = headers; | ||
this.inner = inner; | ||
} | ||
|
||
@Override | ||
public Response send(Request request) throws SmartyException, IOException { | ||
Map<String, String> requestHeaders = request.getHeaders(); | ||
|
||
for (Map.Entry entry : this.headers.entrySet()) { | ||
request.putHeader((String)entry.getKey(), (String)entry.getValue()); | ||
} | ||
|
||
return this.inner.send(request); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/test/java/com/smartystreets/api/CustomHeaderSenderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.smartystreets.api; | ||
|
||
import com.smartystreets.api.exceptions.SmartyException; | ||
import com.smartystreets.api.mocks.RequestCapturingSender; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class CustomHeaderSenderTest { | ||
|
||
@Test | ||
public void testAllCustomHeadersAreAddedToTheRequest() throws IOException, SmartyException { | ||
HashMap<String, String> headers = new HashMap<>(); | ||
headers.put("A", "1"); | ||
headers.put("B", "2"); | ||
RequestCapturingSender inner = new RequestCapturingSender(); | ||
CustomHeaderSender sender = new CustomHeaderSender(headers, inner); | ||
Request request = new Request(); | ||
|
||
sender.send(request); | ||
|
||
Map<String, String> requestHeaders = inner.getRequest().getHeaders(); | ||
assertNotNull("There should be headers here.", requestHeaders); | ||
assertEquals(headers.get("A"), inner.getRequest().getHeaders().get("A")); | ||
|
||
} | ||
} |