@@ -34,7 +34,7 @@ protected function setUp(): void
34
34
{
35
35
$ this ->service = $ this ->createMock (TraceServiceInterface::class);
36
36
$ this ->storage = $ this ->createMock (TraceStorageInterface::class);
37
- $ this ->listener = new TraceSubscriber (true , true , $ this ->service , $ this ->storage );
37
+ $ this ->listener = new TraceSubscriber (true , null , true , null , $ this ->service , $ this ->storage );
38
38
39
39
$ this ->dispatcher = new EventDispatcher ();
40
40
$ this ->dispatcher ->addSubscriber ($ this ->listener );
@@ -74,7 +74,7 @@ public function testListenerSetsTheTraceToStorageWhenFoundInRequestHeaders(): vo
74
74
public function testListenerIgnoresIncomingRequestHeadersWhenTrustRequestIsFalse (): void
75
75
{
76
76
$ this ->dispatcher ->removeSubscriber ($ this ->listener );
77
- $ this ->dispatcher ->addSubscriber (new TraceSubscriber (false , true , $ this ->service , $ this ->storage ));
77
+ $ this ->dispatcher ->addSubscriber (new TraceSubscriber (false , null , true , null , $ this ->service , $ this ->storage ));
78
78
79
79
$ trace = new TraceContext ();
80
80
$ this ->service ->expects (static ::never ())->method ('supports ' );
@@ -138,7 +138,7 @@ public function testRequestSetsIdOnResponse(): void
138
138
public function testListenerDoesNothingToResponseWithoutMasterRequestWhenSendResponseHeaderIsFalse (): void
139
139
{
140
140
$ this ->dispatcher ->removeSubscriber ($ this ->listener );
141
- $ this ->dispatcher ->addSubscriber (new TraceSubscriber (false , false , $ this ->service , $ this ->storage ));
141
+ $ this ->dispatcher ->addSubscriber (new TraceSubscriber (false , null , false , null , $ this ->service , $ this ->storage ));
142
142
143
143
$ this ->storage ->expects (static ::never ())->method ('getTrace ' );
144
144
$ this ->service ->expects (static ::never ())->method ('handleResponse ' );
@@ -148,4 +148,66 @@ public function testListenerDoesNothingToResponseWithoutMasterRequestWhenSendRes
148
148
KernelEvents::RESPONSE
149
149
);
150
150
}
151
+
152
+ public function testListenerSetsTheTraceToStorageWhenFoundInTrustedRequestHeaders (): void
153
+ {
154
+ $ listener = new TraceSubscriber (true , '127.0.0.1 ' , true , '127.0.0.1 ' , $ this ->service , $ this ->storage );
155
+ $ dispatcher = new EventDispatcher ();
156
+ $ dispatcher ->addSubscriber ($ listener );
157
+
158
+ $ this ->service ->expects (static ::once ())->method ('getRequestTrace ' )->with ($ this ->request );
159
+ $ this ->service ->expects (static ::once ())->method ('supports ' )->with ($ this ->request )->willReturn (true );
160
+ $ this ->storage ->expects (static ::once ())->method ('setTrace ' );
161
+
162
+ $ dispatcher ->dispatch (
163
+ new RequestEvent ($ this ->kernel , $ this ->request , HttpKernelInterface::MAIN_REQUEST ),
164
+ KernelEvents::REQUEST
165
+ );
166
+ }
167
+
168
+ public function testListenerIgnoresRequestHeadersOnNonTrustedRequest (): void
169
+ {
170
+ $ listener = new TraceSubscriber (true , '127.0.0.2 ' , true , '127.0.0.2 ' , $ this ->service , $ this ->storage );
171
+ $ dispatcher = new EventDispatcher ();
172
+ $ dispatcher ->addSubscriber ($ listener );
173
+
174
+ $ this ->service ->expects (static ::never ())->method ('supports ' );
175
+ $ this ->service ->expects (static ::never ())->method ('getRequestTrace ' );
176
+
177
+ $ dispatcher ->dispatch (
178
+ new RequestEvent ($ this ->kernel , $ this ->request , HttpKernelInterface::MAIN_REQUEST ),
179
+ KernelEvents::REQUEST
180
+ );
181
+ }
182
+
183
+ public function testRequestSetsIdOnResponseOnTrustedIp (): void
184
+ {
185
+ $ listener = new TraceSubscriber (true , '127.0.0.1 ' , true , '127.0.0.1 ' , $ this ->service , $ this ->storage );
186
+ $ dispatcher = new EventDispatcher ();
187
+ $ dispatcher ->addSubscriber ($ listener );
188
+
189
+ $ trace = new TraceContext ();
190
+ $ this ->storage ->expects (static ::once ())->method ('getTrace ' )->willReturn ($ trace );
191
+ $ this ->service ->expects (static ::once ())->method ('handleResponse ' )->with ($ this ->response , $ trace );
192
+
193
+ $ dispatcher ->dispatch (
194
+ new ResponseEvent ($ this ->kernel , $ this ->request , HttpKernelInterface::MAIN_REQUEST , $ this ->response ),
195
+ KernelEvents::RESPONSE
196
+ );
197
+ }
198
+
199
+ public function testRequestDoesNotSetIdOnResponseOnNonTrustedIp (): void
200
+ {
201
+ $ listener = new TraceSubscriber (true , '127.0.0.2 ' , true , '127.0.0.2 ' , $ this ->service , $ this ->storage );
202
+ $ dispatcher = new EventDispatcher ();
203
+ $ dispatcher ->addSubscriber ($ listener );
204
+
205
+ $ this ->storage ->expects (static ::never ())->method ('getTrace ' );
206
+ $ this ->service ->expects (static ::never ())->method ('handleResponse ' );
207
+
208
+ $ dispatcher ->dispatch (
209
+ new ResponseEvent ($ this ->kernel , $ this ->request , HttpKernelInterface::MAIN_REQUEST , $ this ->response ),
210
+ KernelEvents::RESPONSE
211
+ );
212
+ }
151
213
}
0 commit comments