-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from mrmike/support-header-modifiers
Add support header modifiers
- Loading branch information
Showing
16 changed files
with
280 additions
and
22 deletions.
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
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
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
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
21 changes: 21 additions & 0 deletions
21
ok2curl/src/main/java/com/moczul/ok2curl/modifier/HeaderModifier.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,21 @@ | ||
package com.moczul.ok2curl.modifier; | ||
|
||
import com.moczul.ok2curl.Header; | ||
|
||
/** | ||
* HeaderModifier allow for changing header name/value before creating curl log | ||
*/ | ||
public interface HeaderModifier { | ||
|
||
/** | ||
* @param header | ||
* @return true if header should be modified and false otherwise. | ||
*/ | ||
boolean matches(Header header); | ||
|
||
/** | ||
* @param header | ||
* @return modified header or null to omit header in curl log | ||
*/ | ||
Header modify(Header header); | ||
} |
2 changes: 2 additions & 0 deletions
2
ok2curl/src/test/java/com/moczul/ok2curl/CurlBuilderTest.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
83 changes: 83 additions & 0 deletions
83
ok2curl/src/test/java/com/moczul/ok2curl/util/HeaderModifierTest.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,83 @@ | ||
package com.moczul.ok2curl.util; | ||
|
||
import com.moczul.ok2curl.CurlBuilder; | ||
import com.moczul.ok2curl.Header; | ||
import com.moczul.ok2curl.modifier.HeaderModifier; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import okhttp3.Request; | ||
|
||
import static junit.framework.Assert.assertEquals; | ||
|
||
public class HeaderModifierTest { | ||
|
||
private final HeaderModifier cookieHeaderModifier = new HeaderModifier() { | ||
|
||
@Override | ||
public boolean matches(Header header) { | ||
return "Cookie".equals(header.name()); | ||
} | ||
|
||
@Override | ||
public Header modify(Header header) { | ||
return new Header(header.name(), "modifiedCookieValue"); | ||
} | ||
}; | ||
|
||
private final HeaderModifier nullHeaderModifier = new HeaderModifier() { | ||
|
||
@Override | ||
public boolean matches(Header header) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public Header modify(Header header) { | ||
return null; | ||
} | ||
}; | ||
|
||
@Test | ||
public void curlCommand_shouldContains_modifiedHeader() throws Exception { | ||
final Request request = new Request.Builder() | ||
.url("http://example.com/") | ||
.header("Cookie", "FIRST=foo") | ||
.build(); | ||
|
||
final List<HeaderModifier> modifiers = Collections.singletonList(cookieHeaderModifier); | ||
final String command = new CurlBuilder(request, -1L, modifiers).build(); | ||
|
||
assertEquals("curl -X GET -H \"Cookie:modifiedCookieValue\" http://example.com/", command); | ||
} | ||
|
||
@Test | ||
public void curlCommand_shouldNotBeModified_ifDoesNotContainMatchingHeader() throws Exception { | ||
final Request request = new Request.Builder() | ||
.url("http://example.com/") | ||
.header("Accept", "application/json") | ||
.build(); | ||
final List<HeaderModifier> modifiers = Collections.singletonList(cookieHeaderModifier); | ||
|
||
final String command = new CurlBuilder(request, -1L, modifiers).build(); | ||
|
||
assertEquals("curl -X GET -H \"Accept:application/json\" http://example.com/", command); | ||
} | ||
|
||
@Test | ||
public void curlCommand_shouldNotContainsAnyHeaders_forNullHeaderModifier() throws Exception { | ||
final Request request = new Request.Builder() | ||
.url("http://example.com/") | ||
.header("Cookie", "FIRST=foo") | ||
.header("Accept", "application/json") | ||
.build(); | ||
|
||
final List<HeaderModifier> modifiers = Collections.singletonList(nullHeaderModifier); | ||
final String command = new CurlBuilder(request, -1L, modifiers).build(); | ||
|
||
assertEquals(command, "curl -X GET http://example.com/", command); | ||
} | ||
} |
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
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
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
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
11 changes: 11 additions & 0 deletions
11
sample/src/main/java/com/moczul/sample/modifier/Base64Decoder.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,11 @@ | ||
package com.moczul.sample.modifier; | ||
|
||
import android.util.Base64; | ||
|
||
public class Base64Decoder { | ||
|
||
public String decode(String value) { | ||
final byte[] decodedBytes = Base64.decode(value.getBytes(), Base64.DEFAULT); | ||
return new String(decodedBytes); | ||
} | ||
} |
Oops, something went wrong.