8000
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 9a234e3 + 0c492d9 commit e2a7971Copy full SHA for e2a7971
DataCollector/RequestDataCollector.php
@@ -252,6 +252,18 @@ public function getContent()
252
return $this->data['content'];
253
}
254
255
+ public function isJsonRequest()
256
+ {
257
+ return 1 === preg_match('{^application/(?:\w+\++)*json$}i', $this->data['request_headers']['content-type']);
258
+ }
259
+
260
+ public function getPrettyJson()
261
262
+ $decoded = json_decode($this->getContent());
263
264
+ return JSON_ERROR_NONE === json_last_error() ? json_encode($decoded, JSON_PRETTY_PRINT) : null;
265
266
267
public function getContentType()
268
{
269
return $this->data['content_type'];
Tests/DataCollector/RequestDataCollectorTest.php
@@ -333,4 +333,58 @@ private function getCookieByName(Response $response, $name)
333
334
throw new \InvalidArgumentException(sprintf('Cookie named "%s" is not in response', $name));
335
336
337
+ /**
338
+ * @dataProvider provideJsonContentTypes
339
+ */
340
+ public function testIsJson($contentType, $expected)
341
342
+ $response = $this->createResponse();
343
+ $request = $this->createRequest();
344
+ $request->headers->set('Content-Type', $contentType);
345
346
+ $c = new RequestDataCollector();
347
+ $c->collect($request, $response);
348
349
+ $this->assertSame($expected, $c->isJsonRequest());
350
351
352
+ public function provideJsonContentTypes()
353
354
+ return array(
355
+ array('text/csv', false),
356
+ array('application/json', true),
357
+ array('application/JSON', true),
358
+ array('application/hal+json', true),
359
+ array('application/xml+json', true),
360
+ array('application/xml', false),
361
+ array('', false),
362
+ );
363
364
365
366
+ * @dataProvider providePrettyJson
367
368
+ public function testGetPrettyJsonValidity($content, $expected)
369
370
371
+ $request = Request::create('/', 'POST', array(), array(), array(), array(), $content);
372
373
374
375
376
+ $this->assertSame($expected, $c->getPrettyJson());
377
378
379
+ public function providePrettyJson()
380
381
382
+ array('null', 'null'),
383
+ array('{ "foo": "bar" }', '{
384
+ "foo": "bar"
385
+}'),
386
+ array('{ "abc" }', null),
387
+ array('', null),
388
389
390