use of com.alibaba.nacos.common.http.HttpRestResult in project nacos by alibaba.
the class DefaultAsyncHttpClientRequest method execute.
@Override
public <T> void execute(URI uri, String httpMethod, RequestHttpEntity requestHttpEntity, final ResponseHandler<T> responseHandler, final Callback<T> callback) throws Exception {
HttpRequestBase httpRequestBase = DefaultHttpClientRequest.build(uri, httpMethod, requestHttpEntity);
asyncClient.execute(httpRequestBase, new FutureCallback<HttpResponse>() {
@Override
public void completed(HttpResponse result) {
DefaultClientHttpResponse response = new DefaultClientHttpResponse(result);
try {
HttpRestResult<T> httpRestResult = responseHandler.handle(response);
callback.onReceive(httpRestResult);
} catch (Exception e) {
callback.onError(e);
} finally {
HttpClientUtils.closeQuietly(result);
}
}
@Override
public void failed(Exception ex) {
callback.onError(ex);
}
@Override
public void cancelled() {
callback.onCancel();
}
});
}
use of com.alibaba.nacos.common.http.HttpRestResult in project nacos by alibaba.
the class SecurityProxyTest method testTestLoginFailCode.
@Test
public void testTestLoginFailCode() throws Exception {
NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
HttpRestResult<Object> result = new HttpRestResult<>();
result.setCode(400);
when(nacosRestTemplate.postForm(any(), (Header) any(), any(), any(), any())).thenReturn(result);
Properties properties = new Properties();
properties.setProperty(PropertyKeyConst.USERNAME, "aaa");
properties.setProperty(PropertyKeyConst.PASSWORD, "123456");
SecurityProxy securityProxy = new SecurityProxy(properties, nacosRestTemplate);
boolean ret = securityProxy.login("localhost");
Assert.assertFalse(ret);
}
use of com.alibaba.nacos.common.http.HttpRestResult in project nacos by alibaba.
the class SecurityProxyTest method testLoginSuccess.
@Test
public void testLoginSuccess() throws Exception {
// given
NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
HttpRestResult<Object> result = new HttpRestResult<>();
result.setData("{\"accessToken\":\"ttttttttttttttttt\",\"tokenTtl\":1000}");
result.setCode(200);
when(nacosRestTemplate.postForm(any(), (Header) any(), any(), any(), any())).thenReturn(result);
Properties properties = new Properties();
properties.setProperty(PropertyKeyConst.USERNAME, "aaa");
properties.setProperty(PropertyKeyConst.PASSWORD, "123456");
SecurityProxy securityProxy = new SecurityProxy(properties, nacosRestTemplate);
// when
boolean ret = securityProxy.login("localhost");
// then
Assert.assertTrue(ret);
}
use of com.alibaba.nacos.common.http.HttpRestResult in project nacos by alibaba.
the class SecurityProxyTest method testGetAccessToken.
@Test
public void testGetAccessToken() throws Exception {
NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
HttpRestResult<Object> result = new HttpRestResult<>();
result.setData("{\"accessToken\":\"abc\",\"tokenTtl\":1000}");
result.setCode(200);
when(nacosRestTemplate.postForm(any(), (Header) any(), any(), any(), any())).thenReturn(result);
Properties properties = new Properties();
properties.setProperty(PropertyKeyConst.USERNAME, "aaa");
properties.setProperty(PropertyKeyConst.PASSWORD, "123456");
SecurityProxy securityProxy = new SecurityProxy(properties, nacosRestTemplate);
securityProxy.login("localhost");
String accessToken = securityProxy.getAccessToken();
Assert.assertEquals("abc", accessToken);
}
use of com.alibaba.nacos.common.http.HttpRestResult in project nacos by alibaba.
the class NacosClientAuthServiceImplTest method testLoginSuccess.
@Test
public void testLoginSuccess() throws Exception {
// given
NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
HttpRestResult<Object> result = new HttpRestResult<>();
result.setData("{\"accessToken\":\"ttttttttttttttttt\",\"tokenTtl\":1000}");
result.setCode(200);
when(nacosRestTemplate.postForm(any(), (Header) any(), any(), any(), any())).thenReturn(result);
Properties properties = new Properties();
properties.setProperty(PropertyKeyConst.USERNAME, "aaa");
properties.setProperty(PropertyKeyConst.PASSWORD, "123456");
List<String> serverList = new ArrayList<>();
serverList.add("localhost");
NacosClientAuthServiceImpl nacosClientAuthService = new NacosClientAuthServiceImpl();
nacosClientAuthService.setServerList(serverList);
nacosClientAuthService.setNacosRestTemplate(nacosRestTemplate);
// when
boolean ret = nacosClientAuthService.login(properties);
// then
Assert.assertTrue(ret);
}
Aggregations