Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(http-client): set content-length of request #5

Merged
merged 1 commit into from Sep 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -40,10 +40,12 @@ public abstract class AbstractHttpConnector<Q extends HttpBaseRequest<Q, R>, R e
protected static HttpConnectorLogger LOG = HttpLogger.HTTP_LOGGER;

protected CloseableHttpClient httpClient;
protected final Charset charset;

public AbstractHttpConnector(String connectorId) {
super(connectorId);
httpClient = createClient();
charset = Charset.forName("utf-8");
}

protected CloseableHttpClient createClient() {
Expand Down Expand Up @@ -135,8 +137,10 @@ protected <T extends HttpRequestBase> void applyHeaders(T httpRequest, Map<Strin
protected <T extends HttpRequestBase> void applyPayload(T httpRequest, Q request) {
if (httpMethodSupportsPayload(httpRequest)) {
if (request.getPayload() != null) {
ByteArrayInputStream payload = new ByteArrayInputStream(request.getPayload().getBytes(Charset.forName("utf-8")));
((HttpEntityEnclosingRequestBase) httpRequest).setEntity(new InputStreamEntity(payload));
byte[] bytes = request.getPayload().getBytes(charset);
ByteArrayInputStream payload = new ByteArrayInputStream(bytes);
InputStreamEntity entity = new InputStreamEntity(payload, bytes.length);
((HttpEntityEnclosingRequestBase) httpRequest).setEntity(entity);
}
}
else if (request.getPayload() != null) {
Expand Down
Expand Up @@ -161,6 +161,15 @@ public void shouldSetPayloadOnHttpRequest() throws IOException {
assertThat(content).isEqualTo(EXAMPLE_PAYLOAD);
}

@Test
public void shouldSetContentLength() {
connector.createRequest().url(EXAMPLE_URL).payload(EXAMPLE_PAYLOAD).post().execute();
HttpPost request = interceptor.getTarget();
long contentLength = request.getEntity().getContentLength();

assertThat(contentLength).isEqualTo(EXAMPLE_PAYLOAD.length());
}

protected void verifyHttpRequest(Class<? extends HttpRequestBase> requestClass) {
Object target = interceptor.getTarget();
assertThat(target).isInstanceOf(requestClass);
Expand Down