Skip to content

Commit

Permalink
Add tests for issue 73
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielLiu1123 committed Dec 1, 2024
1 parent b1b5184 commit fee7ab0
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package issues.issue73;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* @author Freeman
* @since 2024/12/1
*/
@Configuration(proxyBeanMethods = false)
@EnableAutoConfiguration
@Import(HttpClientConfiguration.class)
@RestController
@RequestMapping("/users")
class CfgWithHttpClientConfiguration {
@GetMapping("/{id}")
public String getUsername(@PathVariable("id") String id) {
return "Hello " + id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package issues.issue73;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* @author Freeman
* @since 2024/12/1
*/
@Configuration(proxyBeanMethods = false)
@EnableAutoConfiguration
@RestController
@RequestMapping("/users")
class CfgWithoutHttpClientConfiguration {
@GetMapping("/{id}")
public String getUsername(@PathVariable("id") String id) {
return "Hello " + id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package issues.issue73;

import org.springframework.boot.web.context.WebServerApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.support.RestClientAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;

/**
* @author Freeman
* @since 2024/12/1
*/
@Configuration(proxyBeanMethods = false)
public class HttpClientConfiguration {

@Bean
public UserApi userApi(RestClient.Builder builder, WebServerApplicationContext ctx) {
builder.baseUrl("http://localhost:" + ctx.getWebServer().getPort());
return HttpServiceProxyFactory.builderFor(RestClientAdapter.create(builder.build()))
.build()
.createClient(UserApi.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package issues.issue73;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.springframework.test.util.TestSocketUtils.findAvailableTcpPort;

import org.junit.jupiter.api.Test;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.web.client.ResourceAccessException;

/**
* @author Freeman
* @see <a href="https://github.com/DanielLiu1123/httpexchange-spring-boot-starter/pull/73">Fixes use manually registered bean not works</a>
*/
class Issue73Test {

@Test
void useManualRegisteredBean_whenManualRegisteredBeanExists() {
var port = findAvailableTcpPort();
try (var ctx = new SpringApplicationBuilder(CfgWithHttpClientConfiguration.class)
.web(WebApplicationType.SERVLET)
.properties("server.port=" + port)
.properties("http-exchange.base-packages=" + UserApi.class.getPackageName())
.properties("http-exchange.base-url=localhost:" + (port - 1)) // wrong base-url
.run()) {

var api = ctx.getBean(UserApi.class);

var username = api.getUsername("1");

// Got the correct result, means the manual registered bean works
assertThat(username).isEqualTo("Hello 1");
}
}

@Test
void useAutoRegisteredBean_whenNoManualRegisteredBean() {
var port = findAvailableTcpPort();

try (var ctx = new SpringApplicationBuilder(CfgWithoutHttpClientConfiguration.class)
.web(WebApplicationType.SERVLET)
.properties("server.port=" + port)
.properties("http-exchange.base-packages=" + UserApi.class.getPackageName())
.properties("http-exchange.base-url=localhost:" + (port - 1)) // wrong base-url
.run()) {

var api = ctx.getBean(UserApi.class);

assertThatCode(() -> api.getUsername("1"))
.isInstanceOf(ResourceAccessException.class)
.hasMessageContaining("I/O error");
}

try (var ctx = new SpringApplicationBuilder(CfgWithoutHttpClientConfiguration.class)
.web(WebApplicationType.SERVLET)
.properties("server.port=" + port)
.properties("http-exchange.base-packages=" + UserApi.class.getPackageName())
.properties("http-exchange.base-url=localhost:" + port) // correct base-url
.run()) {

var api = ctx.getBean(UserApi.class);

var username = api.getUsername("1");

assertThat(username).isEqualTo("Hello 1");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package issues.issue73;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.service.annotation.GetExchange;
import org.springframework.web.service.annotation.HttpExchange;

@HttpExchange("/users")
public interface UserApi {
@GetExchange("/{id}")
String getUsername(@PathVariable("id") String id);
}

0 comments on commit fee7ab0

Please sign in to comment.