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

chore: added multipart test in call api #806

Merged
merged 2 commits into from
Jul 24, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,12 @@
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.tngtech.archunit</groupId>
<artifactId>archunit</artifactId>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/twilio/constant/EnumConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public class EnumConstants {
@RequiredArgsConstructor
public enum ContentType {
JSON("application/json"),
FORM_URLENCODED("application/x-www-form-urlencoded");
FORM_URLENCODED("application/x-www-form-urlencoded"),
MULTIPART_FORM_DATA("multipart/form-data");

private final String value;
}
Expand Down
30 changes: 25 additions & 5 deletions src/test/java/com/twilio/ClusterTest.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.twilio;

import com.twilio.base.Page;

import com.twilio.base.bearertoken.ResourceSet;
import com.twilio.http.CustomHttpClient;
import com.twilio.http.TwilioRestClient;
import com.twilio.rest.api.v2010.account.IncomingPhoneNumber;
import com.twilio.rest.api.v2010.account.IncomingPhoneNumberReader;
import com.twilio.rest.api.v2010.account.Message;
import com.twilio.rest.chat.v2.Service;
import com.twilio.rest.chat.v2.service.User;
import com.twilio.rest.events.v1.Sink;
import com.twilio.rest.events.v1.Subscription;
import com.twilio.rest.previewiam.organizations.Account;
import org.hamcrest.CoreMatchers;
import org.junit.Assume;
import org.junit.Before;
Expand All @@ -19,10 +22,9 @@
import java.util.List;
import java.util.Map;

import static org.junit.Assert.*;

import com.twilio.rest.previewiam.organizations.Account;
import com.twilio.base.bearertoken.ResourceSet;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

public class ClusterTest {
String fromNumber;
Expand All @@ -32,6 +34,8 @@ public class ClusterTest {
String clientSecret;
String organisationSid;

TwilioRestClient customRestClient;

@Before
public void setUp() {
// only run when ClusterTest property is passed (mvn test -Dtest="ClusterTest"), skip test run on mvn test
Expand All @@ -48,6 +52,9 @@ public void setUp() {
clientSecret = System.getenv("TWILIO_ORGS_CLIENT_SECRET");
organisationSid = System.getenv("TWILIO_ORG_SID");
TwilioOrgsTokenAuth.init(grantType, clientId, clientSecret);

// CustomHttpClient
customRestClient = new TwilioRestClient.Builder(apiKey, secret).accountSid(accountSid).httpClient(new CustomHttpClient()).build();
}

@Test
Expand Down Expand Up @@ -141,4 +148,17 @@ public void testOrgsApi(){
assertNotNull(userId);
}

// Test multipart/form-data
@Test
public void testMultiPartFormData() {
Message message = Message.creator(
new com.twilio.type.PhoneNumber(toNumber), new com.twilio.type.PhoneNumber(fromNumber),
"Where's Wallace?")
.create(customRestClient);
assertNotNull(message);
assertTrue(message.getBody().contains("Where's Wallace?"));
assertEquals(fromNumber, message.getFrom().toString());
assertEquals(toNumber, message.getTo().toString());
}

}
70 changes: 70 additions & 0 deletions src/test/java/com/twilio/http/CustomHttpClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.twilio.http;

import com.twilio.exception.ApiException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.StringBody;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

public class CustomHttpClient extends NetworkHttpClient {

public CustomHttpClient() {
super();
}

@Override
public <T extends IRequest> Response makeRequest(T request) {
HttpMethod method = request.getMethod();
RequestBuilder builder = RequestBuilder.create(method.toString())
.setUri(request.constructURL().toString())
.setVersion(HttpVersion.HTTP_1_1)
.setCharset(StandardCharsets.UTF_8);
if (request instanceof Request) {
Request basicRequest = (Request) request;
if (basicRequest.requiresAuthentication()) {
builder.addHeader(HttpHeaders.AUTHORIZATION, basicRequest.getAuthString());
}
}

for (Map.Entry<String, List<String>> entry : request.getHeaderParams().entrySet()) {
for (String value : entry.getValue()) {
builder.addHeader(entry.getKey(), value);
}
}
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
for (Map.Entry<String, List<String>> entry : request.getPostParams().entrySet()) {
for (String value : entry.getValue()) {
multipartEntityBuilder.addPart(entry.getKey(), new StringBody(value, ContentType.TEXT_PLAIN));
}
}
builder.addHeader(HttpHeaders.USER_AGENT, HttpUtility.getUserAgentString(request.getUserAgentExtensions(), true));
builder.setEntity(multipartEntityBuilder.build());
HttpResponse response = null;

try {
response = client.execute(builder.build());
HttpEntity entity = response.getEntity();
return new Response(
// Consume the entire HTTP response before returning the stream
entity == null ? null : new BufferedHttpEntity(entity).getContent(),
response.getStatusLine().getStatusCode(),
response.getAllHeaders()
);
} catch (IOException e) {
throw new ApiException(e.getMessage(), e);
} finally {
HttpClientUtils.closeQuietly(response);
}
}
}
Loading