use of com.ingenico.connect.gateway.sdk.java.ResponseHeader in project connect-sdk-java by Ingenico-ePayments.
the class PaymentsClientTest method testCreateNotFound.
/**
* Tests that a 404 response with a non-JSON response will throw a {@link NotFoundException}.
*/
@Test
@SuppressWarnings("resource")
public void testCreateNotFound() {
Client client = Factory.createClient(session);
String responseBody = getResource("not_found.html");
whenPost().thenReturn(new Response(404, responseBody, Arrays.asList(new ResponseHeader("content-type", "text/html"))));
CreatePaymentRequest body = createRequest();
try {
client.merchant("merchantId").payments().create(body);
Assert.fail("Expected NotFoundException");
} catch (NotFoundException e) {
Assert.assertNotNull(e.getCause());
Assert.assertEquals(ResponseException.class, e.getCause().getClass());
Assert.assertTrue(e.getCause().toString().contains(responseBody));
}
}
use of com.ingenico.connect.gateway.sdk.java.ResponseHeader in project connect-sdk-java by Ingenico-ePayments.
the class PaymentsClientTest method testCreateMethodNotAllowed.
/**
* Tests that a 405 response with a non-JSON response will throw a {@link CommunicationException}.
*/
@Test
@SuppressWarnings("resource")
public void testCreateMethodNotAllowed() {
Client client = Factory.createClient(session);
String responseBody = getResource("method_not_allowed.html");
whenPost().thenReturn(new Response(405, responseBody, Arrays.asList(new ResponseHeader("content-type", "text/html"))));
CreatePaymentRequest body = createRequest();
try {
client.merchant("merchantId").payments().create(body);
Assert.fail("Expected CommunicationException");
} catch (CommunicationException e) {
Assert.assertNotNull(e.getCause());
Assert.assertEquals(ResponseException.class, e.getCause().getClass());
Assert.assertTrue(e.getCause().toString().contains(responseBody));
}
}
use of com.ingenico.connect.gateway.sdk.java.ResponseHeader in project connect-sdk-java by Ingenico-ePayments.
the class PaymentsClientTest method testCreateInternalServerErrorWithoutBody.
/**
* Tests that a 500 response with a JSON response with no body will throw a {@link GlobalCollectException} and not a {@link NullPointerException}.
*/
@Test
@SuppressWarnings("resource")
public void testCreateInternalServerErrorWithoutBody() {
Client client = Factory.createClient(session);
String responseBody = null;
whenPost().thenReturn(new Response(500, responseBody, Arrays.asList(new ResponseHeader("content-type", "text/html"))));
CreatePaymentRequest body = createRequest();
try {
client.merchant("merchantId").payments().create(body);
Assert.fail("Expected GlobalCollectException");
} catch (GlobalCollectException e) {
Assert.assertEquals(responseBody, e.getResponseBody());
Assert.assertNull(e.getErrorId());
Assert.assertEquals(0, e.getErrors().size());
}
}
use of com.ingenico.connect.gateway.sdk.java.ResponseHeader in project connect-sdk-java by Ingenico-ePayments.
the class DefaultConnection method getHeaders.
protected List<ResponseHeader> getHeaders(HttpResponse httpResponse) {
Header[] headers = httpResponse.getAllHeaders();
List<ResponseHeader> result = new ArrayList<ResponseHeader>(headers.length);
for (Header header : headers) {
result.add(new ResponseHeader(header.getName(), header.getValue()));
}
return result;
}
use of com.ingenico.connect.gateway.sdk.java.ResponseHeader in project connect-sdk-java by Ingenico-ePayments.
the class DefaultConnection method executeRequest.
@SuppressWarnings("resource")
protected Response executeRequest(HttpUriRequest request) {
final String requestId = UUID.randomUUID().toString();
final long startTime = System.currentTimeMillis();
HttpContext context = new BasicHttpContext();
context.setAttribute(REQUEST_ID_ATTRIBUTE, requestId);
context.setAttribute(START_TIMME_ATTRIBUTE, startTime);
try {
CloseableHttpResponse httpResponse = httpClient.execute(request, context);
HttpEntity entity = httpResponse.getEntity();
try {
int statusCode = httpResponse.getStatusLine().getStatusCode();
String body = entity == null ? null : EntityUtils.toString(entity, CHARSET);
List<ResponseHeader> headers = getHeaders(httpResponse);
return new Response(statusCode, body, headers);
} finally {
/*
* Ensure that the entity content is fully consumed and the
* content stream, if exists, is closed so the connection can be
* reused. Do not close the httpResponse because that will
* prevent the connection from being reused.
*/
EntityUtils.consume(entity);
}
} catch (ClientProtocolException e) {
logError(requestId, e, startTime, communicatorLogger);
throw new CommunicationException(e);
} catch (IOException e) {
logError(requestId, e, startTime, communicatorLogger);
throw new CommunicationException(e);
} catch (RuntimeException e) {
logError(requestId, e, startTime, communicatorLogger);
throw e;
}
}
Aggregations