-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathHookTest.java
More file actions
185 lines (153 loc) · 5.95 KB
/
Copy pathHookTest.java
File metadata and controls
185 lines (153 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package com.easypost;
import com.easypost.exception.API.NotFoundError;
import com.easypost.exception.EasyPostException;
import com.easypost.hooks.RequestHookResponses;
import com.easypost.hooks.ResponseHookResponses;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.util.function.Function;
public class HookTest {
private static TestUtils.VCR vcr;
private static boolean hookHit = false;
/**
* Set up the testing environment for this file.
*
* @throws EasyPostException when the request fails.
*/
@BeforeAll
public static void setup() throws EasyPostException {
vcr = new TestUtils.VCR("hook", TestUtils.ApiKey.TEST);
}
/**
* Test failing a hook if we subscribed to a request hook.
*
* @param data The RequestHookResponses object representing the hook data.
* @return The result of the test.
*/
public static Object failIfSubscribedToRequest(RequestHookResponses data) {
fail("Test failed");
return false;
}
/**
* Test failing a hook if we subscribed to a response hook.
*
* @param data The ResponseHookResponses object representing the hook data.
* @return The result of the test.
*/
public static Object failIfSubscribedToResponse(ResponseHookResponses data) {
fail("Test failed");
return false;
}
/**
* Test subscribing a request hook.
*
* @param data The RequestHookResponses object representing the hook data.
* @return The result of the test.
*/
public static Object testRequestHooks(RequestHookResponses data) {
assertEquals("https://api.easypost.com/v2/parcels", data.getPath());
assertEquals("POST", data.getMethod());
assertNotNull(data.getHeaders());
assertNotNull(data.getRequestBody());
assertNotNull(data.getRequestTimestamp());
assertNotNull(data.getRequestUuid());
return true;
}
/**
* Test subscribing a response hook when an HTTP error occurs.
*
* @param data The ResponseHookResponses object representing the hook data.
* @return The result of the test.
*/
public static Object testResponseHookOnHttpError(ResponseHookResponses data) {
assertEquals("https://api.easypost.com/v2/parcels/par_123", data.getPath());
assertEquals("GET", data.getMethod());
assertEquals(404, data.getHttpStatus());
assertNotNull(data.getHeaders());
assertNotNull(data.getResponseBody());
assertNotNull(data.getRequestTimestamp());
assertNotNull(data.getRequestTimestamp());
assertNotNull(data.getRequestUuid());
hookHit = true;
return true;
}
/**
* Test subscribing a response hook.
*
* @param data The ResponseHookResponses object representing the hook data.
* @return The result of the test.
*/
public static Object testResponseHooks(ResponseHookResponses data) {
assertEquals("https://api.easypost.com/v2/parcels", data.getPath());
assertEquals("POST", data.getMethod());
assertEquals(201, data.getHttpStatus());
assertNotNull(data.getHeaders());
assertNotNull(data.getResponseBody());
assertNotNull(data.getRequestTimestamp());
assertNotNull(data.getRequestTimestamp());
assertNotNull(data.getRequestUuid());
return true;
}
/**
* Test creating a Parcel with request hook subscribed.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testCreateParcelWithRequestHook() throws EasyPostException {
vcr.setUpTest("create");
Function<RequestHookResponses, Object> requestHook = HookTest::testRequestHooks;
vcr.client.subscribeToRequestHook(requestHook);
vcr.client.parcel.create(Fixtures.basicParcel());
}
/**
* Test creating a Parcel with response hook subscribed.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testCreateParcelWithResponseHook() throws EasyPostException {
vcr.setUpTest("create");
Function<ResponseHookResponses, Object> requestHook = HookTest::testResponseHooks;
vcr.client.subscribeToResponseHook(requestHook);
vcr.client.parcel.create(Fixtures.basicParcel());
}
/**
* Test creating a Parcel with unsubscribed hooks.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testUnsubscribeHooks() throws EasyPostException {
vcr.setUpTest("create");
Function<RequestHookResponses, Object> failedRequestHook = HookTest::failIfSubscribedToRequest;
vcr.client.subscribeToRequestHook(failedRequestHook);
vcr.client.unsubscribeFromRequestHook(failedRequestHook);
Function<ResponseHookResponses, Object> failedResponseHook = HookTest::failIfSubscribedToResponse;
vcr.client.subscribeToResponseHook(failedResponseHook);
vcr.client.unsubscribeFromResponseHook(failedResponseHook);
vcr.client.parcel.create(Fixtures.basicParcel());
}
/**
* Test that response hooks are still fired even if the HTTP call fails.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testResponseHookFiredOnHTTPError() throws EasyPostException {
vcr.setUpTest("http_error");
hookHit = false;
Function<ResponseHookResponses, Object> requestHook = HookTest::testResponseHookOnHttpError;
vcr.client.subscribeToResponseHook(requestHook);
try {
vcr.client.parcel.retrieve("par_123");
} catch (NotFoundError e) {
assertEquals(404, e.getStatusCode());
}
assertTrue(hookHit);
}
}