use of org.jocean.http.rosa.impl.AttachmentInMemory in project jocean-http by isdom.
the class DefaultSignalClientTestCase method testSignalClientWithAttachmentSuccess.
@Test
public void testSignalClientWithAttachmentSuccess() throws Exception {
final TestResponse respToSendback = new TestResponse("0", "OK");
final AtomicReference<HttpMethod> reqMethodReceivedRef = new AtomicReference<>();
final AtomicReference<String> reqpathReceivedRef = new AtomicReference<>();
final AtomicReference<TestRequestByPost> reqbeanReceivedRef = new AtomicReference<>();
final List<FileUpload> uploads = new ArrayList<>();
final Action2<FullHttpRequest, HttpTrade> requestAndTradeAwareWhenCompleted = new Action2<FullHttpRequest, HttpTrade>() {
@Override
public void call(final FullHttpRequest req, final HttpTrade trade) {
reqMethodReceivedRef.set(req.method());
reqpathReceivedRef.set(req.uri());
HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(HTTP_DATA_FACTORY, req);
// first is signal
boolean isfirst = true;
while (decoder.hasNext()) {
final InterfaceHttpData data = decoder.next();
if (!isfirst) {
if (data instanceof FileUpload) {
uploads.add((FileUpload) data);
}
} else {
isfirst = false;
try {
reqbeanReceivedRef.set((TestRequestByPost) JSON.parseObject(Nettys.dumpByteBufAsBytes(((FileUpload) data).content()), TestRequestByPost.class));
} catch (Exception e) {
LOG.warn("exception when JSON.parseObject, detail: {}", ExceptionUtils.exception2detail(e));
}
}
}
trade.outbound(buildResponse(respToSendback, trade.onTerminate()));
}
};
// launch test server for attachment send
final String testAddr = UUID.randomUUID().toString();
final Subscription server = TestHttpUtil.createTestServerWith(testAddr, requestAndTradeAwareWhenCompleted, Feature.ENABLE_LOGGING, Feature.ENABLE_COMPRESSOR);
try {
final TestChannelCreator creator = new TestChannelCreator();
final TestChannelPool pool = new TestChannelPool(1);
final DefaultHttpClient httpclient = new DefaultHttpClient(creator, pool);
final DefaultSignalClient signalClient = new DefaultSignalClient(new URI("http://test"), httpclient, new AttachmentBuilder4InMemory());
signalClient.registerRequestType(TestRequestByPost.class, TestResponse.class, null, buildUri2Addr(testAddr), Feature.ENABLE_LOGGING);
final AttachmentInMemory[] attachsToSend = new AttachmentInMemory[] { new AttachmentInMemory("1", "text/plain", "11111111111111".getBytes(Charsets.UTF_8)), new AttachmentInMemory("2", "text/plain", "22222222222222222".getBytes(Charsets.UTF_8)), new AttachmentInMemory("3", "text/plain", "333333333333333".getBytes(Charsets.UTF_8)) };
final TestRequestByPost reqToSend = new TestRequestByPost("1", null);
final TestResponse respReceived = ((SignalClient) signalClient).interaction().request(reqToSend).feature(attachsToSend).<TestResponse>build().timeout(1, TimeUnit.SECONDS).toBlocking().single();
assertEquals(HttpMethod.POST, reqMethodReceivedRef.get());
assertEquals("/test/simpleRequest", reqpathReceivedRef.get());
assertEquals(reqToSend, reqbeanReceivedRef.get());
assertEquals(respToSendback, respReceived);
final FileUpload[] attachsReceived = uploads.toArray(new FileUpload[0]);
assertEquals(attachsToSend.length, attachsReceived.length);
for (int idx = 0; idx < attachsToSend.length; idx++) {
final AttachmentInMemory inmemory = attachsToSend[idx];
final FileUpload upload = attachsReceived[idx];
assertEquals(inmemory.filename, upload.getName());
assertEquals(inmemory.contentType, upload.getContentType());
assertTrue(Arrays.equals(inmemory.content(), upload.get()));
}
pool.awaitRecycleChannels();
} finally {
server.unsubscribe();
}
}
Aggregations