Issues (120)

tests/MiddlewareTest.php (19 issues)

1
<?php declare(strict_types=1);
2
3
/*
4
 * This file is part of Flight Routing.
5
 *
6
 * PHP version 8.0 and above required
7
 *
8
 * @author    Divine Niiquaye Ibok <[email protected]>
9
 * @copyright 2019 Divine Niiquaye Ibok (https://divinenii.com/)
10
 * @license   https://opensource.org/licenses/BSD-3-Clause License
11
 *
12
 * For the full copyright and license information, please view the LICENSE
13
 * file that was distributed with this source code.
14
 */
15
16
use Flight\Routing\Exceptions\RouteNotFoundException;
17
use Flight\Routing\Handlers\FileHandler;
18
use Flight\Routing\Handlers\RouteHandler;
19
use Flight\Routing\Middlewares\PathMiddleware;
20
use Flight\Routing\Middlewares\UriRedirectMiddleware;
21
use Flight\Routing\Router;
22
use Flight\Routing\Tests\Fixtures;
23
use Nyholm\Psr7\Factory\Psr17Factory;
24
use Nyholm\Psr7\ServerRequest;
25
use Nyholm\Psr7\Uri;
26
use PHPUnit\Framework as t;
27
use Psr\Http\Message\ResponseFactoryInterface;
28
use Psr\Http\Message\ResponseInterface;
29
use Psr\Http\Message\ServerRequestInterface;
30
31
dataset('paths_data', [
32
    // name => [$uriPath, $requestPath, $expectedPath, $status ]
33
    'root-without-prefix-tail_1' => ['/foo', '/foo', '', 200],
34
    'root-without-prefix-tail_2' => ['/foo', '/foo/', '/foo', 301],
35
    'root-without-prefix-tail_3' => ['/foo', '/foo@', '', 404],
36
    'root-without-prefix-tail_4' => ['/[{bar}]', '/', '', 200],
37
    'root-without-prefix-tail_5' => ['/[{bar}]', '/foo/', '/foo', 301],
38
    'root-without-prefix-tail_6' => ['/[{bar}]', '/foo', '', 200],
39
    'root-with-prefix-tail_1' => ['/foo/', '/foo/', '', 200],
40
    'root-with-prefix-tail_2' => ['/foo/', '/foo@', '', 404],
41
    'root-with-prefix-tail_3' => ['/foo/', '/foo', '/foo/', 301],
42
    'root-with-prefix-tail_4' => ['/[{bar}]/', '/', '', 200],
43
    'root-with-prefix-tail_5' => ['/[{bar}]/', '/foo', '/foo/', 301],
44
    'root-with-prefix-tail_6' => ['/[{bar}]/', '/foo/', '', 200],
45
]);
46
47
dataset('redirects', function (): Generator {
48
    yield 'Redirect string with symbols' => [
49
        ['/@come_here' => '/ch'], '/ch',
50
    ];
51
52
    yield 'Redirect string with format' => [
53
        ['/index.html' => '/home'], '/home',
54
    ];
55
56
    yield 'Redirect string with format reverse' => [
57
        ['/home' => '/index.html'], '/index.html',
58
    ];
59
60
    yield 'Redirect string with Uri instance' => [
61
        ['/sdjfdkgjdg' => new Uri('./cool')], '/cool',
62
    ];
63
});
64
65
test('if path middleware constructor is ok', function (): void {
66
    $middleware = new PathMiddleware();
67
    $response = $middleware->process(new ServerRequest('GET', '/foo'), new Fixtures\BlankRequestHandler());
68
69
    t\assertInstanceOf(ResponseInterface::class, $response);
0 ignored issues
show
The function assertInstanceOf was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
    /** @scrutinizer ignore-call */ 
70
    t\assertInstanceOf(ResponseInterface::class, $response);
Loading history...
70
    t\assertEquals(200, $response->getStatusCode());
0 ignored issues
show
The function assertEquals was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
    /** @scrutinizer ignore-call */ 
71
    t\assertEquals(200, $response->getStatusCode());
Loading history...
71
    t\assertFalse($response->hasHeader('Location'));
0 ignored issues
show
The function assertFalse was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
    /** @scrutinizer ignore-call */ 
72
    t\assertFalse($response->hasHeader('Location'));
Loading history...
72
});
73
74
test('if path middleware process the right status code', function (): void {
75
    $handler = new RouteHandler(new Psr17Factory());
76
    $router = Router::withCollection();
77
    $router->getCollection()->get('/foo', fn (ResponseFactoryInterface $f) => $f->createResponse());
78
    $router->pipe(new PathMiddleware());
79
80
    $response = $router->process(new ServerRequest('GET', '/foo'), $handler);
81
    t\assertInstanceOf(ResponseInterface::class, $response);
0 ignored issues
show
The function assertInstanceOf was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
    /** @scrutinizer ignore-call */ 
82
    t\assertInstanceOf(ResponseInterface::class, $response);
Loading history...
82
    t\assertEquals(204, $response->getStatusCode());
0 ignored issues
show
The function assertEquals was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

82
    /** @scrutinizer ignore-call */ 
83
    t\assertEquals(204, $response->getStatusCode());
Loading history...
83
    t\assertFalse($response->hasHeader('Location'));
0 ignored issues
show
The function assertFalse was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

83
    /** @scrutinizer ignore-call */ 
84
    t\assertFalse($response->hasHeader('Location'));
Loading history...
84
});
85
86
test('if path middleware can process from a subfolder correctly', function (): void {
87
    $subFolder = null;
88
    $handler = new RouteHandler(new Psr17Factory());
89
    $router = Router::withCollection();
90
    $router->pipe(new PathMiddleware());
91
    $router->getCollection()->get('/foo/', function (ServerRequestInterface $req, ResponseFactoryInterface $f) use (&$subFolder) {
92
        $subFolder = $req->getAttribute(PathMiddleware::SUB_FOLDER);
93
        $res = $f->createResponse();
94
        $res->getBody()->write(\sprintf('Routing from subfolder %s as base root', $subFolder));
95
96
        return $res;
97
    });
98
99
    $request = new ServerRequest(Router::METHOD_GET, '/build/foo', [], null, '1.1', ['PATH_INFO' => '/foo']);
100
    $response = $router->process($request, $handler);
101
    t\assertEquals('/build', $subFolder);
0 ignored issues
show
The function assertEquals was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

101
    /** @scrutinizer ignore-call */ 
102
    t\assertEquals('/build', $subFolder);
Loading history...
102
    t\assertEquals(302, $response->getStatusCode());
103
    t\assertEquals('/foo/', $response->getHeaderLine('Location'));
104
});
105
106
test('if path middleware with expected 301', function (string $uriPath, string $requestPath, string $expectedPath, int $expectsStatus): void {
107
    $router = Router::withCollection();
108
    $router->pipe(new PathMiddleware(true));
109
    $router->getCollection()->get($uriPath, function (ResponseFactoryInterface $f): ResponseInterface {
110
        $res = $f->createResponse()->withHeader('Content-Type', FileHandler::MIME_TYPE['html']);
111
        $res->getBody()->write('Hello World');
112
113
        return $res;
114
    });
115
116
    try {
117
        $response = $router->process(new ServerRequest(Router::METHOD_GET, $requestPath), new RouteHandler(new Psr17Factory()));
118
    } catch (RouteNotFoundException $e) {
119
        t\assertEquals($expectsStatus, $e->getCode());
0 ignored issues
show
The function assertEquals was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

119
        /** @scrutinizer ignore-call */ 
120
        t\assertEquals($expectsStatus, $e->getCode());
Loading history...
120
121
        return;
122
    }
123
124
    t\assertEquals($expectsStatus, $response->getStatusCode());
125
    t\assertEquals($expectedPath, $response->getHeaderLine('Location'));
126
})->with('paths_data');
127
128
test('if path middleware with expected 301 => 302', function (string $uriPath, string $requestPath, string $expectedPath, int $expectsStatus): void {
129
    $router = Router::withCollection();
130
    $router->pipe(new PathMiddleware());
131
    $router->getCollection()->get($uriPath, function (ResponseFactoryInterface $f): ResponseInterface {
132
        $res = $f->createResponse()->withHeader('Content-Type', FileHandler::MIME_TYPE['html']);
133
        $res->getBody()->write('Hello World');
134
135
        return $res;
136
    });
137
138
    try {
139
        $response = $router->process(new ServerRequest(Router::METHOD_GET, $requestPath), new RouteHandler(new Psr17Factory()));
140
    } catch (RouteNotFoundException $e) {
141
        t\assertEquals($expectsStatus, $e->getCode());
0 ignored issues
show
The function assertEquals was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

141
        /** @scrutinizer ignore-call */ 
142
        t\assertEquals($expectsStatus, $e->getCode());
Loading history...
142
143
        return;
144
    }
145
146
    t\assertEquals(301 === $expectsStatus ? 302 : $expectsStatus, $response->getStatusCode());
147
    t\assertEquals($expectedPath, $response->getHeaderLine('Location'));
148
})->with('paths_data');
149
150
test('if path middleware with expected 301 => 307', function (string $uriPath, string $requestPath, string $expectedPath, int $expectsStatus): void {
151
    $router = Router::withCollection();
152
    $router->pipe(new PathMiddleware(false, true));
153
    $router->getCollection()->get($uriPath, function (ResponseFactoryInterface $f): ResponseInterface {
154
        $res = $f->createResponse()->withHeader('Content-Type', FileHandler::MIME_TYPE['html']);
155
        $res->getBody()->write('Hello World');
156
157
        return $res;
158
    });
159
160
    try {
161
        $response = $router->process(new ServerRequest(Router::METHOD_GET, $requestPath), new RouteHandler(new Psr17Factory()));
162
    } catch (RouteNotFoundException $e) {
163
        t\assertEquals($expectsStatus, $e->getCode());
0 ignored issues
show
The function assertEquals was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

163
        /** @scrutinizer ignore-call */ 
164
        t\assertEquals($expectsStatus, $e->getCode());
Loading history...
164
165
        return;
166
    }
167
168
    t\assertEquals(301 === $expectsStatus ? 307 : $expectsStatus, $response->getStatusCode());
169
    t\assertEquals($expectedPath, $response->getHeaderLine('Location'));
170
})->with('paths_data');
171
172
test('if path middleware with expected 301 => 308', function (string $uriPath, string $requestPath, string $expectedPath, int $expectsStatus): void {
173
    $router = Router::withCollection();
174
    $router->pipe(new PathMiddleware(true, true));
175
    $router->getCollection()->get($uriPath, function (ResponseFactoryInterface $f): ResponseInterface {
176
        $res = $f->createResponse()->withHeader('Content-Type', FileHandler::MIME_TYPE['html']);
177
        $res->getBody()->write('Hello World');
178
179
        return $res;
180
    });
181
182
    try {
183
        $response = $router->process(new ServerRequest(Router::METHOD_GET, $requestPath), new RouteHandler(new Psr17Factory()));
184
    } catch (RouteNotFoundException $e) {
185
        t\assertEquals($expectsStatus, $e->getCode());
0 ignored issues
show
The function assertEquals was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

185
        /** @scrutinizer ignore-call */ 
186
        t\assertEquals($expectsStatus, $e->getCode());
Loading history...
186
187
        return;
188
    }
189
190
    t\assertEquals(301 === $expectsStatus ? 308 : $expectsStatus, $response->getStatusCode());
191
    t\assertEquals($expectedPath, $response->getHeaderLine('Location'));
192
})->with('paths_data');
193
194
test('if uri-redirect middleware can process the right status code', function (array $redirects, string $expected): void {
195
    $router = Router::withCollection();
196
    $router->pipe(new UriRedirectMiddleware($redirects));
197
    $router->getCollection()->get($expected, Fixtures\BlankRequestHandler::class);
198
199
    $res = $router->process(new ServerRequest(Router::METHOD_GET, $expected), new RouteHandler(new Psr17Factory()));
200
    t\assertInstanceOf(ResponseInterface::class, $res);
0 ignored issues
show
The function assertInstanceOf was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

200
    /** @scrutinizer ignore-call */ 
201
    t\assertInstanceOf(ResponseInterface::class, $res);
Loading history...
201
    t\assertSame(204, $res->getStatusCode());
0 ignored issues
show
The function assertSame was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

201
    /** @scrutinizer ignore-call */ 
202
    t\assertSame(204, $res->getStatusCode());
Loading history...
202
})->with('redirects');
203
204
test('if uri-redirect middleware can redirect old path to new as 301', function (array $redirects, string $expected): void {
205
    $router = Router::withCollection();
206
    $router->pipe(new UriRedirectMiddleware($redirects));
207
    $router->getCollection()->get($expected, Fixtures\BlankRequestHandler::class);
208
209
    $actual = \key($redirects);
210
    $res = $router->process(new ServerRequest(Router::METHOD_GET, $actual), new RouteHandler(new Psr17Factory()));
211
    t\assertInstanceOf(ResponseInterface::class, $res);
0 ignored issues
show
The function assertInstanceOf was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

211
    /** @scrutinizer ignore-call */ 
212
    t\assertInstanceOf(ResponseInterface::class, $res);
Loading history...
212
    t\assertSame(301, $res->getStatusCode());
0 ignored issues
show
The function assertSame was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

212
    /** @scrutinizer ignore-call */ 
213
    t\assertSame(301, $res->getStatusCode());
Loading history...
213
    t\assertSame((string) $redirects[$actual], $res->getHeaderLine('Location'));
214
})->with('redirects');
215
216
test('if uri-redirect middleware can redirect old path to new as 308', function (array $redirects, string $expected): void {
217
    $router = Router::withCollection();
218
    $router->pipe(new UriRedirectMiddleware($redirects, true));
219
    $router->getCollection()->get($expected, Fixtures\BlankRequestHandler::class);
220
221
    $actual = \key($redirects);
222
    $res = $router->process(new ServerRequest(Router::METHOD_GET, $actual), new RouteHandler(new Psr17Factory()));
223
    t\assertInstanceOf(ResponseInterface::class, $res);
0 ignored issues
show
The function assertInstanceOf was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

223
    /** @scrutinizer ignore-call */ 
224
    t\assertInstanceOf(ResponseInterface::class, $res);
Loading history...
224
    t\assertSame(308, $res->getStatusCode());
0 ignored issues
show
The function assertSame was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

224
    /** @scrutinizer ignore-call */ 
225
    t\assertSame(308, $res->getStatusCode());
Loading history...
225
    t\assertSame((string) $redirects[$actual], $res->getHeaderLine('Location'));
226
})->with('redirects');
227
228
test('if uri-redirect middleware can redirect a full path to new', function (): void {
229
    $router = Router::withCollection();
230
    $router->pipe(new UriRedirectMiddleware(['/user/\d+' => '#/account/me']));
231
    $router->getCollection()->get('/account/me', Fixtures\BlankRequestHandler::class);
232
233
    $uri = new Uri('/user/23?page=settings#notification');
234
    $res = $router->process(new ServerRequest(Router::METHOD_GET, $uri), new RouteHandler(new Psr17Factory()));
235
    t\assertInstanceOf(ResponseInterface::class, $res);
0 ignored issues
show
The function assertInstanceOf was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

235
    /** @scrutinizer ignore-call */ 
236
    t\assertInstanceOf(ResponseInterface::class, $res);
Loading history...
236
    t\assertSame(301, $res->getStatusCode());
0 ignored issues
show
The function assertSame was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

236
    /** @scrutinizer ignore-call */ 
237
    t\assertSame(301, $res->getStatusCode());
Loading history...
237
    t\assertSame('/account/me?page=settings#notification', $res->getHeaderLine('Location'));
238
});
239