Skip to content

Commit 10144cc

Browse files
committed
Add JSON flags to Saloon requests
1 parent 359ca0c commit 10144cc

4 files changed

Lines changed: 143 additions & 2 deletions

File tree

src/Request/GetReceiptsRequest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@
1616
use Saloon\Enums\Method;
1717
use Saloon\Http\Request;
1818
use Saloon\Http\Response;
19+
use Saloon\Repositories\Body\JsonBodyRepository;
1920
use Saloon\Traits\Body\HasJsonBody;
2021
use Saloon\Traits\Plugins\AcceptsJson;
2122
use UnexpectedValueException;
2223

2324
final class GetReceiptsRequest extends Request implements HasBody
2425
{
2526
use AcceptsJson;
26-
use HasJsonBody;
27+
use HasJsonBody {
28+
body as protected buildBody;
29+
}
2730
use CompressesBody;
2831

2932
public const MAX_RECEIPT_COUNT = 1000;
@@ -37,6 +40,14 @@ public function resolveEndpoint(): string
3740
return '/getReceipts';
3841
}
3942

43+
/**
44+
* @inheritDoc
45+
*/
46+
public function body(): JsonBodyRepository
47+
{
48+
return $this->buildBody()->setJsonFlags(JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
49+
}
50+
4051
// DTO ----
4152

4253
/**

src/Request/SendNotificationsRequest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@
1818
use Saloon\Enums\Method;
1919
use Saloon\Http\Request;
2020
use Saloon\Http\Response;
21+
use Saloon\Repositories\Body\JsonBodyRepository;
2122
use Saloon\Traits\Body\HasJsonBody;
2223
use Saloon\Traits\Plugins\AcceptsJson;
2324
use UnexpectedValueException;
2425

2526
final class SendNotificationsRequest extends Request implements HasBody
2627
{
2728
use AcceptsJson;
28-
use HasJsonBody;
29+
use HasJsonBody {
30+
body as protected buildBody;
31+
}
2932
use CompressesBody;
3033

3134
public const MAX_NOTIFICATION_COUNT = 100;
@@ -40,6 +43,14 @@ public function resolveEndpoint(): string
4043
return '/send';
4144
}
4245

46+
/**
47+
* @inheritDoc
48+
*/
49+
public function body(): JsonBodyRepository
50+
{
51+
return $this->buildBody()->setJsonFlags(JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
52+
}
53+
4354
// DTO ----
4455

4556
/**

tests/Unit/Request/GetReceiptsRequestTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Dru1x\ExpoPush\PushReceipt\PushReceiptErrorCode;
1010
use Dru1x\ExpoPush\PushReceipt\PushReceiptIdCollection;
1111
use Dru1x\ExpoPush\Request\GetReceiptsRequest;
12+
use JsonException;
1213
use OverflowException;
1314
use PHPUnit\Framework\Attributes\Test;
1415
use PHPUnit\Framework\TestCase;
@@ -207,6 +208,57 @@ public function body_throws_exception_when_receipt_id_collection_is_too_large():
207208
$request->body();
208209
}
209210

211+
#[Test]
212+
public function body_sets_expected_json_flags(): void
213+
{
214+
$request = new GetReceiptsRequest(
215+
new PushReceiptIdCollection(),
216+
);
217+
218+
$this->assertSame(
219+
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE,
220+
$request->body()->getJsonFlags(),
221+
);
222+
}
223+
224+
#[Test]
225+
public function body_does_not_escape_slashes(): void
226+
{
227+
$request = new GetReceiptsRequest(
228+
new PushReceiptIdCollection('XXXX/YYYY'),
229+
);
230+
231+
$json = (string) $request->body();
232+
233+
$this->assertStringContainsString('XXXX/YYYY', $json);
234+
$this->assertStringNotContainsString('XXXX\/YYYY', $json);
235+
}
236+
237+
#[Test]
238+
public function body_does_not_escape_unicode_characters(): void
239+
{
240+
$request = new GetReceiptsRequest(
241+
new PushReceiptIdCollection('café-🎉'),
242+
);
243+
244+
$json = (string) $request->body();
245+
246+
$this->assertStringContainsString('café-🎉', $json);
247+
$this->assertStringNotContainsString('\u00e9', $json);
248+
}
249+
250+
#[Test]
251+
public function body_throws_json_exception_when_data_contains_invalid_utf8(): void
252+
{
253+
$request = new GetReceiptsRequest(
254+
new PushReceiptIdCollection("\xB1\x31"),
255+
);
256+
257+
$this->expectException(JsonException::class);
258+
259+
(string) $request->body();
260+
}
261+
210262
// Helpers ----
211263

212264
protected function generatePushReceiptId(): string

tests/Unit/Request/SendNotificationsRequestTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Dru1x\ExpoPush\PushToken\PushToken;
1414
use Dru1x\ExpoPush\Request\SendNotificationsRequest;
1515
use InvalidArgumentException;
16+
use JsonException;
1617
use OverflowException;
1718
use PHPUnit\Framework\Attributes\Test;
1819
use PHPUnit\Framework\TestCase;
@@ -278,4 +279,70 @@ public function body_throws_exception_when_any_push_message_data_cannot_be_encod
278279

279280
$request->body();
280281
}
282+
283+
#[Test]
284+
public function body_sets_expected_json_flags(): void
285+
{
286+
$request = new SendNotificationsRequest(
287+
new PushMessageCollection(),
288+
);
289+
290+
$this->assertSame(
291+
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE,
292+
$request->body()->getJsonFlags(),
293+
);
294+
}
295+
296+
#[Test]
297+
public function body_does_not_escape_slashes(): void
298+
{
299+
$request = new SendNotificationsRequest(
300+
new PushMessageCollection(
301+
new PushMessage(
302+
to: new PushToken('ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]'),
303+
title: 'https://example.com',
304+
),
305+
),
306+
);
307+
308+
$json = (string) $request->body();
309+
310+
$this->assertStringContainsString('https://example.com', $json);
311+
$this->assertStringNotContainsString('https:\/\/example.com', $json);
312+
}
313+
314+
#[Test]
315+
public function body_does_not_escape_unicode_characters(): void
316+
{
317+
$request = new SendNotificationsRequest(
318+
new PushMessageCollection(
319+
new PushMessage(
320+
to: new PushToken('ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]'),
321+
title: 'café 🎉',
322+
),
323+
),
324+
);
325+
326+
$json = (string) $request->body();
327+
328+
$this->assertStringContainsString('café 🎉', $json);
329+
$this->assertStringNotContainsString('\u00e9', $json);
330+
}
331+
332+
#[Test]
333+
public function body_throws_json_exception_when_data_contains_invalid_utf8(): void
334+
{
335+
$request = new SendNotificationsRequest(
336+
new PushMessageCollection(
337+
new PushMessage(
338+
to: new PushToken('ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]'),
339+
title: "\xB1\x31",
340+
),
341+
),
342+
);
343+
344+
$this->expectException(JsonException::class);
345+
346+
(string) $request->body();
347+
}
281348
}

0 commit comments

Comments
 (0)