-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSessionMiddlewareTest.php
More file actions
146 lines (114 loc) · 5.49 KB
/
SessionMiddlewareTest.php
File metadata and controls
146 lines (114 loc) · 5.49 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
use Illuminate\Session\SessionManager;
use Illuminate\Session\Store;
use L4\Tests\BackwardCompatibleTestCase;
use Mockery as m;
use Symfony\Component\HttpKernel\HttpKernelInterface;
class SessionMiddlewareTest extends BackwardCompatibleTestCase
{
protected function tearDown(): void
{
m::close();
}
public function testSessionIsProperlyStartedAndClosed()
{
$request = Symfony\Component\HttpFoundation\Request::create('http://www.foo.com/some', 'GET');
$response = new Symfony\Component\HttpFoundation\Response;
$middle = new Illuminate\Session\Middleware(
$app = m::mock(HttpKernelInterface::class),
$manager = m::mock(SessionManager::class)
);
$manager->shouldReceive('getSessionConfig')->andReturn([
'driver' => 'file',
'lottery' => [100, 100],
'path' => '/',
'domain' => null,
'lifetime' => 120,
'expire_on_close' => false,
]);
$manager->shouldReceive('driver')->andReturn($driver = m::mock(Store::class)->makePartial());
$driver->shouldReceive('setRequestOnHandler')->once()->with($request);
$driver->shouldReceive('start')->once();
$app->shouldReceive('handle')->once()->with($request, Symfony\Component\HttpKernel\HttpKernelInterface::MAIN_REQUEST, true)->andReturn($response);
$driver->shouldReceive('save')->once();
$driver->shouldReceive('getHandler')->andReturn($handler = m::mock('StdClass'));
$handler->shouldReceive('gc')->once()->with(120 * 60);
$driver->shouldReceive('getName')->andReturn('name');
$driver->shouldReceive('getId')->andReturn(1);
$driver->shouldReceive('setPreviousUrl')->with('http://www.foo.com/some')->once();
$middleResponse = $middle->handle($request);
$this->assertSame($response, $middleResponse);
$this->assertEquals(1, head($response->headers->getCookies())->getValue());
}
public function testSessionIsNotUsedWhenNoDriver()
{
$request = Symfony\Component\HttpFoundation\Request::create('/', 'GET');
$response = new Symfony\Component\HttpFoundation\Response;
$middle = new Illuminate\Session\Middleware(
$app = m::mock(HttpKernelInterface::class),
$manager = m::mock(SessionManager::class)
);
$manager->shouldReceive('getSessionConfig')->andReturn([
'driver' => null,
]);
$app->shouldReceive('handle')->once()->with($request, Symfony\Component\HttpKernel\HttpKernelInterface::MAIN_REQUEST, true)->andReturn($response);
$middleResponse = $middle->handle($request);
$this->assertSame($response, $middleResponse);
}
public function testSessionIsNotUsedWhenRequestHasValidAuthorizationHeader(): void
{
$request = Symfony\Component\HttpFoundation\Request::create('http://www.foo.com/api/some-api', 'GET', server: [
'HTTP_AUTHORIZATION' => 'Bearer 1234567890',
]);
$response = new Symfony\Component\HttpFoundation\Response;
$middle = new Illuminate\Session\Middleware(
$app = m::mock(HttpKernelInterface::class),
$manager = m::mock(SessionManager::class)
);
$manager->shouldNotReceive('getSessionConfig');
$manager->shouldReceive('driver')->andReturn($driver = m::mock(Store::class)->makePartial());
$driver->shouldNotReceive('setRequestOnHandler');
$driver->shouldNotReceive('start');
$app->shouldReceive('handle')->once()->with($request, Symfony\Component\HttpKernel\HttpKernelInterface::MAIN_REQUEST, true)->andReturn($response);
$driver->shouldNotReceive('save');
$driver->shouldNotReceive('getHandler');
$driver->shouldNotReceive('getName')->andReturn('name');
$driver->shouldNotReceive('getId')->andReturn(1);
$driver->shouldNotReceive('setPreviousUrl');
$middleResponse = $middle->handle($request);
self::assertSame($response, $middleResponse);
self::assertCount(0, $response->headers->getCookies());
}
public function testSessionIsNotUsedWhenRequestFromOauthApi(): void
{
$request = Symfony\Component\HttpFoundation\Request::create('http://www.foo.com/api/v1/oauth/token', 'POST');
$response = new Symfony\Component\HttpFoundation\Response;
$middle = new Illuminate\Session\Middleware(
$app = m::mock(HttpKernelInterface::class),
$manager = m::mock(SessionManager::class)
);
$manager->shouldNotReceive('getSessionConfig');
$manager->shouldReceive('driver')->andReturn($driver = m::mock(Store::class)->makePartial());
$driver->shouldNotReceive('setRequestOnHandler');
$driver->shouldNotReceive('start');
$app->shouldReceive('handle')->once()->with($request, Symfony\Component\HttpKernel\HttpKernelInterface::MAIN_REQUEST, true)->andReturn($response);
$driver->shouldNotReceive('save');
$driver->shouldNotReceive('getHandler');
$driver->shouldNotReceive('getName')->andReturn('name');
$driver->shouldNotReceive('getId')->andReturn(1);
$driver->shouldNotReceive('setPreviousUrl');
$middleResponse = $middle->handle($request);
self::assertSame($response, $middleResponse);
self::assertCount(0, $response->headers->getCookies());
}
public function testCheckingForRequestUsingArraySessions()
{
$middleware = new Illuminate\Session\Middleware(
m::mock(HttpKernelInterface::class),
$manager = m::mock(SessionManager::class),
function() { return true; }
);
$manager->shouldReceive('setDefaultDriver')->once()->with('array');
$middleware->checkRequestForArraySessions(new Symfony\Component\HttpFoundation\Request);
}
}