1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xsolla\SDK\Tests\Integration\Webhook; |
4
|
|
|
|
5
|
|
|
use Guzzle\Common\Event; |
6
|
|
|
use Guzzle\Http\Client; |
7
|
|
|
use Guzzle\Http\Exception\BadResponseException; |
8
|
|
|
use Symfony\Component\Process\Process; |
9
|
|
|
use Xsolla\SDK\API\XsollaClient; |
10
|
|
|
use Xsolla\SDK\Version; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @group webhook |
14
|
|
|
*/ |
15
|
|
|
class ServerTest extends \PHPUnit_Framework_TestCase |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var Process |
19
|
|
|
*/ |
20
|
|
|
protected static $process; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Client |
24
|
|
|
*/ |
25
|
|
|
protected $guzzleClient; |
26
|
|
|
|
27
|
|
|
public static function setUpBeforeClass() |
28
|
|
|
{ |
29
|
|
|
self::$process = new Process('php -S [::1]:8999', __DIR__.'/../../resources'); |
30
|
|
|
self::$process->setTimeout(1); |
31
|
|
|
self::$process->start(); |
32
|
|
|
usleep(100000); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public static function tearDownAfterClass() |
36
|
|
|
{ |
37
|
|
|
self::$process->stop(0); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function setUp() |
41
|
|
|
{ |
42
|
|
|
$this->guzzleClient = new Client('http://[::1]:8999'); |
43
|
|
|
global $argv; |
|
|
|
|
44
|
|
View Code Duplication |
if (in_array('--debug', $argv, true)) { |
|
|
|
|
45
|
|
|
$echoCb = function (Event $event) { |
46
|
|
|
echo (string) $event['request'].PHP_EOL; |
47
|
|
|
echo (string) $event['response'].PHP_EOL; |
48
|
|
|
}; |
49
|
|
|
$this->guzzleClient->getEventDispatcher()->addListener('request.complete', $echoCb); |
50
|
|
|
$this->guzzleClient->getEventDispatcher()->addListener('request.exception', $echoCb); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @dataProvider cbProvider |
56
|
|
|
*/ |
57
|
|
|
public function testResponse( |
58
|
|
|
$expectedStatusCode, |
59
|
|
|
$expectedResponseContent, |
60
|
|
|
$request, |
61
|
|
|
$testCase, |
62
|
|
|
$testHeaders |
63
|
|
|
) { |
64
|
|
|
$signature = sha1($request.ServerMock::PROJECT_SECRET_KEY); |
65
|
|
|
$headers = null; |
|
|
|
|
66
|
|
|
if ($testHeaders) { |
67
|
|
|
$headers = $testHeaders; |
68
|
|
|
} else { |
69
|
|
|
$headers = array('Authorization' => 'Signature '.$signature); |
70
|
|
|
} |
71
|
|
|
$request = $this->guzzleClient->post('/webhook_server.php?test_case='.$testCase, $headers, $request); |
72
|
|
|
try { |
73
|
|
|
$response = $request->send(); |
74
|
|
|
} catch (BadResponseException $e) { |
75
|
|
|
$response = $e->getResponse(); |
76
|
|
|
} |
77
|
|
|
static::assertSame($expectedResponseContent, $response->getBody(true)); |
78
|
|
|
static::assertSame($expectedStatusCode, $response->getStatusCode()); |
79
|
|
|
static::assertArrayHasKey('x-xsolla-sdk', $response->getHeaders()); |
80
|
|
|
static::assertSame(Version::getVersion(), (string) $response->getHeader('x-xsolla-sdk')); |
81
|
|
|
static::assertArrayHasKey('content-type', $response->getHeaders()); |
82
|
|
|
if (204 === $response->getStatusCode()) { |
83
|
|
|
static::assertStringStartsWith('text/plain', (string) $response->getHeader('content-type')); |
84
|
|
|
} else { |
85
|
|
|
static::assertStringStartsWith('application/json', (string) $response->getHeader('content-type')); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function cbProvider() |
90
|
|
|
{ |
91
|
|
|
return array( |
92
|
|
|
array( |
93
|
|
|
'expectedStatusCode' => 204, |
94
|
|
|
'expectedResponseContent' => '', |
95
|
|
|
'request' => '{"notification_type": "payment"}', |
96
|
|
|
'testCase' => 'success', |
97
|
|
|
'testHeaders' => null, |
98
|
|
|
), |
99
|
|
|
array( |
100
|
|
|
'expectedStatusCode' => 422, |
101
|
|
|
'expectedResponseContent' => XsollaClient::jsonEncode( |
102
|
|
|
array( |
103
|
|
|
'error' => array( |
104
|
|
|
'code' => 'INVALID_PARAMETER', |
105
|
|
|
'message' => 'notification_type key not found in Xsolla webhook request', |
106
|
|
|
), |
107
|
|
|
) |
108
|
|
|
), |
109
|
|
|
'request' => '{"foo": "bar"}', |
110
|
|
|
'testCase' => 'empty_request', |
111
|
|
|
'testHeaders' => null, |
112
|
|
|
), |
113
|
|
|
array( |
114
|
|
|
'expectedStatusCode' => 422, |
115
|
|
|
'expectedResponseContent' => XsollaClient::jsonEncode( |
116
|
|
|
array( |
117
|
|
|
'error' => array( |
118
|
|
|
'code' => 'INVALID_PARAMETER', |
119
|
|
|
'message' => 'Unknown notification_type in Xsolla webhook request: unknown', |
120
|
|
|
), |
121
|
|
|
) |
122
|
|
|
), |
123
|
|
|
'request' => '{"notification_type": "unknown"}', |
124
|
|
|
'testCase' => 'unknown_notification_type', |
125
|
|
|
'testHeaders' => null, |
126
|
|
|
), |
127
|
|
|
array( |
128
|
|
|
'expectedStatusCode' => 401, |
129
|
|
|
'expectedResponseContent' => XsollaClient::jsonEncode( |
130
|
|
|
array( |
131
|
|
|
'error' => array( |
132
|
|
|
'code' => 'INVALID_SIGNATURE', |
133
|
|
|
'message' => 'Invalid Signature. Signature provided in "Authorization" header (78143a5ac4b892a68ce8b0b8b49e26667db0fa00) does not match with expected', |
134
|
|
|
), |
135
|
|
|
) |
136
|
|
|
), |
137
|
|
|
'request' => null, |
138
|
|
|
'testCase' => 'invalid_signature', |
139
|
|
|
'testHeaders' => array('Authorization' => 'Signature 78143a5ac4b892a68ce8b0b8b49e26667db0fa00'), |
140
|
|
|
), |
141
|
|
|
array( |
142
|
|
|
'expectedStatusCode' => 401, |
143
|
|
|
'expectedResponseContent' => XsollaClient::jsonEncode( |
144
|
|
|
array( |
145
|
|
|
'error' => array( |
146
|
|
|
'code' => 'INVALID_SIGNATURE', |
147
|
|
|
'message' => '"Authorization" header not found in Xsolla webhook request. Please check troubleshooting section in README.md https://github.com/xsolla/xsolla-sdk-php#troubleshooting', |
148
|
|
|
), |
149
|
|
|
) |
150
|
|
|
), |
151
|
|
|
'request' => null, |
152
|
|
|
'testCase' => 'authorization_header_not_found', |
153
|
|
|
'testHeaders' => array('foo' => 'bar'), |
154
|
|
|
), |
155
|
|
|
array( |
156
|
|
|
'expectedStatusCode' => 401, |
157
|
|
|
'expectedResponseContent' => XsollaClient::jsonEncode( |
158
|
|
|
array( |
159
|
|
|
'error' => array( |
160
|
|
|
'code' => 'INVALID_SIGNATURE', |
161
|
|
|
'message' => 'Signature not found in "Authorization" header from Xsolla webhook request: INVALID_FORMAT', |
162
|
|
|
), |
163
|
|
|
) |
164
|
|
|
), |
165
|
|
|
'request' => null, |
166
|
|
|
'testCase' => 'invalid_signature_format', |
167
|
|
|
'testHeaders' => array('Authorization' => 'INVALID_FORMAT'), |
168
|
|
|
), |
169
|
|
|
array( |
170
|
|
|
'expectedStatusCode' => 422, |
171
|
|
|
'expectedResponseContent' => XsollaClient::jsonEncode( |
172
|
|
|
array( |
173
|
|
|
'error' => array( |
174
|
|
|
'code' => 'INVALID_PARAMETER', |
175
|
|
|
'message' => 'Unable to parse Xsolla webhook request into JSON: Syntax error.', |
176
|
|
|
), |
177
|
|
|
) |
178
|
|
|
), |
179
|
|
|
'request' => 'INVALID_REQUEST_CONTENT', |
180
|
|
|
'testCase' => 'invalid_request_content', |
181
|
|
|
'testHeaders' => null, |
182
|
|
|
), |
183
|
|
|
array( |
184
|
|
|
'expectedStatusCode' => 401, |
185
|
|
|
'expectedResponseContent' => XsollaClient::jsonEncode( |
186
|
|
|
array( |
187
|
|
|
'error' => array( |
188
|
|
|
'code' => 'INVALID_CLIENT_IP', |
189
|
|
|
'message' => 'Client IP address (::1) not found in allowed IP addresses whitelist (159.255.220.240/28, 185.30.20.16/29, 185.30.21.0/24, 185.30.21.16/29). Please check troubleshooting section in README.md https://github.com/xsolla/xsolla-sdk-php#troubleshooting', |
190
|
|
|
), |
191
|
|
|
) |
192
|
|
|
), |
193
|
|
|
'request' => null, |
194
|
|
|
'testCase' => 'invalid_ip', |
195
|
|
|
'testHeaders' => null, |
196
|
|
|
), |
197
|
|
|
array( |
198
|
|
|
'expectedStatusCode' => 500, |
199
|
|
|
'expectedResponseContent' => XsollaClient::jsonEncode( |
200
|
|
|
array( |
201
|
|
|
'error' => array( |
202
|
|
|
'code' => 'SERVER_ERROR', |
203
|
|
|
'message' => 'callback_server_error', |
204
|
|
|
), |
205
|
|
|
) |
206
|
|
|
), |
207
|
|
|
'request' => '{"notification_type": "payment"}', |
208
|
|
|
'testCase' => 'callback_server_error', |
209
|
|
|
'testHeaders' => null, |
210
|
|
|
), |
211
|
|
|
array( |
212
|
|
|
'expectedStatusCode' => 400, |
213
|
|
|
'expectedResponseContent' => XsollaClient::jsonEncode( |
214
|
|
|
array( |
215
|
|
|
'error' => array( |
216
|
|
|
'code' => 'CLIENT_ERROR', |
217
|
|
|
'message' => 'callback_client_error', |
218
|
|
|
), |
219
|
|
|
) |
220
|
|
|
), |
221
|
|
|
'request' => '{"notification_type": "payment"}', |
222
|
|
|
'testCase' => 'callback_client_error', |
223
|
|
|
'testHeaders' => null, |
224
|
|
|
), |
225
|
|
|
); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state