@@ -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 );
@@ -96,7 +107,7 @@ public function testUsePath()
96107 public function testAttemptAuthenticationNoUsername ()
97108 {
98109 $ this ->createListener ();
99- $ request = new Request (array (), array (), array (), array (), array (), array (), '{"usr": "dunglas", "password": "foo"} ' );
110+ $ request = new Request (array (), array (), array (), array (), array (), array (' HTTP_CONTENT_TYPE ' => ' application/json ' ), '{"usr": "dunglas", "password": "foo"} ' );
100111 $ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
101112
102113 $ this ->listener ->handle ($ event );
@@ -106,7 +117,7 @@ public function testAttemptAuthenticationNoUsername()
106117 public function testAttemptAuthenticationNoPassword ()
107118 {
108119 $ this ->createListener ();
109- $ request = new Request (array (), array (), array (), array (), array (), array (), '{"username": "dunglas", "pass": "foo"} ' );
120+ $ request = new Request (array (), array (), array (), array (), array (), array (' HTTP_CONTENT_TYPE ' => ' application/json ' ), '{"username": "dunglas", "pass": "foo"} ' );
110121 $ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
111122
112123 $ this ->listener ->handle ($ event );
@@ -116,7 +127,7 @@ public function testAttemptAuthenticationNoPassword()
116127 public function testAttemptAuthenticationUsernameNotAString ()
117128 {
118129 $ this ->createListener ();
119- $ request = new Request (array (), array (), array (), array (), array (), array (), '{"username": 1, "password": "foo"} ' );
130+ $ request = new Request (array (), array (), array (), array (), array (), array (' HTTP_CONTENT_TYPE ' => ' application/json ' ), '{"username": 1, "password": "foo"} ' );
120131 $ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
121132
122133 $ this ->listener ->handle ($ event );
@@ -126,7 +137,7 @@ public function testAttemptAuthenticationUsernameNotAString()
126137 public function testAttemptAuthenticationPasswordNotAString ()
127138 {
128139 $ this ->createListener ();
129- $ request = new Request (array (), array (), array (), array (), array (), array (), '{"username": "dunglas", "password": 1} ' );
140+ $ request = new Request (array (), array (), array (), array (), array (), array (' HTTP_CONTENT_TYPE ' => ' application/json ' ), '{"username": "dunglas", "password": 1} ' );
130141 $ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
131142
132143 $ this ->listener ->handle ($ event );
@@ -137,7 +148,7 @@ public function testAttemptAuthenticationUsernameTooLong()
137148 {
138149 $ this ->createListener ();
139150 $ username = str_repeat ('x ' , Security::MAX_USERNAME_LENGTH + 1 );
140- $ request = new Request (array (), array (), array (), array (), array (), array (), sprintf ('{"username": "%s", "password": 1} ' , $ username ));
151+ $ request = new Request (array (), array (), array (), array (), array (), array (' HTTP_CONTENT_TYPE ' => ' application/json ' ), sprintf ('{"username": "%s", "password": 1} ' , $ username ));
141152 $ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
142153
143154 $ this ->listener ->handle ($ event );
@@ -147,7 +158,18 @@ public function testAttemptAuthenticationUsernameTooLong()
147158 public function testDoesNotAttemptAuthenticationIfRequestPathDoesNotMatchCheckPath ()
148159 {
149160 $ this ->createListener (array ('check_path ' => '/ ' ), true , false );
150- $ request = new Request ();
161+ $ request = new Request (array (), array (), array (), array (), array (), array ('HTTP_CONTENT_TYPE ' => 'application/json ' ));
162+ $ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
163+ $ event ->setResponse (new Response ('original ' ));
164+
165+ $ this ->listener ->handle ($ event );
166+ $ this ->assertSame ('original ' , $ event ->getResponse ()->getContent ());
167+ }
168+
169+ public function testDoesNotAttemptAuthenticationIfRequestContentTypeIsNotJson ()
170+ {
171+ $ this ->createListener ();
172+ $ request = new Request (array (), array (), array (), array (), array (), array (), '{"username": "dunglas", "password": "foo"} ' );
151173 $ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
152174 $ event ->setResponse (new Response ('original ' ));
153175
@@ -158,7 +180,7 @@ public function testDoesNotAttemptAuthenticationIfRequestPathDoesNotMatchCheckPa
158180 public function testAttemptAuthenticationIfRequestPathMatchesCheckPath ()
159181 {
160182 $ this ->createListener (array ('check_path ' => '/ ' ));
161- $ request = new Request (array (), array (), array (), array (), array (), array (), '{"username": "dunglas", "password": "foo"} ' );
183+ $ request = new Request (array (), array (), array (), array (), array (), array (' HTTP_CONTENT_TYPE ' => ' application/json ' ), '{"username": "dunglas", "password": "foo"} ' );
162184 $ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
163185
164186 $ this ->listener ->handle ($ event );
0 commit comments