Skip to content

Commit 53ed507

Browse files
authored
Merge pull request #18 from maxmind/greg/correctly-encode-string-entities
Correctly encode non-ASCII string entities.
2 parents c596769 + 05f721a commit 53ed507

4 files changed

Lines changed: 31 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
1.1.1 (2016-10-12)
5+
------------------
6+
7+
* Non-ASCII characters are now correctly encoded as UTF-8 in the request body.
8+
Reported by Julien Guery. GitHub #17.
9+
410
1.1.0 (2016-10-10)
511
------------------
612

src/main/java/com/maxmind/minfraud/WebServiceClient.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import java.net.*;
3232
import java.util.*;
3333

34+
import static org.apache.http.entity.ContentType.APPLICATION_JSON;
35+
3436
/**
3537
* Client for MaxMind minFraud Score, Insights, and Factors
3638
*/
@@ -309,8 +311,7 @@ private HttpPost requestFor(Transaction transaction, URL url)
309311

310312
String requestBody = transaction.toJson();
311313

312-
StringEntity input = new StringEntity(requestBody);
313-
input.setContentType("application/json");
314+
StringEntity input = new StringEntity(requestBody, APPLICATION_JSON);
314315

315316
request.setEntity(input);
316317
return request;

src/test/java/com/maxmind/minfraud/WebServiceClientTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.github.tomakehurst.wiremock.junit.WireMockRule;
44
import com.maxmind.minfraud.exception.*;
5+
import com.maxmind.minfraud.request.Device;
6+
import com.maxmind.minfraud.request.Shipping;
57
import com.maxmind.minfraud.request.Transaction;
68
import com.maxmind.minfraud.response.FactorsResponse;
79
import com.maxmind.minfraud.response.InsightsResponse;
@@ -14,6 +16,8 @@
1416
import org.junit.runner.RunWith;
1517
import org.skyscreamer.jsonassert.JSONAssert;
1618

19+
import java.net.InetAddress;
20+
1721
import static com.github.tomakehurst.wiremock.client.WireMock.*;
1822
import static com.jcabi.matchers.RegexMatchers.matchesPattern;
1923
import static com.maxmind.minfraud.request.RequestTestHelper.*;
@@ -70,6 +74,23 @@ public void testFullFactorsTransaction() throws Exception {
7074
}
7175
}
7276

77+
@Test
78+
public void testRequestEncoding() throws Exception {
79+
try (WebServiceClient client = createSuccessClient("insights", "{}")) {
80+
Transaction request = new Transaction.Builder(
81+
new Device.Builder(InetAddress.getByName("1.1.1.1")).build()
82+
).shipping(
83+
new Shipping.Builder()
84+
.firstName("Allan dias á s maia")
85+
.build()
86+
).build();
87+
client.insights(request);
88+
89+
verify(postRequestedFor(urlMatching("/minfraud/v2.0/insights"))
90+
.withRequestBody(equalToJson("{\"device\":{\"ip_address\":\"1.1.1.1\"},\"shipping\":{\"first_name\":\"Allan dias á s maia\"}}")));
91+
}
92+
}
93+
7394
@Test
7495
public void test200WithNoBody() throws Exception {
7596
try (WebServiceClient client = createSuccessClient("insights", "")) {

src/test/java/com/maxmind/minfraud/request/RequestTestHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,6 @@ public static void verifyRequestFor(String service) throws IOException, URISynta
128128

129129
verify(postRequestedFor(urlMatching("/minfraud/v2.0/" + service))
130130
.withRequestBody(equalToJson(requestBody))
131-
.withHeader("Content-Type", matching("application/json")));
131+
.withHeader("Content-Type", matching("application/json; charset=UTF-8")));
132132
}
133133
}

0 commit comments

Comments
 (0)