Skip to content

Commit b5a423e

Browse files
committed
Change encoding format and introduce additional integration tests
1 parent 9b3fe67 commit b5a423e

File tree

3 files changed

+89
-6
lines changed

3 files changed

+89
-6
lines changed

src/Requests/Payload.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,27 @@ protected function generateFromPsr7Request(Psr7Request $request) : string
4444
$timestamp = isset($this->request->getHeader('X-SIGNED-TIMESTAMP')[0]) ?
4545
$this->request->getHeader('X-SIGNED-TIMESTAMP')[0] : '';
4646

47+
$string = json_decode((string) $this->request->getBody());
48+
49+
if (is_null($string)) {
50+
return json_encode([
51+
'id' => (string)$id,
52+
'method' => strtoupper($this->request->getMethod()),
53+
'timestamp' => $timestamp,
54+
'uri' => rtrim((string)$this->request->getUri(), '/'),
55+
'content' => (string) $this->request->getBody()
56+
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
57+
}
58+
4759
return json_encode([
4860
'id' => (string) $id,
4961
'method' => strtoupper($this->request->getMethod()),
5062
'timestamp' => $timestamp,
51-
'uri' => (string) $this->request->getUri(),
52-
'content' => $this->request->getBody()->getContents()
63+
'uri' => rtrim((string) $this->request->getUri(), '/'),
64+
'content' => json_encode(
65+
json_decode((string) $this->request->getBody()),
66+
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
67+
)
5368
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
5469
}
5570

tests/ClientTest.php

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public function it_generates_a_signature_with_a_simple_json_payload_containing_
120120
$this->assertTrue($request->hasHeader('Algorithm'));
121121
$this->assertTrue($request->hasHeader('Signature'));
122122
$this->assertSame(
123-
'bf0f2eb48acf86cf72a87b48393f71fb2eebbb2c11fa0d838cbb127d74a0a00e',
123+
'd35d92484222fce7e5c194381e5f53342caae6fa626cd61e3431bddc549b34e1',
124124
$request->getHeader('Signature')[0]
125125
);
126126
});
@@ -143,7 +143,7 @@ public function it_generates_a_signature_with_a_simple_json_payload_containing_
143143
$this->assertTrue($request->hasHeader('Algorithm'));
144144
$this->assertTrue($request->hasHeader('Signature'));
145145
$this->assertSame(
146-
'10b165e59775d1a564be49046edd60137d40fcecebbdf59f41b01568ca07db63',
146+
'65ff94dce4894eb306a76ff0d397ec264b1c4980b57afbc3dd9526af242d239b',
147147
$request->getHeader('Signature')[0]
148148
);
149149
});
@@ -166,11 +166,79 @@ public function it_generates_a_signature_with_a_simple_json_payload_containing_a
166166
$this->assertTrue($request->hasHeader('Algorithm'));
167167
$this->assertTrue($request->hasHeader('Signature'));
168168
$this->assertSame(
169-
'8c36d7384111d27336c410ebfec38c7da2eca9ec4779216f9cb8f921a08c4572',
169+
'ebd68bfe7ed51c050fb92db098946cd21b7b23be6f682360a5e893840a1dc52f',
170170
$request->getHeader('Signature')[0]
171171
);
172172
});
173173

174174
$this->client->post($uri, ['json' => ['test' => $uri]]);
175175
}
176+
177+
/**
178+
* @test
179+
*/
180+
public function it_generates_a_signature_with_a_complex_json_payload()
181+
{
182+
Carbon::setTestNow('2001-01-01 00:00:00');
183+
$this->expectUuid4('303103f5-3dca-4704-96ad-860717769ec9');
184+
185+
$uri = 'https://localhost/poop';
186+
187+
$this->handler->expects('POST', $uri)
188+
->inspectRequest(function ($request) use ($uri) {
189+
$this->assertTrue($request->hasHeader('Algorithm'));
190+
$this->assertTrue($request->hasHeader('Signature'));
191+
$this->assertSame(
192+
'0c3f0c81ba1fa3df9d3e0a1d72c4d491125153c0dea8355b6d48fe7ef1a4dacc',
193+
$request->getHeader('Signature')[0]
194+
);
195+
});
196+
197+
$this->client->post(
198+
$uri,
199+
[
200+
'json' => [
201+
'users' => [
202+
['id' => 1, 'name' => 'Chris Hayes', 'email' => '[email protected]'],
203+
['id' => 2, 'name' => 'Jaspaul Bola', 'email' => '[email protected]'],
204+
['id' => 3, 'name' => 'Mr Penã 💩', 'email' => 'Mr-Penã@soapboxhq.com']
205+
]
206+
]
207+
]
208+
);
209+
}
210+
211+
/**
212+
* @test
213+
*/
214+
public function it_generates_a_signature_with_a_complex_json_payload_after_stripping_the_trailing_slash()
215+
{
216+
Carbon::setTestNow('2001-01-01 00:00:00');
217+
$this->expectUuid4('303103f5-3dca-4704-96ad-860717769ec9');
218+
219+
$uri = 'https://localhost/poop/';
220+
221+
$this->handler->expects('POST', $uri)
222+
->inspectRequest(function ($request) use ($uri) {
223+
$this->assertTrue($request->hasHeader('Algorithm'));
224+
$this->assertTrue($request->hasHeader('Signature'));
225+
$this->assertSame(
226+
'0c3f0c81ba1fa3df9d3e0a1d72c4d491125153c0dea8355b6d48fe7ef1a4dacc',
227+
$request->getHeader('Signature')[0]
228+
);
229+
});
230+
231+
$this->client->post(
232+
$uri,
233+
[
234+
'json' => [
235+
'users' => [
236+
['id' => 1, 'name' => 'Chris Hayes', 'email' => '[email protected]'],
237+
['id' => 2, 'name' => 'Jaspaul Bola', 'email' => '[email protected]'],
238+
['id' => 3, 'name' => 'Mr Penã 💩', 'email' => 'Mr-Penã@soapboxhq.com']
239+
]
240+
]
241+
]
242+
);
243+
}
176244
}

tests/Requests/PayloadTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ public function it_stringifies_a_json_payload_with_a_url_to_a_string()
284284
->withHeader('X-SIGNED-ID', '303103f5-3dca-4704-96ad-860717769ec9')
285285
->withHeader('X-SIGNED-TIMESTAMP', '2018-04-06 20:34:47');
286286

287-
$expected = '{"id":"303103f5-3dca-4704-96ad-860717769ec9","method":"GET","timestamp":"2018-04-06 20:34:47","uri":"https://localhost","content":"{\"url\":\"https:\\\\/\\\\/google.com\"}"}';
287+
$expected = '{"id":"303103f5-3dca-4704-96ad-860717769ec9","method":"GET","timestamp":"2018-04-06 20:34:47","uri":"https://localhost","content":"{\"url\":\"https://google.com\"}"}';
288288

289289
$this->assertEquals($expected, (string) new Payload($request));
290290
}

0 commit comments

Comments
 (0)