org.springframework.mock.http.server.reactive.MockServerHttpResponse Java Examples

The following examples show how to use org.springframework.mock.http.server.reactive.MockServerHttpResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpHandlerConnector.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public Mono<ClientHttpResponse> connect(HttpMethod httpMethod, URI uri,
		Function<? super ClientHttpRequest, Mono<Void>> requestCallback) {

	MonoProcessor<ClientHttpResponse> result = MonoProcessor.create();

	MockClientHttpRequest mockClientRequest = new MockClientHttpRequest(httpMethod, uri);
	MockServerHttpResponse mockServerResponse = new MockServerHttpResponse();

	mockClientRequest.setWriteHandler(requestBody -> {
		log("Invoking HttpHandler for ", httpMethod, uri);
		ServerHttpRequest mockServerRequest = adaptRequest(mockClientRequest, requestBody);
		ServerHttpResponse responseToUse = prepareResponse(mockServerResponse, mockServerRequest);
		this.handler.handle(mockServerRequest, responseToUse).subscribe(aVoid -> {}, result::onError);
		return Mono.empty();
	});

	mockServerResponse.setWriteHandler(responseBody ->
			Mono.fromRunnable(() -> {
				log("Creating client response for ", httpMethod, uri);
				result.onNext(adaptResponse(mockServerResponse, responseBody));
			}));

	log("Writing client request for ", httpMethod, uri);
	requestCallback.apply(mockClientRequest).subscribe(aVoid -> {}, result::onError);

	return result;
}
 
Example #2
Source File: HttpHandlerConnector.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private ClientHttpResponse adaptResponse(MockServerHttpResponse response, Flux<DataBuffer> body) {
	HttpStatus status = Optional.ofNullable(response.getStatusCode()).orElse(HttpStatus.OK);
	MockClientHttpResponse clientResponse = new MockClientHttpResponse(status);
	clientResponse.getHeaders().putAll(response.getHeaders());
	clientResponse.getCookies().putAll(response.getCookies());
	clientResponse.setBody(body);
	return clientResponse;
}
 
Example #3
Source File: HttpHandlerConnector.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public Mono<ClientHttpResponse> connect(HttpMethod httpMethod, URI uri,
		Function<? super ClientHttpRequest, Mono<Void>> requestCallback) {

	MonoProcessor<ClientHttpResponse> result = MonoProcessor.create();

	MockClientHttpRequest mockClientRequest = new MockClientHttpRequest(httpMethod, uri);
	MockServerHttpResponse mockServerResponse = new MockServerHttpResponse();

	mockClientRequest.setWriteHandler(requestBody -> {
		log("Invoking HttpHandler for ", httpMethod, uri);
		ServerHttpRequest mockServerRequest = adaptRequest(mockClientRequest, requestBody);
		ServerHttpResponse responseToUse = prepareResponse(mockServerResponse, mockServerRequest);
		this.handler.handle(mockServerRequest, responseToUse).subscribe(aVoid -> {}, result::onError);
		return Mono.empty();
	});

	mockServerResponse.setWriteHandler(responseBody ->
			Mono.fromRunnable(() -> {
				log("Creating client response for ", httpMethod, uri);
				result.onNext(adaptResponse(mockServerResponse, responseBody));
			}));

	log("Writing client request for ", httpMethod, uri);
	requestCallback.apply(mockClientRequest).subscribe(aVoid -> {}, result::onError);

	return result;
}
 
Example #4
Source File: HttpHandlerConnector.java    From java-technology-stack with MIT License 5 votes vote down vote up
private ClientHttpResponse adaptResponse(MockServerHttpResponse response, Flux<DataBuffer> body) {
	HttpStatus status = Optional.ofNullable(response.getStatusCode()).orElse(HttpStatus.OK);
	MockClientHttpResponse clientResponse = new MockClientHttpResponse(status);
	clientResponse.getHeaders().putAll(response.getHeaders());
	clientResponse.getCookies().putAll(response.getCookies());
	clientResponse.setBody(body);
	return clientResponse;
}
 
Example #5
Source File: PathMatcherServerWebExchangeMatcherTests.java    From spring-security-reactive with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
	MockServerHttpRequest request = MockServerHttpRequest.post("/path").build();
	MockServerHttpResponse response = new MockServerHttpResponse();
	DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
	exchange = new DefaultServerWebExchange(request, response, sessionManager);
	pattern = "/pattern";
	path = "/path";

	matcher = new PathMatcherServerWebExchangeMatcher(pattern);
	matcher.setPathMatcher(pathMatcher);
}
 
Example #6
Source File: GatewayHttpTagsProviderTests.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
@Test
public void notAbstractServerHttpResponse() {
	ServerWebExchange mockExchange = mock(ServerWebExchange.class);
	ServerHttpResponseDecorator responseDecorator = new ServerHttpResponseDecorator(
			new MockServerHttpResponse());
	responseDecorator.setStatusCode(OK);

	when(mockExchange.getRequest())
			.thenReturn(MockServerHttpRequest.get(ROUTE_URI).build());
	when(mockExchange.getResponse()).thenReturn(responseDecorator);

	Tags tags = tagsProvider.apply(mockExchange);
	assertThat(tags).isEqualTo(DEFAULT_TAGS);
}
 
Example #7
Source File: ProblemExceptionHandlerTest.java    From problem-spring-web with MIT License 5 votes vote down vote up
@Test
void shouldHandleResponseStatusException() {
    MockServerHttpResponse response = exchange.getResponse();
    handler.handle(exchange, new ResponseStatusException(HttpStatus.CONFLICT)).block();

    assertThat(response.getStatusCode(), is(HttpStatus.CONFLICT));
    assertThat(response.getHeaders().getContentType(), is(MediaTypes.PROBLEM));
    assertThat(response.getBodyAsString().block(), containsString(Status.CONFLICT.getReasonPhrase()));
}
 
Example #8
Source File: AdviceUtilsTest.java    From problem-spring-web with MIT License 5 votes vote down vote up
@Test
void shouldSetHttpResponse() {
    MockServerHttpResponse response = webExchange.getResponse();

    AdviceUtils.setHttpResponse(responseEntity, webExchange, mapper)
            .map(it -> response.setComplete())
            .block();

    assertThat(response.getStatusCode(), is(HttpStatus.CONFLICT));
    assertThat(response.getHeaders().getContentType(), is(MediaTypes.PROBLEM));
    assertThat(response.getBodyAsString().block(), containsString(Status.CONFLICT.getReasonPhrase()));

}
 
Example #9
Source File: MockServerWebExchange.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private MockServerWebExchange(MockServerHttpRequest request, WebSessionManager sessionManager) {
	super(request, new MockServerHttpResponse(), sessionManager,
			ServerCodecConfigurer.create(), new AcceptHeaderLocaleContextResolver());
}
 
Example #10
Source File: MockServerWebExchange.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public MockServerHttpResponse getResponse() {
	return (MockServerHttpResponse) super.getResponse();
}
 
Example #11
Source File: MockServerWebExchange.java    From java-technology-stack with MIT License 4 votes vote down vote up
private MockServerWebExchange(MockServerHttpRequest request, WebSessionManager sessionManager) {
	super(request, new MockServerHttpResponse(), sessionManager,
			ServerCodecConfigurer.create(), new AcceptHeaderLocaleContextResolver());
}
 
Example #12
Source File: MockServerWebExchange.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public MockServerHttpResponse getResponse() {
	return (MockServerHttpResponse) super.getResponse();
}