Skip to content

Commit

Permalink
fix issue with unicode requests when doing requests that contain data
Browse files Browse the repository at this point in the history
  • Loading branch information
ibalosh committed Mar 6, 2023
1 parent b71f8da commit ee62fc0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 35 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</developers>

<properties>
<postmark.version>1.10.1</postmark.version>
<postmark.version>1.10.2</postmark.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<jackson.minimum.version>2.9.7</jackson.minimum.version>
Expand Down
50 changes: 25 additions & 25 deletions src/main/java/com/postmarkapp/postmark/client/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
import org.apache.hc.core5.util.Timeout;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Map;

/**
Expand Down Expand Up @@ -76,29 +75,7 @@ public ClientResponse execute(REQUEST_TYPES request_type, String url) throws IOE
* @return response from HTTP request
*/
public ClientResponse execute(REQUEST_TYPES requestType, String url, String data) throws IOException {
ClassicHttpRequest request;

switch (requestType) {
case POST:
request = ClassicRequestBuilder.post(getHttpUrl(url)).setEntity(data, ContentType.APPLICATION_JSON).build();
break;

case PUT:
request = ClassicRequestBuilder.put(getHttpUrl(url)).setEntity(data, ContentType.APPLICATION_JSON).build();
break;

case PATCH:
request = ClassicRequestBuilder.patch(getHttpUrl(url)).setEntity(data, ContentType.APPLICATION_JSON).build();
break;

case DELETE:
request = ClassicRequestBuilder.delete(getHttpUrl(url)).build();
break;

default:
request = ClassicRequestBuilder.get(getHttpUrl(url)).build();
break;
}
ClassicHttpRequest request = createRequest(requestType, url, data);

for (Map.Entry<String, Object> header : headers.entrySet()) {
request.setHeader(header.getKey(), header.getValue().toString());
Expand All @@ -109,6 +86,29 @@ public ClientResponse execute(REQUEST_TYPES requestType, String url, String data
response -> new ClientResponse(response.getCode(), EntityUtils.toString(response.getEntity())));
}

/**
*
* Create HTTP request based on request type and other data
*
* @param requestType type of HTTP request to initiate
* @param url request url
* @param data data sent for POST/PUT requests
* @return built HTTP request to execute
*/
private ClassicHttpRequest createRequest(REQUEST_TYPES requestType, String url, String data) {
ClassicRequestBuilder requestBuilder = ClassicRequestBuilder
.create(requestType.name())
.setUri(getHttpUrl(url));

if (data != null) {
return requestBuilder
.setEntity(data, ContentType.APPLICATION_JSON)
.build();
} else {
return requestBuilder.build();
}
}

// Setters and Getters

public void setConnectTimeoutSeconds(int connectTimeoutSeconds) {
Expand Down
21 changes: 12 additions & 9 deletions src/test/java/integration/TemplateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ void validateTextBody() throws PostmarkException, IOException {

@Test
void createTemplate() throws PostmarkException, IOException {

String templateName = "name";

TemplateContent templateContent = new TemplateContent();
Expand All @@ -72,26 +71,30 @@ void createTemplate() throws PostmarkException, IOException {
}

@Test
void createTemplate_should_work_with_utf_8() throws PostmarkException, IOException {
void createTemplateWithUTFContent() throws PostmarkException, IOException {

String utf8String = "test html with unicode symbols: € Ä Æ ©";
String templateName = "name";
String htmlBodyText = "test html with unicode symbols: € Ä Æ ©";
String textBodyText = "test text with unicode symbols: € Ä Æ ©";
String templateName = "name € Ä Æ ©";
String subject = "subject € Ä Æ ©";

TemplateContent templateContent = new TemplateContent();
templateContent.setHtmlBody(utf8String);
templateContent.setTextBody("test text");
templateContent.setHtmlBody(htmlBodyText);
templateContent.setTextBody(textBodyText);
templateContent.setName(templateName);
templateContent.setSubject("subject");
templateContent.setSubject(subject);

BaseTemplate response = client.createTemplate(templateContent);
assertEquals(response.getName(),templateName);

Template template = client.getTemplate(response.getTemplateId());
assertEquals(utf8String, template.getHtmlBody());
assertEquals(htmlBodyText, template.getHtmlBody());
assertEquals(textBodyText, template.getTextBody());
assertEquals(subject, template.getSubject());
assertEquals(templateName, template.getName());

Integer id = response.getTemplateId();
client.deleteTemplate(id);

}

@Test
Expand Down

0 comments on commit ee62fc0

Please sign in to comment.