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

Commit 7ac72e2

Browse files
sphinksbob983
authored andcommitted
Fix incorrect header names in Webhook class (#20)
Fix minor problems in webhook class - correct name of custom 'help-scout' headers - add mockito to test harness - create small unit test for checking of event header - fix NPE in case ErrorStream is null
1 parent 88d3456 commit 7ac72e2

File tree

5 files changed

+142
-3
lines changed

5 files changed

+142
-3
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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>

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)