Search in sources :

Example 16 with HttpRestResult

use of com.alibaba.nacos.common.http.HttpRestResult in project nacos by alibaba.

the class NamingHttpClientProxyTest method testGetServiceList.

@Test
public void testGetServiceList() throws Exception {
    // given
    NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
    HttpRestResult<Object> a = new HttpRestResult<Object>();
    a.setData("{\"count\":2,\"doms\":[\"aaa\",\"bbb\"]}");
    a.setCode(200);
    when(nacosRestTemplate.exchangeForm(any(), any(), any(), any(), any(), any())).thenReturn(a);
    SecurityProxy proxy = mock(SecurityProxy.class);
    ServerListManager mgr = mock(ServerListManager.class);
    when(mgr.getServerList()).thenReturn(Arrays.asList("localhost"));
    Properties props = new Properties();
    ServiceInfoHolder holder = mock(ServiceInfoHolder.class);
    NamingHttpClientProxy clientProxy = new NamingHttpClientProxy("namespaceId", proxy, mgr, props, holder);
    final Field nacosRestTemplateField = NamingHttpClientProxy.class.getDeclaredField("nacosRestTemplate");
    nacosRestTemplateField.setAccessible(true);
    nacosRestTemplateField.set(clientProxy, nacosRestTemplate);
    String groupName = "group1";
    // when
    ListView<String> serviceList = clientProxy.getServiceList(1, 10, groupName, new NoneSelector());
    // then
    verify(nacosRestTemplate, times(1)).exchangeForm(endsWith("/service/list"), any(), any(), any(), eq(HttpMethod.GET), any());
    Assert.assertEquals(2, serviceList.getCount());
    Assert.assertEquals("aaa", serviceList.getData().get(0));
    Assert.assertEquals("bbb", serviceList.getData().get(1));
}
Also used : ServiceInfoHolder(com.alibaba.nacos.client.naming.cache.ServiceInfoHolder) ServerListManager(com.alibaba.nacos.client.naming.core.ServerListManager) HttpRestResult(com.alibaba.nacos.common.http.HttpRestResult) Properties(java.util.Properties) NoneSelector(com.alibaba.nacos.api.selector.NoneSelector) Field(java.lang.reflect.Field) SecurityProxy(com.alibaba.nacos.client.security.SecurityProxy) NacosRestTemplate(com.alibaba.nacos.common.http.client.NacosRestTemplate) Test(org.junit.Test)

Example 17 with HttpRestResult

use of com.alibaba.nacos.common.http.HttpRestResult in project nacos by alibaba.

the class NamingHttpClientProxyTest method testReqApi2.

@Test
public void testReqApi2() throws Exception {
    // given
    NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
    when(nacosRestTemplate.exchangeForm(any(), any(), any(), any(), any(), any())).thenAnswer(invocationOnMock -> {
        // return url
        HttpRestResult<Object> res = new HttpRestResult<Object>();
        res.setData(invocationOnMock.getArgument(0));
        res.setCode(200);
        return res;
    });
    SecurityProxy proxy = mock(SecurityProxy.class);
    ServerListManager mgr = mock(ServerListManager.class);
    when(mgr.getServerList()).thenReturn(Arrays.asList("localhost"));
    ServiceInfoHolder holder = mock(ServiceInfoHolder.class);
    NamingHttpClientProxy clientProxy = new NamingHttpClientProxy("namespaceId", proxy, mgr, new Properties(), holder);
    final Field nacosRestTemplateField = NamingHttpClientProxy.class.getDeclaredField("nacosRestTemplate");
    nacosRestTemplateField.setAccessible(true);
    nacosRestTemplateField.set(clientProxy, nacosRestTemplate);
    String api = "/api";
    Map<String, String> params = new HashMap<>();
    Map<String, String> body = new HashMap<>();
    String method = HttpMethod.GET;
    // when
    String res = clientProxy.reqApi(api, params, body, method);
    // then
    Assert.assertEquals("http://localhost:8848/api", res);
}
Also used : ServiceInfoHolder(com.alibaba.nacos.client.naming.cache.ServiceInfoHolder) ServerListManager(com.alibaba.nacos.client.naming.core.ServerListManager) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HttpRestResult(com.alibaba.nacos.common.http.HttpRestResult) Properties(java.util.Properties) Field(java.lang.reflect.Field) SecurityProxy(com.alibaba.nacos.client.security.SecurityProxy) NacosRestTemplate(com.alibaba.nacos.common.http.client.NacosRestTemplate) Test(org.junit.Test)

Example 18 with HttpRestResult

use of com.alibaba.nacos.common.http.HttpRestResult in project nacos by alibaba.

the class AbstractResponseHandler method handleError.

private HttpRestResult<T> handleError(HttpClientResponse response) throws Exception {
    Header headers = response.getHeaders();
    String message = IoUtils.toString(response.getBody(), headers.getCharset());
    return new HttpRestResult<T>(headers, response.getStatusCode(), null, message);
}
Also used : Header(com.alibaba.nacos.common.http.param.Header) HttpRestResult(com.alibaba.nacos.common.http.HttpRestResult)

Example 19 with HttpRestResult

use of com.alibaba.nacos.common.http.HttpRestResult in project nacos by alibaba.

the class BeanResponseHandler method convertResult.

@Override
public HttpRestResult<T> convertResult(HttpClientResponse response, Type responseType) throws Exception {
    final Header headers = response.getHeaders();
    InputStream body = response.getBody();
    T extractBody = JacksonUtils.toObj(body, responseType);
    return new HttpRestResult<T>(headers, response.getStatusCode(), extractBody, null);
}
Also used : Header(com.alibaba.nacos.common.http.param.Header) InputStream(java.io.InputStream) HttpRestResult(com.alibaba.nacos.common.http.HttpRestResult)

Example 20 with HttpRestResult

use of com.alibaba.nacos.common.http.HttpRestResult in project nacos by alibaba.

the class StringResponseHandler method convertResult.

@Override
public HttpRestResult<String> convertResult(HttpClientResponse response, Type responseType) throws Exception {
    final Header headers = response.getHeaders();
    String extractBody = IoUtils.toString(response.getBody(), headers.getCharset());
    return new HttpRestResult<String>(headers, response.getStatusCode(), extractBody, null);
}
Also used : Header(com.alibaba.nacos.common.http.param.Header) HttpRestResult(com.alibaba.nacos.common.http.HttpRestResult)

Aggregations

HttpRestResult (com.alibaba.nacos.common.http.HttpRestResult)33 NacosRestTemplate (com.alibaba.nacos.common.http.client.NacosRestTemplate)29 Properties (java.util.Properties)29 Test (org.junit.Test)29 Field (java.lang.reflect.Field)19 ServiceInfoHolder (com.alibaba.nacos.client.naming.cache.ServiceInfoHolder)18 ServerListManager (com.alibaba.nacos.client.naming.core.ServerListManager)18 SecurityProxy (com.alibaba.nacos.client.security.SecurityProxy)18 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 Instance (com.alibaba.nacos.api.naming.pojo.Instance)3 Service (com.alibaba.nacos.api.naming.pojo.Service)3 NoneSelector (com.alibaba.nacos.api.selector.NoneSelector)3 Header (com.alibaba.nacos.common.http.param.Header)3 ServiceInfo (com.alibaba.nacos.api.naming.pojo.ServiceInfo)2 BeatInfo (com.alibaba.nacos.client.naming.beat.BeatInfo)1 DefaultClientHttpResponse (com.alibaba.nacos.common.http.client.response.DefaultClientHttpResponse)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1