Skip to content

Commit

Permalink
chore: added multipart test in call api (#806)
Browse files Browse the repository at this point in the history
* added multipart test in message api with custom client
  • Loading branch information
sbansla authored Jul 24, 2024
1 parent 002234c commit ab74cef
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 6 deletions.
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);
}
}
}

0 comments on commit ab74cef

Please sign in to comment.