Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit df6deac

Browse files
committed
Test for valid request JSON before showing Pretty tab
1 parent 63c0219 commit df6deac

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/request.html.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,12 @@
179179
</div>
180180
{% elseif collector.content %}
181181
<div class="sf-tabs">
182-
{% if collector.isJsonRequest and collector.prettyJsonContent != "null" %}
182+
{% if collector.isJsonRequest and collector.prettyJson.valid %}
183183
<div class="tab">
184184
<h3 class="tab-title">Pretty</h3>
185185
<div class="tab-content">
186186
<div class="card" style="max-height: 500px; overflow-y: auto;">
187-
<pre class="break-long-words">{{ collector.prettyJsonContent }}</pre>
187+
<pre class="break-long-words">{{ collector.prettyJson.content }}</pre>
188188
</div>
189189
</div>
190190
</div>

src/Symfony/Component/HttpKernel/DataCollector/RequestDataCollector.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,16 @@ public function getContent()
263263
return $this->data['content'];
264264
}
265265

266-
public function getPrettyJsonContent()
266+
public function getPrettyJson()
267267
{
268-
return json_encode(json_decode($this->getContent()), JSON_PRETTY_PRINT);
268+
$decoded = json_decode($this->getContent());
269+
$valid = JSON_ERROR_NONE === json_last_error();
270+
$content = json_encode($decoded, JSON_PRETTY_PRINT);
271+
272+
return array(
273+
'valid' => $valid,
274+
'content' => $content,
275+
);
269276
}
270277

271278
public function getContentType()

src/Symfony/Component/HttpKernel/Tests/DataCollector/RequestDataCollectorTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,4 +361,30 @@ public function provideJsonContentTypes()
361361
array('', false),
362362
);
363363
}
364+
365+
/**
366+
* @dataProvider providePrettyJson
367+
*/
368+
public function testGetPrettyJsonValidity($content, $expected)
369+
{
370+
$response = $this->createResponse();
371+
$request = Request::create('/', 'POST', [], [], [], [], $content);
372+
373+
$c = new RequestDataCollector();
374+
$c->collect($request, $response);
375+
376+
$this->assertSame($expected, $c->getPrettyJson());
377+
}
378+
379+
public function providePrettyJson()
380+
{
381+
return array(
382+
array('null', array('valid' => true, 'content' => 'null')),
383+
array('{ "foo": "bar" }', array('valid' => true, 'content' => '{
384+
"foo": "bar"
385+
}')),
386+
array('{ "abc" }', array('valid' => false, 'content' => 'null')),
387+
array('', array('valid' => false, 'content' => 'null')),
388+
);
389+
}
364390
}

0 commit comments

Comments
 (0)