1
+ <?php
2
+
3
+ use Workerman \Protocols \Http \Response ;
4
+
5
+ it ('test some simple case ' , function () {
6
+ $ response = new Response (201 , ['X-foo ' => 'bar ' ], 'hello, xiami ' );
7
+
8
+ expect ($ response ->getStatusCode ())->toBe (201 );
9
+ expect ($ response ->getHeaders ())->toBe (['X-foo ' => 'bar ' ]);
10
+ expect ($ response ->rawBody ())->toBe ('hello, xiami ' );
11
+
12
+ //headers
13
+ $ response ->header ('abc ' , '123 ' );
14
+ $ response ->withHeader ('X-foo ' , 'baz ' );
15
+ $ response ->withHeaders (['def ' => '456 ' ]);
16
+ expect ((string )$ response )
17
+ ->toContain ('X-foo: baz ' )
18
+ ->toContain ('abc: 123 ' )
19
+ ->toContain ('def: 456 ' );
20
+ $ response ->withoutHeader ('def ' );
21
+ expect ((string )$ response )->not ->toContain ('def: 456 ' );
22
+ expect ($ response ->getHeader ('abc ' ))->toBe ('def ' );
23
+
24
+ $ response ->withStatus (202 , 'some reason ' );
25
+ expect ($ response ->getReasonPhrase ())->toBe ('some reason ' );
26
+
27
+ $ response ->withProtocolVersion ('1.0 ' );
28
+ $ response ->withBody ('hello, world ' );
29
+ expect ((string )$ response )
30
+ ->toContain ('HTTP/1.0 ' )
31
+ ->toContain ('hello, world ' )
32
+ ->toContain ('Content-Type: ' )
33
+ ->toContain ('Content-Length: 12 ' )
34
+ ->not ()->toContain ('Transfer-Encoding: ' );
35
+
36
+
37
+ //cookie
38
+ $ response ->cookie ('foo ' , 'bar ' , httpOnly: true , domain: 'xia.moe ' );
39
+ expect ((string )$ response )
40
+ ->toContain ('Set-Cookie: foo=bar; Domain=xia.moe; HttpOnly ' );
41
+ });
42
+
43
+ it ('tests file ' , function (){
44
+ //todo may have to redo the simple test,
45
+ // as the implementation of headers is a different function for files.
46
+ // or actually maybe the Response is the one should be rewritten to reuse?
47
+ $ response = new Response ();
48
+ $ tmpFile = tempnam (sys_get_temp_dir (), 'test ' );
49
+ rename ($ tmpFile , $ tmpFile .'.jpg ' );
50
+ $ tmpFile .= '.jpg ' ;
51
+ file_put_contents ($ tmpFile , 'hello, xiami ' );
52
+ $ response ->withFile ($ tmpFile , 0 , 12 );
53
+ expect ((string )$ response )
54
+ ->toContain ('Content-Type: image/jpeg ' )
55
+ ->toContain ('Last-Modified: ' );
56
+ });
0 commit comments