-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionAuthenticatedMiddlewareTest.php
More file actions
87 lines (68 loc) · 2.89 KB
/
SessionAuthenticatedMiddlewareTest.php
File metadata and controls
87 lines (68 loc) · 2.89 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
namespace Koded\Session;
use Koded\Http\{ServerRequest, ServerResponse, StatusCode};
use Koded\Stdlib\Config;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface};
use Psr\Http\Server\RequestHandlerInterface;
class SessionAuthenticatedMiddlewareTest extends TestCase
{
/** @var SessionAuthenticatedMiddleware */
private $middleware;
public function test__construct()
{
$this->assertAttributeEquals('/signin', 'redirectTo', $this->middleware);
}
public function test_process_when_not_authenticated()
{
$handler = new class implements RequestHandlerInterface
{
public function handle(ServerRequestInterface $request): ResponseInterface
{
return new ServerResponse('hello');
}
};
$this->assertFalse(isset($_SESSION[SessionAuthenticatedMiddleware::AUTHENTICATED]));
$response = $this->middleware->process(new ServerRequest, $handler);
$this->assertEquals('/signin', $response->getHeaderLine('location'));
$this->assertSame(StatusCode::TEMPORARY_REDIRECT, $response->getStatusCode());
$this->assertEmpty((string)$response->getBody());
}
public function test_process_ajax_when_not_authenticated()
{
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XmlHttpRequest';
$handler = new class implements RequestHandlerInterface
{
public function handle(ServerRequestInterface $request): ResponseInterface
{
return new ServerResponse();
}
};
$this->assertFalse(isset($_SESSION[SessionAuthenticatedMiddleware::AUTHENTICATED]));
$response = $this->middleware->process(new ServerRequest, $handler);
$this->assertEquals('', $response->getHeaderLine('location'));
$this->assertSame(StatusCode::UNAUTHORIZED, $response->getStatusCode());
$this->assertSame('{"location":"/signin","status":401}', (string)$response->getBody());
}
public function test_process_when_authenticated()
{
$_SESSION[SessionAuthenticatedMiddleware::AUTHENTICATED] = true;
$handler = new class implements RequestHandlerInterface
{
public function handle(ServerRequestInterface $request): ResponseInterface
{
return new ServerResponse('hello');
}
};
$response = $this->middleware->process(new ServerRequest, $handler);
$this->assertEquals('', $response->getHeaderLine('location'));
$this->assertSame(StatusCode::OK, $response->getStatusCode());
$this->assertEquals('hello', (string)$response->getBody());
}
protected function setUp()
{
$this->middleware = new SessionAuthenticatedMiddleware((new Config)->import([
SessionAuthenticatedMiddleware::LOGIN_URI => '/signin',
]));
}
}