-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
254 additions
and
28 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
9 changes: 9 additions & 0 deletions
9
modules/java/src/main/java/be/wegenenverkeer/rxhttp/ClientRequestLogFormatter.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,9 @@ | ||
package be.wegenenverkeer.rxhttp; | ||
|
||
/** | ||
* Created by Karel Maesen, Geovise BVBA on 2019-06-25. | ||
*/ | ||
public interface ClientRequestLogFormatter { | ||
|
||
String toLogMessage(ClientRequest request); | ||
} |
59 changes: 59 additions & 0 deletions
59
modules/java/src/main/java/be/wegenenverkeer/rxhttp/DefaultClientRequestLogFormatter.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,59 @@ | ||
package be.wegenenverkeer.rxhttp; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Created by Karel Maesen, Geovise BVBA on 2019-06-25. | ||
*/ | ||
public class DefaultClientRequestLogFormatter implements ClientRequestLogFormatter { | ||
|
||
private final List<String> headersToLog; | ||
private final List<String> formParamNamesToLog; | ||
|
||
@SuppressWarnings("unchecked") | ||
public DefaultClientRequestLogFormatter() { | ||
this(Collections.EMPTY_LIST, Collections.EMPTY_LIST); | ||
} | ||
|
||
public DefaultClientRequestLogFormatter(List<String> headersToLog, List<String> formParamNamesToLog) { | ||
this.headersToLog = headersToLog.stream().map( String::toLowerCase).collect(Collectors.toList()); | ||
this.formParamNamesToLog = formParamNamesToLog.stream().map(String::toLowerCase).collect(Collectors.toList()); | ||
} | ||
|
||
@Override | ||
public String toLogMessage(ClientRequest request) { | ||
StringBuilder sb = new StringBuilder(request.getMethod()) | ||
.append(" ") | ||
.append(request.getUrl()); | ||
|
||
List<String> headerLogs = headersToLog.stream().map(header -> | ||
header + ":" + String.join("; ", request.unwrap().getHeaders().getAll(header)) | ||
).collect(Collectors.toList()); | ||
|
||
if (!headerLogs.isEmpty()) { | ||
sb.append("\theaders:"); | ||
headerLogs.forEach(hl -> { | ||
sb.append("\t").append(hl); | ||
}); | ||
} | ||
|
||
List<String> paramList = request.unwrap().getFormParams().stream() | ||
.filter(fparam -> formParamNamesToLog.contains(fparam.getName().toLowerCase())) | ||
.map(fparam -> fparam.getName() + ":" + fparam.getValue()) | ||
.collect(Collectors.toList()); | ||
|
||
if (!paramList.isEmpty()) { | ||
sb.append("\tformParams:"); | ||
paramList.forEach(pl -> { | ||
sb.append("\t").append(pl); | ||
}); | ||
} | ||
|
||
return sb.toString(); | ||
} | ||
|
||
} | ||
|
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
8 changes: 6 additions & 2 deletions
8
...les/java/src/test/java/be/wegenenverkeer/designtests/RxHttpClientTestChunkedResponse.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
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
105 changes: 105 additions & 0 deletions
105
modules/java/src/test/java/be/wegenenverkeer/rxhttp/TestLoggerConf.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,105 @@ | ||
package be.wegenenverkeer.rxhttp; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* Created by Karel Maesen, Geovise BVBA on 2019-06-25. | ||
*/ | ||
public class TestLoggerConf { | ||
|
||
private RxHttpClient client = null; | ||
|
||
@Test | ||
public void testLoggerConfDefault(){ | ||
client = new RxHttpClient.Builder().setBaseUrl("http://foo.com").build(); | ||
try { | ||
ClientRequest request = client.requestBuilder().setMethod("GET").setUrlRelativetoBase("/test").build(); | ||
assertEquals("GET http://foo.com/test", client.toLogMessage(request)); | ||
} finally { | ||
client.close(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testLoggerConfWithHeader(){ | ||
client = new RxHttpClient.Builder() | ||
.setBaseUrl("http://foo.com") | ||
.logHeaders(Arrays.asList("Test-Header")) | ||
.build(); | ||
try { | ||
ClientRequest request = client | ||
.requestBuilder() | ||
.setMethod("GET") | ||
.setUrlRelativetoBase("/test") | ||
.setHeader("Test-Header", "testvalue") | ||
.build(); | ||
assertEquals("GET http://foo.com/test\theaders:\ttest-header:testvalue", client.toLogMessage(request)); | ||
} finally { | ||
client.close(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testLoggerConfWithHeaderIsCaseInsentivie(){ | ||
client = new RxHttpClient.Builder() | ||
.setBaseUrl("http://foo.com") | ||
.logHeaders(Arrays.asList("Test-Header")) | ||
.build(); | ||
try { | ||
ClientRequest request = client | ||
.requestBuilder() | ||
.setMethod("GET") | ||
.setUrlRelativetoBase("/test") | ||
.setHeader("test-Header", "testvalue") | ||
.build(); | ||
assertEquals("GET http://foo.com/test\theaders:\ttest-header:testvalue", client.toLogMessage(request)); | ||
} finally { | ||
client.close(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testLoggerConfWithFParamIsCaseInsensitive(){ | ||
client = new RxHttpClient.Builder() | ||
.setBaseUrl("http://foo.com") | ||
.logFormParams(Arrays.asList("TestParam")) | ||
.build(); | ||
try { | ||
ClientRequest request = client | ||
.requestBuilder() | ||
.setMethod("GET") | ||
.setUrlRelativetoBase("/test") | ||
.addFormParam("testparam", "testparamValue") | ||
.build(); | ||
assertEquals("GET http://foo.com/test\tformParams:\ttestparam:testparamValue", client.toLogMessage(request)); | ||
} finally { | ||
client.close(); | ||
} | ||
|
||
} | ||
|
||
@Test | ||
public void testLoggerConfWithFormParam(){ | ||
client = new RxHttpClient.Builder() | ||
.setBaseUrl("http://foo.com") | ||
.logFormParams(Arrays.asList("TestParam")) | ||
.build(); | ||
try { | ||
ClientRequest request = client | ||
.requestBuilder() | ||
.setMethod("GET") | ||
.setUrlRelativetoBase("/test") | ||
.addFormParam("TestParam", "testparamValue") | ||
.build(); | ||
assertEquals("GET http://foo.com/test\tformParams:\tTestParam:testparamValue", client.toLogMessage(request)); | ||
} finally { | ||
client.close(); | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.