Java Code Examples for org.apache.servicecomb.core.Invocation#getHandlerContext()

The following examples show how to use org.apache.servicecomb.core.Invocation#getHandlerContext() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestRestTemplateCopyHeaderFilter.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
@Test
public void beforeSendRequestWithNullHeader(@Mocked Invocation invocation) {
  Map<String, Object> context = new HashMap<>(1);
  HttpHeaders httpHeaders = new HttpHeaders();
  context.put(RestConst.CONSUMER_HEADER, httpHeaders);
  httpHeaders.add("headerName0", "headerValue0");
  httpHeaders.add("headerName1", null);
  httpHeaders.add("headerName2", "headerValue2");
  new Expectations() {
    {
      invocation.getHandlerContext();
      result = context;
    }
  };

  HttpServletRequestEx requestEx = new CommonToHttpServletRequest(null, null, new HttpHeaders(), null, false);
  filter.beforeSendRequest(invocation, requestEx);
  Assert.assertEquals("headerValue0", requestEx.getHeader("headerName0"));
  Assert.assertEquals("headerValue2", requestEx.getHeader("headerName2"));
  Assert.assertNull(requestEx.getHeader("headerName1"));
}
 
Example 2
Source File: TestRestTemplateCopyHeaderFilter.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
@Test
public void beforeSendRequestHaveHeader(@Mocked Invocation invocation) {
  HttpHeaders httpHeaders = new HttpHeaders();
  httpHeaders.add("name", "value");

  Map<String, Object> context = new HashMap<>();
  context.put(RestConst.CONSUMER_HEADER, httpHeaders);
  new Expectations() {
    {
      invocation.getHandlerContext();
      result = context;
    }
  };

  HttpServletRequestEx requestEx = new CommonToHttpServletRequest(null, null, new HttpHeaders(), null, false);
  filter.beforeSendRequest(invocation, requestEx);
  Assert.assertThat(Collections.list(requestEx.getHeaders("name")), Matchers.contains("value"));
}
 
Example 3
Source File: TestRestTemplateCopyHeaderFilter.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
@Test
public void beforeSendRequestSkipContentLength(@Mocked Invocation invocation) {
  HttpHeaders httpHeaders = new HttpHeaders();
  httpHeaders.add(HttpHeaders.CONTENT_LENGTH, "0");

  Map<String, Object> context = new HashMap<>();
  context.put(RestConst.CONSUMER_HEADER, httpHeaders);
  new Expectations() {
    {
      invocation.getHandlerContext();
      result = context;
    }
  };

  HttpServletRequestEx requestEx = new CommonToHttpServletRequest(null, null, new HttpHeaders(), null, false);
  filter.beforeSendRequest(invocation, requestEx);
  Assert.assertNull((requestEx.getHeader(HttpHeaders.CONTENT_LENGTH)));
}
 
Example 4
Source File: TestInvocationToHttpServletRequest.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetRemoteAddressEmpty(@Mocked Invocation invocation) throws Exception {
  handlerContext.remove(Const.REMOTE_ADDRESS);
  new Expectations() {
    {
      invocation.getOperationMeta();
      result = operationMeta;
      operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION);
      result = swaggerOperation;
      invocation.getHandlerContext();
      result = handlerContext;
    }
  };
  InvocationToHttpServletRequest request = new InvocationToHttpServletRequest(invocation);
  String addr = request.getRemoteAddr();
  String host = request.getRemoteHost();
  int port = request.getRemotePort();
  Assert.assertEquals(addr, "");
  Assert.assertEquals(host, "");
  Assert.assertEquals(port, 0);
}
 
Example 5
Source File: TestDefaultHttpClientFilter.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
@Test
public void extractResult_readStreamPart(@Mocked Invocation invocation, @Mocked ReadStreamPart part,
    @Mocked HttpServletResponseEx responseEx) {
  Map<String, Object> handlerContext = new HashMap<>();
  handlerContext.put(RestConst.READ_STREAM_PART, part);
  new Expectations() {
    {
      invocation.getHandlerContext();
      result = handlerContext;
      responseEx.getStatusType();
      result = Status.OK;
    }
  };

  Response response = filter.extractResponse(invocation, responseEx);
  Assert.assertSame(part, response.getResult());
  Assert.assertEquals(Status.OK, response.getStatus());
}
 
Example 6
Source File: TestRestTemplateCopyHeaderFilter.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Test
public void beforeSendRequestNoHeader(@Mocked Invocation invocation) {
  Map<String, Object> context = new HashMap<>();
  new Expectations() {
    {
      invocation.getHandlerContext();
      result = context;
    }
  };

  HttpServletRequestEx requestEx = new CommonToHttpServletRequest(null, null, new HttpHeaders(), null, false);
  filter.beforeSendRequest(invocation, requestEx);
  Assert.assertFalse(requestEx.getHeaderNames().hasMoreElements());
}