Skip to content
This repository was archived by the owner on May 14, 2020. It is now read-only.

Commit c3d0329

Browse files
committed
Merge branch 'develop'
2 parents cc443d8 + e45261d commit c3d0329

File tree

7 files changed

+153
-9
lines changed

7 files changed

+153
-9
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
### 1.6.2 (April 14, 2016)
2+
* @sphinks fixed wrong header names in WebHooks class (#20)
3+
* minimal Java version changed to 1.7
4+
5+
16
### 1.6.1 (February 17, 2016)
27
* Fixed [issue](https://github.com/helpscout/helpscout-api-java/pull/19) regarding `cc`, `bcc` and `to` fields in `Conversation` and `AbstractThread` classes
38

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ Help Scout Java Wrapper
22
=======================
33
Java Wrapper for the Help Scout API. More information can be found on our [developer site](http://developer.helpscout.net).
44

5-
Version 1.6.1 Released
6-
---------------------
5+
Version 1.6.2 Released
6+
----------------------
77
Please see the [Changelog](https://github.com/helpscout/helpscout-api-java/blob/master/CHANGELOG.md) for details.
88

99
Requirements
1010
---------------------
11-
* Java 1.6 or higher
11+
* Java 1.7 or higher
1212
* [Commons Codec](http://commons.apache.org/proper/commons-codec/)
1313
* [Commons Lang](http://commons.apache.org/proper/commons-lang/)
1414
* [Commons IO](http://commons.apache.org/proper/commons-io/)

pom.xml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>net.helpscout</groupId>
55
<artifactId>helpscout-api</artifactId>
66
<packaging>jar</packaging>
7-
<version>1.6.0</version>
7+
<version>1.6.2</version>
88
<name>helpscout-api</name>
99
<url>http://maven.apache.org</url>
1010

@@ -83,6 +83,12 @@
8383
<version>1.3</version>
8484
<scope>test</scope>
8585
</dependency>
86+
<dependency>
87+
<groupId>org.mockito</groupId>
88+
<artifactId>mockito-core</artifactId>
89+
<version>1.10.19</version>
90+
<scope>test</scope>
91+
</dependency>
8692
</dependencies>
8793
<repositories>
8894
<repository>
@@ -120,8 +126,8 @@
120126
<artifactId>maven-compiler-plugin</artifactId>
121127
<version>3.3</version>
122128
<configuration>
123-
<source>1.6</source>
124-
<target>1.6</target>
129+
<source>1.7</source>
130+
<target>1.7</target>
125131
</configuration>
126132
</plugin>
127133
</plugins>

src/main/java/net/helpscout/api/ApiClient.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1732,7 +1732,10 @@ private void checkStatusCode(HttpURLConnection conn, int expectedCode) throws Ap
17321732

17331733
private String getDetailedErrorMessage(HttpURLConnection conn) throws IOException {
17341734
InputStream is = conn.getErrorStream();
1735-
String json = IOUtils.toString(is, "UTF-8");
1735+
String json = "";
1736+
if (is != null) {
1737+
json = IOUtils.toString(is, "UTF-8");
1738+
}
17361739

17371740
return StringUtils.isNotEmpty(json) ? new JsonFormatter().format(json) : null;
17381741
}

src/main/java/net/helpscout/api/Webhook.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ private String getHeader(String headerName) {
3131
* @return string
3232
*/
3333
public String getEventType() {
34-
return this.getHeader("HTTP_X_HELPSCOUT_EVENT");
34+
return this.getHeader("X-HELPSCOUT-EVENT");
3535
}
3636

3737
public boolean isTestEvent() {
@@ -70,7 +70,7 @@ public boolean isValid() {
7070
String computed = generateSignature();
7171

7272
if (computed != null) {
73-
return computed.equals(getHeader("HTTP_X_HELPSCOUT_SIGNATURE"));
73+
return computed.equals(getHeader("X-HELPSCOUT-SIGNATURE"));
7474
}
7575
return false;
7676
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package net.helpscout.api;
2+
3+
import lombok.SneakyThrows;
4+
import net.helpscout.api.model.Customer;
5+
import org.junit.Test;
6+
7+
import javax.servlet.http.HttpServletRequest;
8+
import java.io.BufferedReader;
9+
import java.io.StringReader;
10+
import java.nio.file.Files;
11+
import java.nio.file.Path;
12+
import java.nio.file.Paths;
13+
14+
import static org.hamcrest.Matchers.equalTo;
15+
import static org.junit.Assert.assertThat;
16+
import static org.junit.Assert.assertTrue;
17+
import static org.mockito.Mockito.*;
18+
19+
/**
20+
* @Author: ivan
21+
* Date: 22.03.16
22+
* Time: 0:33
23+
*/
24+
public class WebhookApiTest {
25+
26+
27+
/**
28+
* Real webhook request contains such kind of headers:
29+
*
30+
* host: youcallback.domain.com
31+
* user-agent: Help Scout/Webhooks
32+
* accept-encoding: UTF-8
33+
* content-type: application/json
34+
* x-helpscout-event: customer.created
35+
* x-helpscout-signature: qALfoJFZ/WVbevIxtFYKHJ86D8o=
36+
* x-forwarded-proto: http
37+
* x-forwarded-port: 80
38+
* x-request-start: t=1458595138602884
39+
* x-client-ip: 000.000.000.000
40+
* content-length: 406
41+
* x-forwarded-host: youcallback.domain.com
42+
* x-forwarded-server: youcallback.domain.com
43+
* connection: Keep-Alive
44+
*/
45+
@Test
46+
public void basicTestForCustomEventHeader() {
47+
48+
HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
49+
50+
when(httpServletRequest.getHeader("X-HELPSCOUT-EVENT")).thenReturn("customer.created");
51+
52+
Webhook webhook = new Webhook("SecretKey", httpServletRequest);
53+
54+
assertThat(webhook.getEventType(), equalTo("customer.created"));
55+
assertTrue(webhook.isCustomerEvent());
56+
}
57+
58+
/**
59+
* Calculating of signature performed based on JSON data and compared to
60+
* data in header 'x-helpscout-signature'.
61+
* At file 'webhook_customer' located example structure of json send to webhook.
62+
*/
63+
@Test
64+
@SneakyThrows
65+
public void basicTestForCheckIfRequestIsValid() {
66+
67+
HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
68+
BufferedReader bufferedReader = new BufferedReader(new StringReader(readJsonDataToString("webhook_customer")));
69+
70+
when(httpServletRequest.getReader()).thenReturn(bufferedReader);
71+
when(httpServletRequest.getHeader("x-helpscout-signature".toUpperCase())).thenReturn("gV91IzHpvzCSLYW+/QGAxfm7KOM=");
72+
73+
Webhook webhook = new Webhook("SecretKey", httpServletRequest);
74+
75+
assertTrue(webhook.isValid());
76+
}
77+
78+
/**
79+
* Simple test for getting customer object from Webhook class via JSON data
80+
*/
81+
@Test
82+
@SneakyThrows
83+
public void basicTestForReadingJsonData() {
84+
85+
HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
86+
BufferedReader bufferedReader = new BufferedReader(new StringReader(readJsonDataToString("webhook_customer")));
87+
88+
when(httpServletRequest.getReader()).thenReturn(bufferedReader);
89+
90+
Webhook webhook = new Webhook("SecretKey", httpServletRequest);
91+
Customer customer = webhook.getCustomer();
92+
93+
assertThat(customer.getFirstName(), equalTo("First_Name"));
94+
assertThat(customer.getLastName(), equalTo("Last_Name"));
95+
assertThat(customer.getEmails().get(0).getValue(), equalTo("[email protected]"));
96+
}
97+
98+
@SneakyThrows
99+
private static String readJsonDataToString(String jsonFileName) {
100+
Path pathToJsonData = Paths.get(ClassLoader.getSystemResource("responses/" + jsonFileName + ".json").toURI());
101+
return new String(Files.readAllBytes(pathToJsonData), "UTF-8");
102+
}
103+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"lastName":"Last_Name",
3+
"photoType":null,
4+
"modifiedAt":"2016-03-21T21:18:58Z",
5+
"location":null,
6+
"emails":[
7+
{
8+
"id": 98037223,
9+
"location": "work",
10+
"value": "[email protected]"
11+
}
12+
],
13+
"websites":null,
14+
"phones":null,
15+
"id":76688730,
16+
"organization":null,
17+
"address":null,
18+
"createdAt":"2016-03-21T21:18:58Z",
19+
"background":null,
20+
"age":null,
21+
"gender":null,
22+
"socialProfiles":null,
23+
"firstName":"First_Name",
24+
"photoUrl":null,
25+
"chats":null,
26+
"jobTitle":null
27+
}

0 commit comments

Comments
 (0)