Skip to content

Commit a3dfd66

Browse files
committed
Updates the encoding to not escape unicode characters
This changes the way we encode content for the signed request. The payload will now do a better job of not changing the content it is provided. Additional tests were added to demonstrate how the signature generation works.
1 parent f28cf8a commit a3dfd66

File tree

2 files changed

+113
-1
lines changed

2 files changed

+113
-1
lines changed

src/Requests/Payload.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ protected function generateFromPsr7Request(Psr7Request $request) : string
5050
'timestamp' => $timestamp,
5151
'uri' => (string) $this->request->getUri(),
5252
'content' => $this->request->getBody()->getContents()
53-
], JSON_UNESCAPED_SLASHES);
53+
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
5454
}
5555

5656
/**

tests/Requests/PayloadTest.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,116 @@ public function it_translates_non_requests_to_an_empty_string()
110110
{
111111
$this->assertEquals('', (string) new Payload(null));
112112
}
113+
114+
/**
115+
* @test
116+
*/
117+
public function it_stringifies_a_simple_payload_to_a_string()
118+
{
119+
$request = (new GuzzleRequest('GET', 'https://localhost', [], 'content'))
120+
->withHeader('X-SIGNED-ID', '303103f5-3dca-4704-96ad-860717769ec9')
121+
->withHeader('X-SIGNED-TIMESTAMP', '2018-04-06 20:34:47');
122+
123+
$expected = '{"id":"303103f5-3dca-4704-96ad-860717769ec9","method":"GET","timestamp":"2018-04-06 20:34:47","uri":"https://localhost","content":"content"}';
124+
125+
$this->assertEquals($expected, (string) new Payload($request));
126+
}
127+
128+
/**
129+
* @test
130+
*/
131+
public function it_stringifies_a_payload_with_an_embedded_url_to_a_string()
132+
{
133+
$request = (new GuzzleRequest('GET', 'https://localhost', [], 'https://google.com'))
134+
->withHeader('X-SIGNED-ID', '303103f5-3dca-4704-96ad-860717769ec9')
135+
->withHeader('X-SIGNED-TIMESTAMP', '2018-04-06 20:34:47');
136+
137+
$expected = '{"id":"303103f5-3dca-4704-96ad-860717769ec9","method":"GET","timestamp":"2018-04-06 20:34:47","uri":"https://localhost","content":"https://google.com"}';
138+
139+
$this->assertEquals($expected, (string) new Payload($request));
140+
}
141+
142+
/**
143+
* @test
144+
*/
145+
public function it_stringifies_a_payload_with_the_ã_character_to_use_the_escaped_string()
146+
{
147+
$request = (new GuzzleRequest('GET', 'https://localhost', [], 'ã'))
148+
->withHeader('X-SIGNED-ID', '303103f5-3dca-4704-96ad-860717769ec9')
149+
->withHeader('X-SIGNED-TIMESTAMP', '2018-04-06 20:34:47');
150+
151+
$expected = '{"id":"303103f5-3dca-4704-96ad-860717769ec9","method":"GET","timestamp":"2018-04-06 20:34:47","uri":"https://localhost","content":"ã"}';
152+
153+
$this->assertEquals($expected, (string) new Payload($request));
154+
}
155+
156+
/**
157+
* @test
158+
*/
159+
public function it_stringifies_a_payload_with_the_好_character_to_use_the_escaped_string()
160+
{
161+
$request = (new GuzzleRequest('GET', 'https://localhost', [], ''))
162+
->withHeader('X-SIGNED-ID', '303103f5-3dca-4704-96ad-860717769ec9')
163+
->withHeader('X-SIGNED-TIMESTAMP', '2018-04-06 20:34:47');
164+
165+
$expected = '{"id":"303103f5-3dca-4704-96ad-860717769ec9","method":"GET","timestamp":"2018-04-06 20:34:47","uri":"https://localhost","content":"好"}';
166+
167+
$this->assertEquals($expected, (string) new Payload($request));
168+
}
169+
170+
/**
171+
* @test
172+
*/
173+
public function it_stringifies_a_json_payload_to_a_string()
174+
{
175+
$request = (new GuzzleRequest('GET', 'https://localhost', [], json_encode(['test' => 'test'])))
176+
->withHeader('X-SIGNED-ID', '303103f5-3dca-4704-96ad-860717769ec9')
177+
->withHeader('X-SIGNED-TIMESTAMP', '2018-04-06 20:34:47');
178+
179+
$expected = '{"id":"303103f5-3dca-4704-96ad-860717769ec9","method":"GET","timestamp":"2018-04-06 20:34:47","uri":"https://localhost","content":"{\"test\":\"test\"}"}';
180+
181+
$this->assertEquals($expected, (string) new Payload($request));
182+
}
183+
184+
/**
185+
* @test
186+
*/
187+
public function it_stringifies_a_json_payload_with_the_ã_character_to_a_string()
188+
{
189+
$request = (new GuzzleRequest('GET', 'https://localhost', [], json_encode(['ã' => 'ã'], JSON_UNESCAPED_UNICODE)))
190+
->withHeader('X-SIGNED-ID', '303103f5-3dca-4704-96ad-860717769ec9')
191+
->withHeader('X-SIGNED-TIMESTAMP', '2018-04-06 20:34:47');
192+
193+
$expected = '{"id":"303103f5-3dca-4704-96ad-860717769ec9","method":"GET","timestamp":"2018-04-06 20:34:47","uri":"https://localhost","content":"{\"ã\":\"ã\"}"}';
194+
195+
$this->assertEquals($expected, (string) new Payload($request));
196+
}
197+
198+
/**
199+
* @test
200+
*/
201+
public function it_stringifies_a_json_payload_with_the_好_character_to_a_string()
202+
{
203+
$request = (new GuzzleRequest('GET', 'https://localhost', [], json_encode(['' => ''], JSON_UNESCAPED_UNICODE)))
204+
->withHeader('X-SIGNED-ID', '303103f5-3dca-4704-96ad-860717769ec9')
205+
->withHeader('X-SIGNED-TIMESTAMP', '2018-04-06 20:34:47');
206+
207+
$expected = '{"id":"303103f5-3dca-4704-96ad-860717769ec9","method":"GET","timestamp":"2018-04-06 20:34:47","uri":"https://localhost","content":"{\"好\":\"好\"}"}';
208+
209+
$this->assertEquals($expected, (string) new Payload($request));
210+
}
211+
212+
/**
213+
* @test
214+
*/
215+
public function it_stringifies_a_json_payload_with_a_url_to_a_string()
216+
{
217+
$request = (new GuzzleRequest('GET', 'https://localhost', [], json_encode(['url' => 'https://google.com'], JSON_UNESCAPED_UNICODE)))
218+
->withHeader('X-SIGNED-ID', '303103f5-3dca-4704-96ad-860717769ec9')
219+
->withHeader('X-SIGNED-TIMESTAMP', '2018-04-06 20:34:47');
220+
221+
$expected = '{"id":"303103f5-3dca-4704-96ad-860717769ec9","method":"GET","timestamp":"2018-04-06 20:34:47","uri":"https://localhost","content":"{\"url\":\"https:\\\\/\\\\/google.com\"}"}';
222+
223+
$this->assertEquals($expected, (string) new Payload($request));
224+
}
113225
}

0 commit comments

Comments
 (0)