ConsumerSignFilter implements Filter { @Override public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException { URL url = invoker.getUrl(); boolean shouldAuth = url.getParameter(Constants.SERVICE_AUTH, false); if (shouldAuth) { Authenticator authenticator = ExtensionLoader.getExtensionLoader(Authenticator.class) .getExtension(url.getParameter(Constants.AUTHENTICATOR, Constants.DEFAULT_AUTHENTICATOR)); authenticator.sign(invocation, url); } return invoker.invoke(invocation); }  @Override Result invoke(Invoker<?> invoker, Invocation invocation);  }
@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()); }
@Test void testAuthEnabled() { URL url = URL.valueOf("dubbo: .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); ConsumerSignFilter consumerSignFilter = new ConsumerSignFilter(); consumerSignFilter.invoke(invoker, invocation); verify(invocation, times(1)).setAttachment(eq(Constants.REQUEST_SIGNATURE_KEY), anyString()); }