@@ -63,10 +63,21 @@ private function createListener(array $options = array(), $success = true, $matc
6363 $ this ->listener = new UsernamePasswordJsonAuthenticationListener ($ tokenStorage , $ authenticationManager , $ httpUtils , 'providerKey ' , $ authenticationSuccessHandler , $ authenticationFailureHandler , $ options );
6464 }
6565
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 ()
6777 {
6878 $ this ->createListener ();
6979 $ request = new Request (array (), array (), array (), array (), array (), array (), '{"username": "dunglas", "password": "foo"} ' );
80+ $ request ->setRequestFormat ('json-ld ' );
7081 $ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
7182
7283 $ this ->listener ->handle ($ event );
@@ -76,7 +87,7 @@ public function testHandleSuccess()
7687 public function testHandleFailure ()
7788 {
7889 $ 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"} ' );
8091 $ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
8192
8293 $ this ->listener ->handle ($ event );
@@ -86,7 +97,7 @@ public function testHandleFailure()
8697 public function testUsePath ()
8798 {
8899 $ 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"}} ' );
90101 $ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
91102
92103 $ this ->listener ->handle ($ event );
@@ -113,7 +124,7 @@ public function testAttemptAuthenticationNoJson()
113124 public function testAttemptAuthenticationNoUsername ()
114125 {
115126 $ 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"} ' );
117128 $ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
118129
119130 $ this ->listener ->handle ($ event );
@@ -126,7 +137,7 @@ public function testAttemptAuthenticationNoUsername()
126137 public function testAttemptAuthenticationNoPassword ()
127138 {
128139 $ 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"} ' );
130141 $ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
131142
132143 $ this ->listener ->handle ($ event );
@@ -139,7 +150,7 @@ public function testAttemptAuthenticationNoPassword()
139150 public function testAttemptAuthenticationUsernameNotAString ()
140151 {
141152 $ 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"} ' );
143154 $ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
144155
145156 $ this ->listener ->handle ($ event );
@@ -152,7 +163,7 @@ public function testAttemptAuthenticationUsernameNotAString()
152163 public function testAttemptAuthenticationPasswordNotAString ()
153164 {
154165 $ 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} ' );
156167 $ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
157168
158169 $ this ->listener ->handle ($ event );
@@ -162,7 +173,7 @@ public function testAttemptAuthenticationUsernameTooLong()
162173 {
163174 $ this ->createListener ();
164175 $ 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 ));
166177 $ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
167178
168179 $ this ->listener ->handle ($ event );
@@ -172,7 +183,18 @@ public function testAttemptAuthenticationUsernameTooLong()
172183 public function testDoesNotAttemptAuthenticationIfRequestPathDoesNotMatchCheckPath ()
173184 {
174185 $ 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"} ' );
176198 $ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
177199 $ event ->setResponse (new Response ('original ' ));
178200
@@ -183,7 +205,7 @@ public function testDoesNotAttemptAuthenticationIfRequestPathDoesNotMatchCheckPa
183205 public function testAttemptAuthenticationIfRequestPathMatchesCheckPath ()
184206 {
185207 $ 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"} ' );
187209 $ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
188210
189211 $ this ->listener ->handle ($ event );
0 commit comments