Search in sources :

Example 6 with Invoker

use of org.apache.dubbo.rpc.Invoker in project dubbo by alibaba.

the class ProviderAuthFilterTest method testAuthSuccessfully.

@Test
void testAuthSuccessfully() {
    String service = "org.apache.dubbo.DemoService";
    String method = "test";
    long currentTimeMillis = System.currentTimeMillis();
    URL url = URL.valueOf("dubbo://10.10.10.10:2181").setServiceInterface(service).addParameter(Constants.ACCESS_KEY_ID_KEY, "ak").addParameter(Constants.SECRET_ACCESS_KEY_KEY, "sk").addParameter(CommonConstants.APPLICATION_KEY, "test-provider").addParameter(Constants.SERVICE_AUTH, true);
    Invoker invoker = mock(Invoker.class);
    Invocation invocation = mock(Invocation.class);
    when(invocation.getAttachment(Constants.AK_KEY)).thenReturn("ak");
    when(invocation.getAttachment(CommonConstants.CONSUMER)).thenReturn("test-consumer");
    when(invocation.getAttachment(Constants.REQUEST_TIMESTAMP_KEY)).thenReturn(String.valueOf(currentTimeMillis));
    when(invocation.getMethodName()).thenReturn(method);
    when(invoker.getUrl()).thenReturn(url);
    String requestString = String.format(Constants.SIGNATURE_STRING_FORMAT, url.getColonSeparatedKey(), invocation.getMethodName(), "sk", currentTimeMillis);
    String sign = SignatureUtils.sign(requestString, "sk");
    when(invocation.getAttachment(Constants.REQUEST_SIGNATURE_KEY)).thenReturn(sign);
    ProviderAuthFilter providerAuthFilter = new ProviderAuthFilter();
    Result result = providerAuthFilter.invoke(invoker, invocation);
    assertNull(result);
}
Also used : Invoker(org.apache.dubbo.rpc.Invoker) Invocation(org.apache.dubbo.rpc.Invocation) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) URL(org.apache.dubbo.common.URL) Result(org.apache.dubbo.rpc.Result) Test(org.junit.jupiter.api.Test)

Example 7 with Invoker

use of org.apache.dubbo.rpc.Invoker in project dubbo by alibaba.

the class ProviderAuthFilterTest method testAuthDisabled.

@Test
void testAuthDisabled() {
    URL url = mock(URL.class);
    Invoker invoker = mock(Invoker.class);
    Invocation invocation = mock(Invocation.class);
    when(invoker.getUrl()).thenReturn(url);
    ProviderAuthFilter providerAuthFilter = new ProviderAuthFilter();
    providerAuthFilter.invoke(invoker, invocation);
    verify(url, never()).getParameter(eq(Constants.AUTHENTICATOR), eq(Constants.DEFAULT_AUTHENTICATOR));
}
Also used : Invoker(org.apache.dubbo.rpc.Invoker) Invocation(org.apache.dubbo.rpc.Invocation) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Example 8 with Invoker

use of org.apache.dubbo.rpc.Invoker in project dubbo by alibaba.

the class ProviderAuthFilterTest method testAuthEnabled.

@Test
void testAuthEnabled() {
    URL url = URL.valueOf("dubbo://10.10.10.10:2181").addParameter(Constants.ACCESS_KEY_ID_KEY, "ak").addParameter(Constants.SECRET_ACCESS_KEY_KEY, "sk").addParameter(CommonConstants.APPLICATION_KEY, "test").addParameter(Constants.SERVICE_AUTH, true);
    Invoker invoker = mock(Invoker.class);
    Invocation invocation = mock(Invocation.class);
    when(invoker.getUrl()).thenReturn(url);
    ProviderAuthFilter providerAuthFilter = new ProviderAuthFilter();
    providerAuthFilter.invoke(invoker, invocation);
    verify(invocation, atLeastOnce()).getAttachment(anyString());
}
Also used : Invoker(org.apache.dubbo.rpc.Invoker) Invocation(org.apache.dubbo.rpc.Invocation) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Example 9 with Invoker

use of org.apache.dubbo.rpc.Invoker in project dubbo by alibaba.

the class ConsumerSignFilterTest method testAuthDisabled.

@Test
void testAuthDisabled() {
    URL url = mock(URL.class);
    Invoker invoker = mock(Invoker.class);
    Invocation invocation = mock(Invocation.class);
    when(invoker.getUrl()).thenReturn(url);
    ConsumerSignFilter consumerSignFilter = new ConsumerSignFilter();
    consumerSignFilter.invoke(invoker, invocation);
    verify(invocation, never()).setAttachment(eq(Constants.REQUEST_SIGNATURE_KEY), anyString());
}
Also used : Invoker(org.apache.dubbo.rpc.Invoker) Invocation(org.apache.dubbo.rpc.Invocation) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Example 10 with Invoker

use of org.apache.dubbo.rpc.Invoker in project dubbo by alibaba.

the class RpcUtilsTest method testIsAsync.

@Test
public void testIsAsync() {
    Object[] args = new Object[] { "hello", "dubbo", 520 };
    Class<?> demoServiceClass = DemoService.class;
    String serviceName = demoServiceClass.getName();
    Invoker invoker = mock(Invoker.class);
    URL url = URL.valueOf("test://127.0.0.1:1/org.apache.dubbo.rpc.support.DemoService?interface=org.apache.dubbo.rpc.support.DemoService");
    RpcInvocation inv = new RpcInvocation("test", serviceName, "", new Class<?>[] { String.class, String[].class, Object[].class }, new Object[] { "method", new String[] {}, args }, null, invoker, null);
    Assertions.assertFalse(RpcUtils.isAsync(url, inv));
    inv.setInvokeMode(InvokeMode.ASYNC);
    Assertions.assertTrue(RpcUtils.isAsync(url, inv));
    URL url1 = URL.valueOf("dubbo://localhost/?test.async=true");
    Invocation inv1 = new RpcInvocation("test", "DemoService", "", new Class[] {}, new String[] {});
    inv1.setAttachment("test." + ASYNC_KEY, Boolean.FALSE.toString());
    Assertions.assertFalse(RpcUtils.isAsync(url1, inv1));
    URL url2 = URL.valueOf("dubbo://localhost/?test.async=true");
    Invocation inv2 = new RpcInvocation("test", "DemoService", "", new Class[] {}, new String[] {});
    inv2.setAttachment(ASYNC_KEY, Boolean.FALSE.toString());
    Assertions.assertFalse(RpcUtils.isAsync(url2, inv2));
    URL url3 = URL.valueOf("dubbo://localhost/?test");
    Invocation inv3 = new RpcInvocation("test", "DemoService", "", new Class[] {}, new String[] {});
    inv3.setAttachment(ASYNC_KEY, Boolean.TRUE.toString());
    Assertions.assertTrue(RpcUtils.isAsync(url3, inv3));
    URL url4 = URL.valueOf("dubbo://localhost/?test.async=true");
    Invocation inv4 = new RpcInvocation("test", "DemoService", "", new Class[] {}, new String[] {});
    Assertions.assertTrue(RpcUtils.isAsync(url4, inv4));
    URL url5 = URL.valueOf("dubbo://localhost/?test");
    Invocation inv5 = new RpcInvocation("test", "DemoService", "", new Class[] {}, new String[] {});
    Assertions.assertFalse(RpcUtils.isAsync(url5, inv5));
    URL url6 = URL.valueOf("dubbo://localhost/?test");
    Invocation inv6 = new RpcInvocation("test", "DemoService", "", new Class[] {}, new String[] {});
    inv6.setAttachment("test." + ASYNC_KEY, Boolean.TRUE.toString());
    Assertions.assertTrue(RpcUtils.isAsync(url6, inv6));
    URL url7 = URL.valueOf("dubbo://localhost/?test.async=true&async=false");
    Invocation inv7 = new RpcInvocation("test", "DemoService", "", new Class[] {}, new String[] {});
    Assertions.assertTrue(RpcUtils.isAsync(url7, inv7));
    URL url8 = URL.valueOf("dubbo://localhost/?test.async=false&async=true");
    Invocation inv8 = new RpcInvocation("test", "DemoService", "", new Class[] {}, new String[] {});
    Assertions.assertFalse(RpcUtils.isAsync(url8, inv8));
    URL url9 = URL.valueOf("dubbo://localhost/?test.async=true");
    Invocation inv9 = new RpcInvocation("test", "DemoService", "", new Class[] {}, new String[] {});
    inv9.setAttachment("testB." + ASYNC_KEY, Boolean.FALSE.toString());
    Assertions.assertTrue(RpcUtils.isAsync(url9, inv9));
    URL url10 = URL.valueOf("dubbo://localhost/?test.async=true");
    Invocation inv10 = new RpcInvocation("test", "DemoService", "", new Class[] {}, new String[] {});
    ((RpcInvocation) inv10).setInvokeMode(InvokeMode.ASYNC);
    Assertions.assertTrue(RpcUtils.isAsync(url10, inv9));
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invoker(org.apache.dubbo.rpc.Invoker) Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Invoker (org.apache.dubbo.rpc.Invoker)128 Test (org.junit.jupiter.api.Test)104 URL (org.apache.dubbo.common.URL)74 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)66 ArrayList (java.util.ArrayList)48 Invocation (org.apache.dubbo.rpc.Invocation)42 Result (org.apache.dubbo.rpc.Result)26 AppResponse (org.apache.dubbo.rpc.AppResponse)21 MockClusterInvoker (org.apache.dubbo.rpc.cluster.support.wrapper.MockClusterInvoker)21 RegistryDirectory (org.apache.dubbo.registry.integration.RegistryDirectory)20 Router (org.apache.dubbo.rpc.cluster.Router)18 MockInvoker (org.apache.dubbo.rpc.cluster.router.MockInvoker)18 HashMap (java.util.HashMap)13 LoadBalance (org.apache.dubbo.rpc.cluster.LoadBalance)13 AsyncRpcResult (org.apache.dubbo.rpc.AsyncRpcResult)12 RpcException (org.apache.dubbo.rpc.RpcException)12 LeastActiveLoadBalance (org.apache.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance)8 RandomLoadBalance (org.apache.dubbo.rpc.cluster.loadbalance.RandomLoadBalance)8 RoundRobinLoadBalance (org.apache.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)8