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