2
2
3
3
namespace webignition \Guzzle \Middleware \HttpAuthentication \Tests ;
4
4
5
+ use Mockery \MockInterface ;
5
6
use Psr \Http \Message \RequestInterface ;
6
7
use webignition \Guzzle \Middleware \HttpAuthentication \HttpAuthenticationCredentials ;
7
8
use webignition \Guzzle \Middleware \HttpAuthentication \HttpAuthenticationHeader ;
@@ -41,12 +42,7 @@ function ($returnedRequest, $returnedOptions) use ($request, $options) {
41
42
42
43
public function testInvokeCredentialsInvalidForRequest ()
43
44
{
44
- $ request = \Mockery::mock (RequestInterface::class);
45
-
46
- $ request
47
- ->shouldReceive ('getHeaderLine ' )
48
- ->with ('host ' )
49
- ->andReturn ('example.com ' );
45
+ $ request = $ this ->createOriginalRequest ();
50
46
$ options = [];
51
47
52
48
$ credentials = new HttpAuthenticationCredentials ('username ' , 'password ' , 'example.org ' );
@@ -62,34 +58,89 @@ function ($returnedRequest, $returnedOptions) use ($request, $options) {
62
58
$ returnedFunction ($ request , $ options );
63
59
}
64
60
65
- public function testInvokeCredentialsValidForRequest ()
61
+ public function testInvokeValidCredentialsAppliedToAllRequests ()
66
62
{
67
- $ modifiedRequest = \Mockery::mock (RequestInterface::class);
68
-
69
- $ request = \Mockery::mock (RequestInterface::class);
63
+ $ credentials = new HttpAuthenticationCredentials ('username ' , 'password ' , 'example.com ' );
64
+ $ this ->httpAuthenticationMiddleware ->setHttpAuthenticationCredentials ($ credentials );
70
65
71
- $ request
72
- ->shouldReceive ('getHeaderLine ' )
73
- ->with ('host ' )
74
- ->andReturn ('example.com ' );
66
+ $ requestCount = 3 ;
75
67
76
- $ request
68
+ $ modifiedRequest = \Mockery::mock (RequestInterface::class);
69
+ $ originalRequest = $ this ->createOriginalRequest ();
70
+ $ originalRequest
77
71
->shouldReceive ('withHeader ' )
78
72
->with (HttpAuthenticationHeader::NAME , 'Basic dXNlcm5hbWU6cGFzc3dvcmQ= ' )
79
73
->andReturn ($ modifiedRequest );
80
74
81
- $ options = [];
75
+ for ($ requestIndex = 0 ; $ requestIndex < $ requestCount ; $ requestIndex ++) {
76
+ $ options = [];
77
+
78
+ $ returnedFunction = $ this ->httpAuthenticationMiddleware ->__invoke (
79
+ function ($ returnedRequest , $ returnedOptions ) use ($ modifiedRequest , $ options ) {
80
+ $ this ->assertEquals ($ modifiedRequest , $ returnedRequest );
81
+ $ this ->assertEquals ($ options , $ returnedOptions );
82
+ }
83
+ );
82
84
85
+ $ returnedFunction ($ originalRequest , $ options );
86
+ }
87
+ }
88
+
89
+ public function testInvokeValidCredentialsAppliedToFirstRequestOnly ()
90
+ {
83
91
$ credentials = new HttpAuthenticationCredentials ('username ' , 'password ' , 'example.com ' );
84
92
$ this ->httpAuthenticationMiddleware ->setHttpAuthenticationCredentials ($ credentials );
93
+ $ this ->httpAuthenticationMiddleware ->setIsSingleUse (true );
85
94
86
- $ returnedFunction = $ this ->httpAuthenticationMiddleware ->__invoke (
87
- function ($ returnedRequest , $ returnedOptions ) use ($ modifiedRequest , $ options ) {
88
- $ this ->assertEquals ($ modifiedRequest , $ returnedRequest );
89
- $ this ->assertEquals ($ options , $ returnedOptions );
95
+ $ requestCount = 3 ;
96
+ $ modifiedRequest = \Mockery::mock (RequestInterface::class);
97
+ $ originalRequest = $ this ->createOriginalRequest ();
98
+
99
+ for ($ requestIndex = 0 ; $ requestIndex < $ requestCount ; $ requestIndex ++) {
100
+ if ($ requestIndex === 0 ) {
101
+ $ originalRequest
102
+ ->shouldReceive ('withHeader ' )
103
+ ->with (HttpAuthenticationHeader::NAME , 'Basic dXNlcm5hbWU6cGFzc3dvcmQ= ' )
104
+ ->andReturn ($ modifiedRequest );
90
105
}
91
- );
92
106
93
- $ returnedFunction ($ request , $ options );
107
+ $ options = [];
108
+
109
+ $ returnedFunction = $ this ->httpAuthenticationMiddleware ->__invoke (
110
+ function (
111
+ $ returnedRequest ,
112
+ $ returnedOptions
113
+ ) use (
114
+ $ originalRequest ,
115
+ $ modifiedRequest ,
116
+ $ options ,
117
+ $ requestIndex
118
+ ) {
119
+ if ($ requestIndex === 0 ) {
120
+ $ this ->assertEquals ($ modifiedRequest , $ returnedRequest );
121
+ } else {
122
+ $ this ->assertEquals ($ originalRequest , $ returnedRequest );
123
+ }
124
+
125
+ $ this ->assertEquals ($ options , $ returnedOptions );
126
+ }
127
+ );
128
+
129
+ $ returnedFunction ($ originalRequest , $ options );
130
+ }
131
+ }
132
+
133
+ /**
134
+ * @return MockInterface|RequestInterface
135
+ */
136
+ private function createOriginalRequest ()
137
+ {
138
+ $ request = \Mockery::mock (RequestInterface::class);
139
+ $ request
140
+ ->shouldReceive ('getHeaderLine ' )
141
+ ->with ('host ' )
142
+ ->andReturn ('example.com ' );
143
+
144
+ return $ request ;
94
145
}
95
146
}
0 commit comments