1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Buzz\Test\Unit\Middleware; |
6
|
|
|
|
7
|
|
|
use Buzz\Middleware\DigestAuthMiddleware; |
8
|
|
|
use Nyholm\Psr7\Request; |
9
|
|
|
use Nyholm\Psr7\Response; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
|
12
|
|
|
class DigestAuthListenerTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
public function testDigestAuthHeader() |
15
|
|
|
{ |
16
|
|
|
$request = new Request('GET', 'http://test.webdav.org/auth-digest'); |
17
|
|
|
|
18
|
|
|
$response = new Response(200, [ |
19
|
|
|
'Date' => 'Wed, 24 Jun 2015 21:49:39 GMT', |
20
|
|
|
'Server' => 'Apache/2.0.54 (Debian GNU/Linux) DAV/2 SVN/1.3.2', |
21
|
|
|
'WWW-Authenticate' => 'Digest realm="test", nonce="5PvRe0oZBQA=874ad6aea3519069f30dfc704e594dde6e01b2a6", algorithm=MD5, domain="/auth-digest/", qop="auth"', |
22
|
|
|
'Content-Length' => '401', |
23
|
|
|
'Content-Type' => 'text/html; charset=iso-8859-1', |
24
|
|
|
], "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\"> |
25
|
|
|
<html><head> |
26
|
|
|
<title>401 Authorization Required</title> |
27
|
|
|
</head><body> |
28
|
|
|
<h1>Authorization Required</h1> |
29
|
|
|
<p>This server could not verify that you |
30
|
|
|
are authorized to access the document |
31
|
|
|
requested. Either you supplied the wrong |
32
|
|
|
credentials (e.g., bad password), or your |
33
|
|
|
browser doesn\'t understand how to supply |
34
|
|
|
the credentials required.</p> |
35
|
|
|
</body></html>"); |
36
|
|
|
|
37
|
|
|
// Simulate the First Request/Response, where the server returns 401 |
38
|
|
|
$middleware = new DigestAuthMiddleware('user1', 'user1'); |
39
|
|
|
$newRequest = null; |
40
|
|
|
$middleware->handleRequest($request, function ($request) use (&$newRequest) { |
41
|
|
|
$newRequest = $request; |
42
|
|
|
}); |
43
|
|
|
|
44
|
|
|
$newResponse = null; |
45
|
|
|
$middleware->handleResponse($request, $response, function ($request, $response) use (&$newResponse) { |
46
|
|
|
$newResponse = $response; |
47
|
|
|
}); |
48
|
|
|
|
49
|
|
|
// Simulate sending the second Request using the calculated Authorization Header |
50
|
|
|
$request = new Request('GET', 'http://test.webdav.org/auth-digest'); |
51
|
|
|
$this->assertEmpty($request->getHeader('Authorization')); |
52
|
|
|
|
53
|
|
|
$newRequest = null; |
54
|
|
|
$middleware->handleRequest($request, function ($request) use (&$newRequest) { |
55
|
|
|
$newRequest = $request; |
56
|
|
|
}); |
57
|
|
|
|
58
|
|
|
$this->assertEquals( |
59
|
|
|
'Digest username="user1", realm="test", nonce="5PvRe0oZBQA=874ad6aea3519069f30dfc704e594dde6e01b2a6", response="b2cf05a5d3f51d84a8866309aed6cb5d", uri="/auth-digest"', |
60
|
|
|
$newRequest->getHeaderLine('Authorization') |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|