org.springframework.mock.http.client.reactive.MockClientHttpResponse Java Examples

The following examples show how to use org.springframework.mock.http.client.reactive.MockClientHttpResponse. 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: WiretapConnectorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void captureAndClaim() {
	ClientHttpRequest request = new MockClientHttpRequest(HttpMethod.GET, "/test");
	ClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
	ClientHttpConnector connector = (method, uri, fn) -> fn.apply(request).then(Mono.just(response));

	ClientRequest clientRequest = ClientRequest.create(HttpMethod.GET, URI.create("/test"))
			.header(WebTestClient.WEBTESTCLIENT_REQUEST_ID, "1").build();

	WiretapConnector wiretapConnector = new WiretapConnector(connector);
	ExchangeFunction function = ExchangeFunctions.create(wiretapConnector);
	function.exchange(clientRequest).block(ofMillis(0));

	WiretapConnector.Info actual = wiretapConnector.claimRequest("1");
	ExchangeResult result = actual.createExchangeResult(Duration.ZERO, null);
	assertEquals(HttpMethod.GET, result.getMethod());
	assertEquals("/test", result.getUrl().toString());
}
 
Example #2
Source File: AuthenticationStepsOperatorUnitTests.java    From spring-vault with Apache License 2.0 6 votes vote down vote up
@Test
void justLoginRequestShouldLogin() {

	ClientHttpRequest request = new MockClientHttpRequest(HttpMethod.POST, "/auth/cert/login");
	MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
	response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
	response.setBody(
			"{" + "\"auth\":{\"client_token\":\"my-token\", \"renewable\": true, \"lease_duration\": 10}" + "}");
	ClientHttpConnector connector = (method, uri, fn) -> fn.apply(request).then(Mono.just(response));

	WebClient webClient = WebClient.builder().clientConnector(connector).build();

	AuthenticationSteps steps = AuthenticationSteps
			.just(post("/auth/{path}/login", "cert").as(VaultResponse.class));

	login(steps, webClient).as(StepVerifier::create) //
			.expectNext(VaultToken.of("my-token")) //
			.verifyComplete();
}
 
Example #3
Source File: AuthenticationStepsOperatorUnitTests.java    From spring-vault with Apache License 2.0 6 votes vote down vote up
@Test
void justLoginShouldFail() {

	ClientHttpRequest request = new MockClientHttpRequest(HttpMethod.POST, "/auth/cert/login");
	MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.BAD_REQUEST);
	ClientHttpConnector connector = (method, uri, fn) -> fn.apply(request).then(Mono.just(response));

	WebClient webClient = WebClient.builder().clientConnector(connector).build();

	AuthenticationSteps steps = AuthenticationSteps
			.just(post("/auth/{path}/login", "cert").as(VaultResponse.class));

	login(steps, webClient).as(StepVerifier::create) //
			.expectError() //
			.verify();
}
 
Example #4
Source File: ReactiveVaultClientsUnitTests.java    From spring-vault with Apache License 2.0 6 votes vote down vote up
@Test
void shouldApplyNamespace() {

	ClientHttpRequest request = new MockClientHttpRequest(HttpMethod.POST, "/auth/foo");
	MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);

	ClientHttpConnector connector = (method, uri, fn) -> fn.apply(request).then(Mono.just(response));

	WebClient webClient = WebClient.builder().clientConnector(connector)
			.filter(ReactiveVaultClients.namespace("foo/bar")).build();

	webClient.get().uri("/auth/foo").retrieve().bodyToMono(String.class).as(StepVerifier::create) //
			.verifyComplete();

	assertThat(request.getHeaders()).containsEntry(VaultHttpHeaders.VAULT_NAMESPACE,
			Collections.singletonList("foo/bar"));
}
 
Example #5
Source File: ReactiveVaultClientsUnitTests.java    From spring-vault with Apache License 2.0 6 votes vote down vote up
@Test
void shouldAllowNamespaceOverride() {

	ClientHttpRequest request = new MockClientHttpRequest(HttpMethod.POST, "/auth/foo");
	MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);

	ClientHttpConnector connector = (method, uri, fn) -> fn.apply(request).then(Mono.just(response));

	WebClient webClient = WebClient.builder().clientConnector(connector)
			.filter(ReactiveVaultClients.namespace("foo/bar")).build();

	webClient.get().uri("/auth/foo").header(VaultHttpHeaders.VAULT_NAMESPACE, "baz").retrieve()
			.bodyToMono(String.class) //
			.as(StepVerifier::create) //
			.verifyComplete();

	assertThat(request.getHeaders()).containsEntry(VaultHttpHeaders.VAULT_NAMESPACE,
			Collections.singletonList("baz"));
}
 
Example #6
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 #7
Source File: StatusAssertionTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private StatusAssertions statusAssertions(HttpStatus status) {
	MockClientHttpRequest request = new MockClientHttpRequest(HttpMethod.GET, URI.create("/"));
	MockClientHttpResponse response = new MockClientHttpResponse(status);

	MonoProcessor<byte[]> emptyContent = MonoProcessor.create();
	emptyContent.onComplete();

	ExchangeResult result = new ExchangeResult(request, response, emptyContent, emptyContent, Duration.ZERO, null);
	return new StatusAssertions(result, mock(WebTestClient.ResponseSpec.class));
}
 
Example #8
Source File: HeaderAssertionTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private HeaderAssertions headerAssertions(HttpHeaders responseHeaders) {
	MockClientHttpRequest request = new MockClientHttpRequest(HttpMethod.GET, URI.create("/"));
	MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
	response.getHeaders().putAll(responseHeaders);

	MonoProcessor<byte[]> emptyContent = MonoProcessor.create();
	emptyContent.onComplete();

	ExchangeResult result = new ExchangeResult(request, response, emptyContent, emptyContent, Duration.ZERO, null);
	return new HeaderAssertions(result, mock(WebTestClient.ResponseSpec.class));
}
 
Example #9
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 #10
Source File: StatusAssertionTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private StatusAssertions statusAssertions(HttpStatus status) {
	MockClientHttpRequest request = new MockClientHttpRequest(HttpMethod.GET, URI.create("/"));
	MockClientHttpResponse response = new MockClientHttpResponse(status);

	MonoProcessor<byte[]> emptyContent = MonoProcessor.create();
	emptyContent.onComplete();

	ExchangeResult result = new ExchangeResult(request, response, emptyContent, emptyContent, Duration.ZERO, null);
	return new StatusAssertions(result, mock(WebTestClient.ResponseSpec.class));
}
 
Example #11
Source File: WiretapConnectorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void captureAndClaim() {
	ClientHttpRequest request = new MockClientHttpRequest(HttpMethod.GET, "/test");
	ClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
	ClientHttpConnector connector = (method, uri, fn) -> fn.apply(request).then(Mono.just(response));

	ClientRequest clientRequest = ClientRequest.create(HttpMethod.GET, URI.create("/test"))
			.header(WebTestClient.WEBTESTCLIENT_REQUEST_ID, "1").build();

	WiretapConnector wiretapConnector = new WiretapConnector(connector);
	ExchangeFunction function = ExchangeFunctions.create(wiretapConnector);
	function.exchange(clientRequest).block(ofMillis(0));

	WiretapConnector.Info actual = wiretapConnector.claimRequest("1");
	ExchangeResult result = actual.createExchangeResult(Duration.ZERO, null);
	assertEquals(HttpMethod.GET, result.getMethod());
	assertEquals("/test", result.getUrl().toString());
}
 
Example #12
Source File: HeaderAssertionTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private HeaderAssertions headerAssertions(HttpHeaders responseHeaders) {
	MockClientHttpRequest request = new MockClientHttpRequest(HttpMethod.GET, URI.create("/"));
	MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
	response.getHeaders().putAll(responseHeaders);

	MonoProcessor<byte[]> emptyContent = MonoProcessor.create();
	emptyContent.onComplete();

	ExchangeResult result = new ExchangeResult(request, response, emptyContent, emptyContent, Duration.ZERO, null);
	return new HeaderAssertions(result, mock(WebTestClient.ResponseSpec.class));
}
 
Example #13
Source File: AuthenticationStepsOperatorUnitTests.java    From spring-vault with Apache License 2.0 5 votes vote down vote up
@Test
void zipWithShouldRequestTwoItems() {

	ClientHttpRequest leftRequest = new MockClientHttpRequest(HttpMethod.GET, "/auth/login/left");
	MockClientHttpResponse leftResponse = new MockClientHttpResponse(HttpStatus.OK);
	leftResponse.getHeaders().setContentType(MediaType.APPLICATION_JSON);
	leftResponse.setBody("{" + "\"request_id\": \"left\"}");

	ClientHttpRequest rightRequest = new MockClientHttpRequest(HttpMethod.GET, "/auth/login/right");
	MockClientHttpResponse rightResponse = new MockClientHttpResponse(HttpStatus.OK);
	rightResponse.getHeaders().setContentType(MediaType.APPLICATION_JSON);
	rightResponse.setBody("{" + "\"request_id\": \"right\"}");

	ClientHttpConnector connector = (method, uri, fn) -> {

		if (uri.toString().contains("left")) {
			return fn.apply(leftRequest).then(Mono.just(leftResponse));
		}

		return fn.apply(rightRequest).then(Mono.just(rightResponse));
	};

	WebClient webClient = WebClient.builder().clientConnector(connector).build();

	Node<VaultResponse> left = AuthenticationSteps
			.fromHttpRequest(post("/auth/login/left").as(VaultResponse.class));

	Node<VaultResponse> right = AuthenticationSteps
			.fromHttpRequest(post("/auth/login/right").as(VaultResponse.class));

	AuthenticationSteps steps = left.zipWith(right)
			.login(it -> VaultToken.of(it.getLeft().getRequestId() + "-" + it.getRight().getRequestId()));

	login(steps, webClient).as(StepVerifier::create) //
			.expectNext(VaultToken.of("left-right")) //
			.verifyComplete();
}