-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ensure truncation body filter is always last one (#1608)
* ensure truncation body filter is always last one * reuse the existing truncation test * use a new list to add the truncating body filter
- Loading branch information
Showing
7 changed files
with
96 additions
and
87 deletions.
There are no files selected for viewing
23 changes: 0 additions & 23 deletions
23
logbook-core/src/main/java/org/zalando/logbook/core/TruncatingBodyFilter.java
This file was deleted.
Oops, something went wrong.
37 changes: 0 additions & 37 deletions
37
logbook-core/src/test/java/org/zalando/logbook/core/TruncatingBodyFilterTest.java
This file was deleted.
Oops, something went wrong.
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
74 changes: 62 additions & 12 deletions
74
...t-autoconfigure/src/test/java/org/zalando/logbook/autoconfigure/WriteBodyMaxSizeTest.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 |
---|---|---|
@@ -1,32 +1,82 @@ | ||
package org.zalando.logbook.autoconfigure; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.ArgumentCaptor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.zalando.logbook.BodyFilter; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.MediaType; | ||
import org.zalando.logbook.HttpLogWriter; | ||
import org.zalando.logbook.HttpRequest; | ||
import org.zalando.logbook.Logbook; | ||
import org.zalando.logbook.Precorrelation; | ||
import org.zalando.logbook.test.MockHttpRequest; | ||
|
||
import java.io.IOException; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.doReturn; | ||
import static org.mockito.Mockito.verify; | ||
|
||
@LogbookTest(properties = "logbook.write.max-body-size = 20") | ||
class WriteBodyMaxSizeTest { | ||
|
||
@Autowired | ||
@Qualifier("truncatingBodyFilter") | ||
private BodyFilter bodyFilter; | ||
private Logbook logbook; | ||
|
||
@MockBean | ||
private HttpLogWriter writer; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
doReturn(true).when(writer).isActive(); | ||
} | ||
|
||
@Test | ||
void shouldUseBodyMaxSizeFilter() { | ||
final String body = "{\"foo\":\"someLongMessage\"}"; | ||
final String filtered = bodyFilter.filter("application/json", body); | ||
void shouldUseBodyMaxSizeFilter() throws IOException { | ||
final HttpRequest request = MockHttpRequest.create() | ||
.withBodyAsString("{\"foo\":\"someLongMessage\"}") | ||
.withContentType(MediaType.APPLICATION_JSON_VALUE); | ||
|
||
logbook.process(request).write(); | ||
|
||
final ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); | ||
verify(writer).write(any(Precorrelation.class), captor.capture()); | ||
final String message = captor.getValue(); | ||
|
||
String body = extractBody(message); | ||
|
||
assertThat(filtered).isEqualTo("{\"foo\":\"someLongMess..."); | ||
assertThat(body).isEqualTo("{\"foo\":\"someLongMess...}"); | ||
} | ||
|
||
@Test | ||
void shouldUseBodyMaxSizeOverDefaultFilter() { | ||
final String body = "{\"open_id\":\"someLongSecret\",\"foo\":\"bar\"}"; | ||
final String filtered = bodyFilter.filter("application/json", body); | ||
void shouldUseBodyMaxSizeOverDefaultFilter() throws IOException { | ||
final HttpRequest request = MockHttpRequest.create() | ||
.withBodyAsString("{\"open_id\":\"someLongSecret\",\"foo\":\"bar\"}") | ||
.withContentType(MediaType.APPLICATION_JSON_VALUE); | ||
|
||
assertThat(filtered).isEqualTo("{\"open_id\":\"someLong..."); | ||
logbook.process(request).write(); | ||
|
||
final ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); | ||
verify(writer).write(any(Precorrelation.class), captor.capture()); | ||
final String message = captor.getValue(); | ||
|
||
String body = extractBody(message); | ||
|
||
assertThat(body).isEqualTo("{\"open_id\":\"XXX\",\"fo...}"); | ||
} | ||
|
||
private static String extractBody(String message) { | ||
Pattern pattern = Pattern.compile(".*body\":(.*)"); | ||
Matcher matcher = pattern.matcher(message); | ||
String body = null; | ||
if (matcher.find()) { | ||
body = matcher.group(1); | ||
} | ||
return body; | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
logbook-spring-boot-autoconfigure/src/test/resources/application-truncation_order.yml
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,7 @@ | ||
logbook: | ||
obfuscate: | ||
json-body-fields: | ||
- first_name | ||
- last_name | ||
write: | ||
max-body-size: 20 |