@@ -63,10 +63,21 @@ private function createListener(array $options = array(), $success = true, $matc
63
63
$ this ->listener = new UsernamePasswordJsonAuthenticationListener ($ tokenStorage , $ authenticationManager , $ httpUtils , 'providerKey ' , $ authenticationSuccessHandler , $ authenticationFailureHandler , $ options );
64
64
}
65
65
66
- public function testHandleSuccess ()
66
+ public function testHandleSuccessIfRequestContentTypeIsJson ()
67
+ {
68
+ $ this ->createListener ();
69
+ $ request = new Request (array (), array (), array (), array (), array (), array ('HTTP_CONTENT_TYPE ' => 'application/json ' ), '{"username": "dunglas", "password": "foo"} ' );
70
+ $ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
71
+
72
+ $ this ->listener ->handle ($ event );
73
+ $ this ->assertEquals ('ok ' , $ event ->getResponse ()->getContent ());
74
+ }
75
+
76
+ public function testSuccessIfRequestFormatIsJsonLD ()
67
77
{
68
78
$ this ->createListener ();
69
79
$ request = new Request (array (), array (), array (), array (), array (), array (), '{"username": "dunglas", "password": "foo"} ' );
80
+ $ request ->setRequestFormat ('json-ld ' );
70
81
$ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
71
82
72
83
$ this ->listener ->handle ($ event );
@@ -76,7 +87,7 @@ public function testHandleSuccess()
76
87
public function testHandleFailure ()
77
88
{
78
89
$ this ->createListener (array (), false );
79
- $ request = new Request (array (), array (), array (), array (), array (), array (), '{"username": "dunglas", "password": "foo"} ' );
90
+ $ request = new Request (array (), array (), array (), array (), array (), array (' HTTP_CONTENT_TYPE ' => ' application/json ' ), '{"username": "dunglas", "password": "foo"} ' );
80
91
$ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
81
92
82
93
$ this ->listener ->handle ($ event );
@@ -86,7 +97,7 @@ public function testHandleFailure()
86
97
public function testUsePath ()
87
98
{
88
99
$ this ->createListener (array ('username_path ' => 'user.login ' , 'password_path ' => 'user.pwd ' ));
89
- $ request = new Request (array (), array (), array (), array (), array (), array (), '{"user": {"login": "dunglas", "pwd": "foo"}} ' );
100
+ $ request = new Request (array (), array (), array (), array (), array (), array (' HTTP_CONTENT_TYPE ' => ' application/json ' ), '{"user": {"login": "dunglas", "pwd": "foo"}} ' );
90
101
$ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
91
102
92
103
$ this ->listener ->handle ($ event );
@@ -113,7 +124,7 @@ public function testAttemptAuthenticationNoJson()
113
124
public function testAttemptAuthenticationNoUsername ()
114
125
{
115
126
$ this ->createListener ();
116
- $ request = new Request (array (), array (), array (), array (), array (), array (), '{"usr": "dunglas", "password": "foo"} ' );
127
+ $ request = new Request (array (), array (), array (), array (), array (), array (' HTTP_CONTENT_TYPE ' => ' application/json ' ), '{"usr": "dunglas", "password": "foo"} ' );
117
128
$ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
118
129
119
130
$ this ->listener ->handle ($ event );
@@ -126,7 +137,7 @@ public function testAttemptAuthenticationNoUsername()
126
137
public function testAttemptAuthenticationNoPassword ()
127
138
{
128
139
$ this ->createListener ();
129
- $ request = new Request (array (), array (), array (), array (), array (), array (), '{"username": "dunglas", "pass": "foo"} ' );
140
+ $ request = new Request (array (), array (), array (), array (), array (), array (' HTTP_CONTENT_TYPE ' => ' application/json ' ), '{"username": "dunglas", "pass": "foo"} ' );
130
141
$ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
131
142
132
143
$ this ->listener ->handle ($ event );
@@ -139,7 +150,7 @@ public function testAttemptAuthenticationNoPassword()
139
150
public function testAttemptAuthenticationUsernameNotAString ()
140
151
{
141
152
$ this ->createListener ();
142
- $ request = new Request (array (), array (), array (), array (), array (), array (), '{"username": 1, "password": "foo"} ' );
153
+ $ request = new Request (array (), array (), array (), array (), array (), array (' HTTP_CONTENT_TYPE ' => ' application/json ' ), '{"username": 1, "password": "foo"} ' );
143
154
$ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
144
155
145
156
$ this ->listener ->handle ($ event );
@@ -152,7 +163,7 @@ public function testAttemptAuthenticationUsernameNotAString()
152
163
public function testAttemptAuthenticationPasswordNotAString ()
153
164
{
154
165
$ this ->createListener ();
155
- $ request = new Request (array (), array (), array (), array (), array (), array (), '{"username": "dunglas", "password": 1} ' );
166
+ $ request = new Request (array (), array (), array (), array (), array (), array (' HTTP_CONTENT_TYPE ' => ' application/json ' ), '{"username": "dunglas", "password": 1} ' );
156
167
$ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
157
168
158
169
$ this ->listener ->handle ($ event );
@@ -162,7 +173,7 @@ public function testAttemptAuthenticationUsernameTooLong()
162
173
{
163
174
$ this ->createListener ();
164
175
$ username = str_repeat ('x ' , Security::MAX_USERNAME_LENGTH + 1 );
165
- $ request = new Request (array (), array (), array (), array (), array (), array (), sprintf ('{"username": "%s", "password": 1} ' , $ username ));
176
+ $ request = new Request (array (), array (), array (), array (), array (), array (' HTTP_CONTENT_TYPE ' => ' application/json ' ), sprintf ('{"username": "%s", "password": 1} ' , $ username ));
166
177
$ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
167
178
168
179
$ this ->listener ->handle ($ event );
@@ -172,7 +183,18 @@ public function testAttemptAuthenticationUsernameTooLong()
172
183
public function testDoesNotAttemptAuthenticationIfRequestPathDoesNotMatchCheckPath ()
173
184
{
174
185
$ this ->createListener (array ('check_path ' => '/ ' ), true , false );
175
- $ request = new Request ();
186
+ $ request = new Request (array (), array (), array (), array (), array (), array ('HTTP_CONTENT_TYPE ' => 'application/json ' ));
187
+ $ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
188
+ $ event ->setResponse (new Response ('original ' ));
189
+
190
+ $ this ->listener ->handle ($ event );
191
+ $ this ->assertSame ('original ' , $ event ->getResponse ()->getContent ());
192
+ }
193
+
194
+ public function testDoesNotAttemptAuthenticationIfRequestContentTypeIsNotJson ()
195
+ {
196
+ $ this ->createListener ();
197
+ $ request = new Request (array (), array (), array (), array (), array (), array (), '{"username": "dunglas", "password": "foo"} ' );
176
198
$ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
177
199
$ event ->setResponse (new Response ('original ' ));
178
200
@@ -183,7 +205,7 @@ public function testDoesNotAttemptAuthenticationIfRequestPathDoesNotMatchCheckPa
183
205
public function testAttemptAuthenticationIfRequestPathMatchesCheckPath ()
184
206
{
185
207
$ this ->createListener (array ('check_path ' => '/ ' ));
186
- $ request = new Request (array (), array (), array (), array (), array (), array (), '{"username": "dunglas", "password": "foo"} ' );
208
+ $ request = new Request (array (), array (), array (), array (), array (), array (' HTTP_CONTENT_TYPE ' => ' application/json ' ), '{"username": "dunglas", "password": "foo"} ' );
187
209
$ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
188
210
189
211
$ this ->listener ->handle ($ event );
0 commit comments