File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed
Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Tempest \Http \Responses ;
6+
7+ use Tempest \Http \Header ;
8+ use Tempest \Http \IsResponse ;
9+ use Tempest \Http \Response ;
10+ use Tempest \Http \Status ;
11+
12+ final class Json implements Response
13+ {
14+ use IsResponse;
15+
16+ public function __construct (?array $ body = null )
17+ {
18+ $ this ->status = Status::OK ;
19+ $ this ->body = $ body ;
20+ $ this ->addHeader ('Accept ' , 'application/json ' );
21+ $ this ->addHeader ('Content-Type ' , 'application/json ' );
22+ }
23+ }
Original file line number Diff line number Diff line change 55namespace Tempest \Http \Tests \Responses ;
66
77use PHPUnit \Framework \TestCase ;
8+ use Tempest \Http \Header ;
89use Tempest \Http \Responses \Created ;
10+ use Tempest \Http \Responses \Json ;
911use Tempest \Http \Status ;
1012
1113/**
@@ -22,4 +24,20 @@ public function test_created_response(): void
2224 $ this ->assertSame ('{"foo":"bar"} ' , $ response ->body );
2325 $ this ->assertNotSame (Status::OK , $ response ->status );
2426 }
27+
28+ public function test_json_response (): void
29+ {
30+ $ response = new Json (['foo ' => 'bar ' ]);
31+
32+ $ this ->assertSame (Status::OK , $ response ->status );
33+ $ this ->assertSame (['foo ' => 'bar ' ], $ response ->body );
34+
35+ $ accept = $ response ->getHeader ('Accept ' );
36+ $ contentType = $ response ->getHeader ('Content-Type ' );
37+
38+ $ this ->assertSame ('Accept ' , $ accept ->name );
39+ $ this ->assertSame ('application/json ' , $ accept ->values [0 ]);
40+ $ this ->assertSame ('Content-Type ' , $ contentType ->name );
41+ $ this ->assertSame ('application/json ' , $ contentType ->values [0 ]);
42+ }
2543}
You can’t perform that action at this time.
0 commit comments