Skip to content

Commit

Permalink
Support RestClient in MockRestServiceServer
Browse files Browse the repository at this point in the history
Closes gh-30821
  • Loading branch information
rstoyanchev committed Jul 11, 2023
1 parent 8448f59 commit c61d011
Showing 1 changed file with 52 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,6 +27,7 @@
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.mock.http.client.MockClientHttpRequest;
import org.springframework.util.Assert;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.support.RestGatewaySupport;

Expand Down Expand Up @@ -137,13 +138,22 @@ public void reset() {
}


/**
* Return a builder for a {@code MockRestServiceServer} that should be used
* to reply to the given {@code RestTemplate}.
* @since 6.1
*/
public static MockRestServiceServerBuilder bindTo(RestClient.Builder restClientBuilder) {
return new RestClientMockRestServiceServerBuilder(restClientBuilder);
}

/**
* Return a builder for a {@code MockRestServiceServer} that should be used
* to reply to the given {@code RestTemplate}.
* @since 4.3
*/
public static MockRestServiceServerBuilder bindTo(RestTemplate restTemplate) {
return new DefaultBuilder(restTemplate);
return new RestTemplateMockRestServiceServerBuilder(restTemplate);
}

/**
Expand All @@ -153,7 +163,7 @@ public static MockRestServiceServerBuilder bindTo(RestTemplate restTemplate) {
*/
public static MockRestServiceServerBuilder bindTo(RestGatewaySupport restGatewaySupport) {
Assert.notNull(restGatewaySupport, "'restGatewaySupport' must not be null");
return new DefaultBuilder(restGatewaySupport.getRestTemplate());
return new RestTemplateMockRestServiceServerBuilder(restGatewaySupport.getRestTemplate());
}


Expand Down Expand Up @@ -214,20 +224,14 @@ public interface MockRestServiceServerBuilder {
}


private static class DefaultBuilder implements MockRestServiceServerBuilder {

private final RestTemplate restTemplate;
private abstract static class AbstractMockRestServiceServerBuilder implements MockRestServiceServerBuilder {

private boolean ignoreExpectOrder;

private boolean bufferContent;


public DefaultBuilder(RestTemplate restTemplate) {
Assert.notNull(restTemplate, "RestTemplate must not be null");
this.restTemplate = restTemplate;
}

@Override
public MockRestServiceServerBuilder ignoreExpectOrder(boolean ignoreExpectOrder) {
this.ignoreExpectOrder = ignoreExpectOrder;
Expand All @@ -253,15 +257,48 @@ public MockRestServiceServer build() {
@Override
public MockRestServiceServer build(RequestExpectationManager manager) {
MockRestServiceServer server = new MockRestServiceServer(manager);
MockClientHttpRequestFactory factory = server.new MockClientHttpRequestFactory();
ClientHttpRequestFactory factory = server.new MockClientHttpRequestFactory();
if (this.bufferContent) {
this.restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(factory));
}
else {
this.restTemplate.setRequestFactory(factory);
factory = new BufferingClientHttpRequestFactory(factory);
}
injectRequestFactory(factory);
return server;
}

protected abstract void injectRequestFactory(ClientHttpRequestFactory requestFactory);

}


private static class RestClientMockRestServiceServerBuilder extends AbstractMockRestServiceServerBuilder {

private final RestClient.Builder restClientBuilder;

RestClientMockRestServiceServerBuilder(RestClient.Builder restClientBuilder) {
Assert.notNull(restClientBuilder, "RestClient.Builder must not be null");
this.restClientBuilder = restClientBuilder;
}

@Override
protected void injectRequestFactory(ClientHttpRequestFactory requestFactory) {
this.restClientBuilder.requestFactory(requestFactory);
}
}


private static class RestTemplateMockRestServiceServerBuilder extends AbstractMockRestServiceServerBuilder {

private final RestTemplate restTemplate;

RestTemplateMockRestServiceServerBuilder(RestTemplate restTemplate) {
Assert.notNull(restTemplate, "RestTemplate must not be null");
this.restTemplate = restTemplate;
}

@Override
protected void injectRequestFactory(ClientHttpRequestFactory requestFactory) {
this.restTemplate.setRequestFactory(requestFactory);
}
}


Expand Down

0 comments on commit c61d011

Please sign in to comment.