Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

log request/response without body for POST (#1571) #1572

Merged
merged 4 commits into from
Jul 17, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
mocked unit test to fulfill coverage (#1571)
  • Loading branch information
Matthias Drews committed Jul 14, 2023
commit 3b9622d02cd4d94bffea3f7a95982a86c35eea55
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.zalando.logbook.spring.webflux;

import java.util.Collections;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
Expand All @@ -11,8 +12,14 @@
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpStatus;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
import org.springframework.mock.web.server.MockServerWebExchange;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import org.springframework.web.server.handler.DefaultWebFilterChain;
import org.zalando.logbook.Correlation;
import org.zalando.logbook.HttpLogWriter;
import org.zalando.logbook.Logbook;
Expand All @@ -23,10 +30,12 @@
import org.zalando.logbook.test.TestStrategy;

import java.io.IOException;
import reactor.core.publisher.Mono;

import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;
Expand All @@ -42,9 +51,9 @@ static class FilterConfiguration {
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
public Logbook logbook(HttpLogWriter writer) {
return Logbook.builder()
.strategy(new TestStrategy())
.sink(new DefaultSink(new DefaultHttpLogFormatter(), writer))
.build();
.strategy(new TestStrategy())
.sink(new DefaultSink(new DefaultHttpLogFormatter(), writer))
.build();
}

@Bean
Expand Down Expand Up @@ -278,6 +287,26 @@ void shouldLogRequestWithoutBody() throws IOException {
}
}

@Nested
class WithMockedObjects {
@Test
void shouldNotCallWriteOnInappropriateStage() {
final Logbook logbook = mock(Logbook.class);
final LogbookWebFilter underTest = new LogbookWebFilter(logbook);

final WebFilterChain chain = new DefaultWebFilterChain(
filteredExchange ->
Mono.fromCallable(() -> filteredExchange.getResponse().setStatusCode(HttpStatus.OK))
.then(filteredExchange.getResponse().setComplete()),
Collections.singletonList(underTest));
final ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/test").build());

chain.filter(exchange).block();

assertThat(exchange.getResponse().getStatusCode()).isEqualTo(HttpStatus.OK);
}
}

private void sendAndReceive(final WebClient client) {
sendAndReceive(client, "/echo");
}
Expand Down