|
38 | 38 | import org.junit.jupiter.api.Assertions;
|
39 | 39 | import org.junit.jupiter.api.Test;
|
40 | 40 |
|
| 41 | +import javax.ws.rs.core.Response; |
| 42 | +import javax.ws.rs.ext.ExceptionMapper; |
41 | 43 | import java.util.Map;
|
42 | 44 |
|
43 | 45 | import static org.apache.dubbo.remoting.Constants.SERVER_KEY;
|
| 46 | +import static org.apache.dubbo.rpc.protocol.rest.Constants.EXCEPTION_MAPPER_KEY; |
44 | 47 | import static org.apache.dubbo.rpc.protocol.rest.Constants.EXTENSION_KEY;
|
45 | 48 | import static org.hamcrest.CoreMatchers.equalTo;
|
46 | 49 | import static org.hamcrest.CoreMatchers.is;
|
47 | 50 | import static org.hamcrest.CoreMatchers.nullValue;
|
48 | 51 | import static org.hamcrest.MatcherAssert.assertThat;
|
49 | 52 |
|
50 | 53 | class RestProtocolTest {
|
51 |
| - private Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getExtension("rest"); |
52 |
| - private ProxyFactory proxy = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); |
| 54 | + private final Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getExtension("rest"); |
| 55 | + private final ProxyFactory proxy = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); |
53 | 56 | private final int availablePort = NetUtils.getAvailablePort();
|
54 | 57 | private final URL exportUrl = URL.valueOf("rest://127.0.0.1:" + availablePort + "/rest?interface=org.apache.dubbo.rpc.protocol.rest.DemoService");
|
55 | 58 | private final ModuleServiceRepository repository = ApplicationModel.defaultModel().getDefaultModule().getServiceRepository();
|
@@ -246,6 +249,29 @@ void testDefaultPort() {
|
246 | 249 | assertThat(protocol.getDefaultPort(), is(80));
|
247 | 250 | }
|
248 | 251 |
|
| 252 | + @Test |
| 253 | + void testExceptionMapper() { |
| 254 | + DemoService server = new DemoServiceImpl(); |
| 255 | + |
| 256 | + URL url = this.registerProvider(exportUrl, server, DemoService.class); |
| 257 | + |
| 258 | + URL exceptionUrl = url.addParameter(EXCEPTION_MAPPER_KEY, TestExceptionMapper.class.getName()); |
| 259 | + |
| 260 | + protocol.export(proxy.getInvoker(server, DemoService.class, exceptionUrl)); |
| 261 | + |
| 262 | + DemoService referDemoService = this.proxy.getProxy(protocol.refer(DemoService.class, exceptionUrl)); |
| 263 | + |
| 264 | + Assertions.assertEquals("test-exception", referDemoService.error()); |
| 265 | + } |
| 266 | + |
| 267 | + public static class TestExceptionMapper implements ExceptionMapper<RuntimeException> { |
| 268 | + |
| 269 | + @Override |
| 270 | + public Response toResponse(RuntimeException e) { |
| 271 | + return Response.ok("test-exception").build(); |
| 272 | + } |
| 273 | + } |
| 274 | + |
249 | 275 | private URL registerProvider(URL url, Object impl, Class<?> interfaceClass) {
|
250 | 276 | ServiceDescriptor serviceDescriptor = repository.registerService(interfaceClass);
|
251 | 277 | ProviderModel providerModel = new ProviderModel(
|
|
0 commit comments