use of io.servicecomb.core.Response in project java-chassis by ServiceComb.
the class TestProducerResponseMapper method test.
@Test
public void test() {
ProducerResponseMapperFactory factory = new ProducerResponseMapperFactory();
ProducerResponseMapper m1 = new ProducerResponseCseSame();
ProducerResponseMapper m2 = new ProducerResponseSame();
factory.setMapperList(Arrays.asList(m1, m2));
Assert.assertEquals(m1, factory.createResponseMapper(Response.class));
Assert.assertEquals(m2, factory.createResponseMapper(String.class));
Response r = Response.ok(1);
Response r1 = m1.mapResponse(null, r);
Assert.assertEquals(r, r1);
r1 = m2.mapResponse(Status.OK, 1);
Assert.assertEquals(1, (int) r1.getResult());
}
use of io.servicecomb.core.Response in project java-chassis by ServiceComb.
the class CodeFirstRestTemplateSpringmvc method testIntf.
private void testIntf() {
ResponseEntity<User> responseEntity = intf.responseEntity();
TestMgr.check("User [name=nameA, age=100, index=0]", responseEntity.getBody());
TestMgr.check("h1v", responseEntity.getHeaders().getFirst("h1"));
TestMgr.check("h2v", responseEntity.getHeaders().getFirst("h2"));
checkStatusCode("springmvc", 202, responseEntity.getStatusCode());
Response cseResponse = intf.cseResponse();
TestMgr.check("User [name=nameA, age=100, index=0]", cseResponse.getResult());
TestMgr.check("h1v", cseResponse.getHeaders().getFirst("h1"));
TestMgr.check("h2v", cseResponse.getHeaders().getFirst("h2"));
}
use of io.servicecomb.core.Response in project java-chassis by ServiceComb.
the class TestProducerSchemaFactory method testGetOrCreateProducer.
@Test
public void testGetOrCreateProducer() throws Exception {
OperationMeta operationMeta = schemaMeta.ensureFindOperation("add");
Assert.assertEquals("add", operationMeta.getOperationId());
ProducerOperation producerOperation = operationMeta.getExtData(Const.PRODUCER_OPERATION);
Object addBody = Class.forName("cse.gen.app.ms.schema.addBody").newInstance();
ReflectUtils.setField(addBody, "x", 1);
ReflectUtils.setField(addBody, "y", 2);
Invocation invocation = new Invocation((Endpoint) null, operationMeta, new Object[] { addBody });
Holder<Response> holder = new Holder<>();
producerOperation.invoke(invocation, resp -> {
holder.value = resp;
});
Assert.assertEquals(3, (int) holder.value.getResult());
invocation = new Invocation((Endpoint) null, operationMeta, new Object[] { 1, 2 });
producerOperation.invoke(invocation, resp -> {
holder.value = resp;
});
Assert.assertEquals(true, holder.value.isFailed());
InvocationException exception = (InvocationException) holder.value.getResult();
CommonExceptionData data = (CommonExceptionData) exception.getErrorData();
Assert.assertEquals("Cse Internal Server Error", data.getMessage());
}
use of io.servicecomb.core.Response in project java-chassis by ServiceComb.
the class TestHighwayCodec method testDecodeResponse.
@Test
public void testDecodeResponse() throws Exception {
Invocation invocation = Mockito.mock(Invocation.class);
Mockito.when(operationProtobuf.findResponseSchema(200)).thenReturn(Mockito.mock(WrapSchema.class));
Map<String, String> context = new HashMap<>();
Mockito.when(invocation.getContext()).thenReturn(context);
TcpData tcpData = Mockito.mock(TcpData.class);
Mockito.when(tcpData.getHeaderBuffer()).thenReturn(bodyBuffer);
commonMock();
ResponseHeader header = new ResponseHeader();
header.setStatusCode(200);
header.setContext(new HashMap<String, String>());
header.getContext().put("a", "10");
Buffer responseBuf = HighwayCodec.encodeResponse(0, header, null, null);
TcpData tcp = new TcpData(responseBuf.slice(23, responseBuf.length()), null);
Response response = HighwayCodec.decodeResponse(invocation, operationProtobuf, tcp);
Assert.assertEquals("10", invocation.getContext().get("a"));
Assert.assertEquals(200, response.getStatusCode());
}
use of io.servicecomb.core.Response in project java-chassis by ServiceComb.
the class VertxHttpMethod method handleResponse.
protected void handleResponse(Invocation invocation, HttpClientResponse httpResponse, RestOperationMeta restOperation, AsyncResponse asyncResp) {
ProduceProcessor produceProcessor = null;
String contentType = httpResponse.getHeader("Content-Type");
if (contentType != null) {
String contentTypeForFind = contentType;
int idx = contentType.indexOf(';');
if (idx != -1) {
contentTypeForFind = contentType.substring(0, idx);
}
produceProcessor = restOperation.findProduceProcessor(contentTypeForFind);
}
if (produceProcessor == null) {
String msg = String.format("path %s, statusCode %d, reasonPhrase %s, response content-type %s is not supported", restOperation.getAbsolutePath(), httpResponse.statusCode(), httpResponse.statusMessage(), contentType);
Exception exception = ExceptionFactory.createConsumerException(new CommonExceptionData(msg));
asyncResp.fail(invocation.getInvocationType(), exception);
return;
}
ProduceProcessor finalProduceProcessor = produceProcessor;
httpResponse.bodyHandler(responseBuf -> {
invocation.getResponseExecutor().execute(() -> {
try {
ResponseMeta responseMeta = restOperation.getOperationMeta().findResponseMeta(httpResponse.statusCode());
Object result = finalProduceProcessor.decodeResponse(responseBuf, responseMeta.getJavaType());
Response response = Response.create(httpResponse.statusCode(), httpResponse.statusMessage(), result);
for (String headerName : responseMeta.getHeaders().keySet()) {
List<String> headerValues = httpResponse.headers().getAll(headerName);
for (String headerValue : headerValues) {
response.getHeaders().addHeader(headerName, headerValue);
}
}
asyncResp.complete(response);
} catch (Throwable e) {
asyncResp.fail(invocation.getInvocationType(), e);
}
});
});
}
Aggregations