-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b1b5184
commit fee7ab0
Showing
5 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
...pring-boot-autoconfigure/src/test/java/issues/issue73/CfgWithHttpClientConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...ng-boot-autoconfigure/src/test/java/issues/issue73/CfgWithoutHttpClientConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...hange-spring-boot-autoconfigure/src/test/java/issues/issue73/HttpClientConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
httpexchange-spring-boot-autoconfigure/src/test/java/issues/issue73/Issue73Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
httpexchange-spring-boot-autoconfigure/src/test/java/issues/issue73/UserApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |