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 ' ))
23
+ ->toBe ('123 ' );
24
+
25
+ $ response ->withStatus (202 , 'some reason ' );
26
+ expect ($ response ->getReasonPhrase ())->toBe ('some reason ' );
27
+
28
+ $ response ->withProtocolVersion ('1.0 ' );
29
+ $ response ->withBody ('hello, world ' );
30
+ expect ((string )$ response )
31
+ ->toContain ('HTTP/1.0 ' )
32
+ ->toContain ('hello, world ' )
33
+ ->toContain ('Content-Type: ' )
34
+ ->toContain ('Content-Length: 12 ' )
35
+ ->not ()->toContain ('Transfer-Encoding: ' );
36
+
37
+
38
+ //cookie
39
+ $ response ->cookie ('foo ' , 'bar ' , httpOnly: true , domain: 'xia.moe ' );
40
+ expect ((string )$ response )
41
+ ->toContain ('Set-Cookie: foo=bar; Domain=xia.moe; HttpOnly ' );
42
+ });
43
+
44
+ it ('tests file ' , function (){
45
+ //todo may have to redo the simple test,
46
+ // as the implementation of headers is a different function for files.
47
+ // or actually maybe the Response is the one should be rewritten to reuse?
48
+ $ response = new Response ();
49
+ $ tmpFile = tempnam (sys_get_temp_dir (), 'test ' );
50
+ rename ($ tmpFile , $ tmpFile .'.jpg ' );
51
+ $ tmpFile .= '.jpg ' ;
52
+ file_put_contents ($ tmpFile , 'hello, xiami ' );
53
+ $ response ->withFile ($ tmpFile , 0 , 12 );
54
+ expect ((string )$ response )
55
+ ->toContain ('Content-Type: image/jpeg ' )
56
+ ->toContain ('Last-Modified: ' );
57
+ });
0 commit comments