<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-netty</artifactId>
<version>5.11.2</version>
</dependency>
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mocksever-client-java</artifactId>
<version>5.11.2</version>
</dependency>
import org.mockserver.configuration.ConfigurationProperties;
import org.mockserver.proxy.ProxyConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ProxyServerApplication {
public static void main(String[] args) {
SpringApplication.run(ProxyServerApplication.class, args);
}
}
import org.mockserver.client.MockServerClient;
import org.mockserver.model.HttpResponse;
import org.mockserver.springtest.MockServerTest;
import static org.mockserver.model.HttpRequest.request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
@MockServerTest
public class ProxyController {
@Autowired
private MockServerClient mockServerClient;
@PostMapping("/proxy")
public ResponseEntity<String> proxyRequest(@RequestBody String requestBody) {
HttpResponse response = mockServerClient.sendRequest(request().withBody(requestBody));
return ResponseEntity.status(HttpStatus.valueOf(response.getStatusCode())).body(response.getBody().toString());
}
}
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.ResponseEntity;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ProxyControllerTest {
@LocalServerPort
private int port;
@Autowired
private TestRestTemplate restTemplate;
@Test
public void testProxyRequest() {
String requestBody = "{\"key\": \"value\"}";
ResponseEntity<String> response = restTemplate.postForEntity("http://localhost:" + port + "/proxy", requestBody, String.class);
assertThat(response.getStatusCodeValue()).isEqualTo(200);
assertThat(response.getBody()).isEqualTo("OK");
}
}