@@ -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 );
@@ -96,7 +107,7 @@ public function testUsePath()
96
107
public function testAttemptAuthenticationNoUsername ()
97
108
{
98
109
$ 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"} ' );
100
111
$ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
101
112
102
113
$ this ->listener ->handle ($ event );
@@ -106,7 +117,7 @@ public function testAttemptAuthenticationNoUsername()
106
117
public function testAttemptAuthenticationNoPassword ()
107
118
{
108
119
$ 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"} ' );
110
121
$ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
111
122
112
123
$ this ->listener ->handle ($ event );
@@ -116,7 +127,7 @@ public function testAttemptAuthenticationNoPassword()
116
127
public function testAttemptAuthenticationUsernameNotAString ()
117
128
{
118
129
$ 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"} ' );
120
131
$ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
121
132
122
133
$ this ->listener ->handle ($ event );
@@ -126,7 +137,7 @@ public function testAttemptAuthenticationUsernameNotAString()
126
137
public function testAttemptAuthenticationPasswordNotAString ()
127
138
{
128
139
$ 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} ' );
130
141
$ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
131
142
132
143
$ this ->listener ->handle ($ event );
@@ -137,7 +148,7 @@ public function testAttemptAuthenticationUsernameTooLong()
137
148
{
138
149
$ this ->createListener ();
139
150
$ 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 ));
141
152
$ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
142
153
143
154
$ this ->listener ->handle ($ event );
@@ -147,7 +158,18 @@ public function testAttemptAuthenticationUsernameTooLong()
147
158
public function testDoesNotAttemptAuthenticationIfRequestPathDoesNotMatchCheckPath ()
148
159
{
149
160
$ 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"} ' );
151
173
$ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
152
174
$ event ->setResponse (new Response ('original ' ));
153
175
@@ -158,7 +180,7 @@ public function testDoesNotAttemptAuthenticationIfRequestPathDoesNotMatchCheckPa
158
180
public function testAttemptAuthenticationIfRequestPathMatchesCheckPath ()
159
181
{
160
182
$ 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"} ' );
162
184
$ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
163
185
164
186
$ this ->listener ->handle ($ event );
0 commit comments