mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-11 19:20:01 +08:00
Fixes #7258: Response was sending HTML content type when formatter was set to JSON or XML, nulls were handled wrong
This commit is contained in:
@@ -15,6 +15,7 @@ Yii Framework 2 Change Log
|
|||||||
- Bug #7226: `yii\web\Request::getEtag()` should strip off `-gzip` which may be added by Apache (mcd-php)
|
- Bug #7226: `yii\web\Request::getEtag()` should strip off `-gzip` which may be added by Apache (mcd-php)
|
||||||
- Bug #7227: Query builder should respect column alias setting when `yii\db\Expression` is being selected (mdmunir, qiangxue)
|
- Bug #7227: Query builder should respect column alias setting when `yii\db\Expression` is being selected (mdmunir, qiangxue)
|
||||||
- Bug #7271: `ActiveRecord::populateRecord()` should be called in late binding approach (jlorente)
|
- Bug #7271: `ActiveRecord::populateRecord()` should be called in late binding approach (jlorente)
|
||||||
|
- Bug #7258: Response was sending HTML content type when formatter was set to JSON or XML, nulls were handled wrong (slavcodev, samdark)
|
||||||
- Bug #7358: Fix trimming PHPDoc prefix with TAB indent in `yii\console\Controller::parseDocCommentSummary()` (gugglegum)
|
- Bug #7358: Fix trimming PHPDoc prefix with TAB indent in `yii\console\Controller::parseDocCommentSummary()` (gugglegum)
|
||||||
- Enh #3168: Improved the performance of `yii\rbac\DbManager::checkAccess()` by caching mechanism (qiangxue)
|
- Enh #3168: Improved the performance of `yii\rbac\DbManager::checkAccess()` by caching mechanism (qiangxue)
|
||||||
- Enh #4710: Added `yii\web\AssetManager::appendTimestamp` to support cache busting for assets (qiangxue)
|
- Enh #4710: Added `yii\web\AssetManager::appendTimestamp` to support cache busting for assets (qiangxue)
|
||||||
|
|||||||
77
tests/unit/framework/web/FormatterTest.php
Normal file
77
tests/unit/framework/web/FormatterTest.php
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace yiiunit\framework\web;
|
||||||
|
|
||||||
|
use yii\web\Response;
|
||||||
|
use yii\web\ResponseFormatterInterface;
|
||||||
|
|
||||||
|
abstract class FormatterTest extends \yiiunit\TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var Response
|
||||||
|
*/
|
||||||
|
public $response;
|
||||||
|
/**
|
||||||
|
* @var ResponseFormatterInterface
|
||||||
|
*/
|
||||||
|
public $formatter;
|
||||||
|
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
$this->mockApplication();
|
||||||
|
$this->response = new Response;
|
||||||
|
$this->formatter = $this->getFormatterInstance();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return ResponseFormatterInterface
|
||||||
|
*/
|
||||||
|
abstract protected function getFormatterInstance();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Formatter should not format null
|
||||||
|
*/
|
||||||
|
public function testFormatNull()
|
||||||
|
{
|
||||||
|
$this->response->data = null;
|
||||||
|
$this->formatter->format($this->response);
|
||||||
|
$this->assertEquals(null, $this->response->content);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $data the data to be formatted
|
||||||
|
* @param string $json the expected JSON body
|
||||||
|
* @dataProvider formatScalarDataProvider
|
||||||
|
*/
|
||||||
|
public function testFormatScalar($data, $json)
|
||||||
|
{
|
||||||
|
$this->response->data = $data;
|
||||||
|
$this->formatter->format($this->response);
|
||||||
|
$this->assertEquals($json, $this->response->content);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $data the data to be formatted
|
||||||
|
* @param string $json the expected JSON body
|
||||||
|
* @dataProvider formatArrayDataProvider
|
||||||
|
*/
|
||||||
|
public function testFormatArrays($data, $json)
|
||||||
|
{
|
||||||
|
$this->response->data = $data;
|
||||||
|
$this->formatter->format($this->response);
|
||||||
|
$this->assertEquals($json, $this->response->content);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $data the data to be formatted
|
||||||
|
* @param string $json the expected JSON body
|
||||||
|
* @dataProvider formatObjectDataProvider
|
||||||
|
*/
|
||||||
|
public function testFormatObjects($data, $json)
|
||||||
|
{
|
||||||
|
$this->response->data = $data;
|
||||||
|
$this->formatter->format($this->response);
|
||||||
|
$this->assertEquals($json, $this->response->content);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,21 +7,7 @@
|
|||||||
|
|
||||||
namespace yiiunit\framework\web;
|
namespace yiiunit\framework\web;
|
||||||
|
|
||||||
use yii\base\Object;
|
|
||||||
use yii\web\JsonResponseFormatter;
|
use yii\web\JsonResponseFormatter;
|
||||||
use yii\web\Response;
|
|
||||||
|
|
||||||
class Post extends Object
|
|
||||||
{
|
|
||||||
public $id;
|
|
||||||
public $title;
|
|
||||||
|
|
||||||
public function __construct($id, $title)
|
|
||||||
{
|
|
||||||
$this->id = $id;
|
|
||||||
$this->title = $title;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Alexander Makarov <sam@rmcreative.ru>
|
* @author Alexander Makarov <sam@rmcreative.ru>
|
||||||
@@ -29,40 +15,19 @@ class Post extends Object
|
|||||||
*
|
*
|
||||||
* @group web
|
* @group web
|
||||||
*/
|
*/
|
||||||
class JsonResponseFormatterTest extends \yiiunit\TestCase
|
class JsonResponseFormatterTest extends FormatterTest
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var Response
|
* @return JsonResponseFormatter
|
||||||
*/
|
*/
|
||||||
public $response;
|
protected function getFormatterInstance()
|
||||||
/**
|
|
||||||
* @var JsonResponseFormatter
|
|
||||||
*/
|
|
||||||
public $formatter;
|
|
||||||
|
|
||||||
protected function setUp()
|
|
||||||
{
|
{
|
||||||
$this->mockApplication();
|
return new JsonResponseFormatter();
|
||||||
$this->response = new Response;
|
|
||||||
$this->formatter = new JsonResponseFormatter;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $data the data to be formatted
|
|
||||||
* @param string $json the expected JSON body
|
|
||||||
* @dataProvider formatScalarDataProvider
|
|
||||||
*/
|
|
||||||
public function testFormatScalar($data, $json)
|
|
||||||
{
|
|
||||||
$this->response->data = $data;
|
|
||||||
$this->formatter->format($this->response);
|
|
||||||
$this->assertEquals($json, $this->response->content);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function formatScalarDataProvider()
|
public function formatScalarDataProvider()
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
[null, 'null'],
|
|
||||||
[1, 1],
|
[1, 1],
|
||||||
['abc', '"abc"'],
|
['abc', '"abc"'],
|
||||||
[true, 'true'],
|
[true, 'true'],
|
||||||
@@ -70,18 +35,6 @@ class JsonResponseFormatterTest extends \yiiunit\TestCase
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $data the data to be formatted
|
|
||||||
* @param string $json the expected JSON body
|
|
||||||
* @dataProvider formatArrayDataProvider
|
|
||||||
*/
|
|
||||||
public function testFormatArrays($data, $json)
|
|
||||||
{
|
|
||||||
$this->response->data = $data;
|
|
||||||
$this->formatter->format($this->response);
|
|
||||||
$this->assertEquals($json, $this->response->content);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function formatArrayDataProvider()
|
public function formatArrayDataProvider()
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
@@ -106,18 +59,6 @@ class JsonResponseFormatterTest extends \yiiunit\TestCase
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $data the data to be formatted
|
|
||||||
* @param string $json the expected JSON body
|
|
||||||
* @dataProvider formatObjectDataProvider
|
|
||||||
*/
|
|
||||||
public function testFormatObjects($data, $json)
|
|
||||||
{
|
|
||||||
$this->response->data = $data;
|
|
||||||
$this->formatter->format($this->response);
|
|
||||||
$this->assertEquals($json, $this->response->content);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function formatObjectDataProvider()
|
public function formatObjectDataProvider()
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
|||||||
16
tests/unit/framework/web/Post.php
Normal file
16
tests/unit/framework/web/Post.php
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
namespace yiiunit\framework\web;
|
||||||
|
|
||||||
|
use yii\base\Object;
|
||||||
|
|
||||||
|
class Post extends Object
|
||||||
|
{
|
||||||
|
public $id;
|
||||||
|
public $title;
|
||||||
|
|
||||||
|
public function __construct($id, $title)
|
||||||
|
{
|
||||||
|
$this->id = $id;
|
||||||
|
$this->title = $title;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,86 +7,47 @@
|
|||||||
|
|
||||||
namespace yiiunit\framework\web;
|
namespace yiiunit\framework\web;
|
||||||
|
|
||||||
use yii\base\Object;
|
|
||||||
use yii\web\Response;
|
|
||||||
use yii\web\XmlResponseFormatter;
|
use yii\web\XmlResponseFormatter;
|
||||||
|
|
||||||
class Post extends Object
|
|
||||||
{
|
|
||||||
public $id;
|
|
||||||
public $title;
|
|
||||||
|
|
||||||
public function __construct($id, $title)
|
|
||||||
{
|
|
||||||
$this->id = $id;
|
|
||||||
$this->title = $title;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Qiang Xue <qiang.xue@gmail.com>
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||||||
* @since 2.0
|
* @since 2.0
|
||||||
*
|
*
|
||||||
* @group web
|
* @group web
|
||||||
*/
|
*/
|
||||||
class XmlResponseFormatterTest extends \yiiunit\TestCase
|
class XmlResponseFormatterTest extends FormatterTest
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var Response
|
* @return XmlResponseFormatter
|
||||||
*/
|
*/
|
||||||
public $response;
|
protected function getFormatterInstance()
|
||||||
/**
|
|
||||||
* @var XmlResponseFormatter
|
|
||||||
*/
|
|
||||||
public $formatter;
|
|
||||||
|
|
||||||
protected function setUp()
|
|
||||||
{
|
{
|
||||||
$this->mockApplication();
|
return new XmlResponseFormatter();
|
||||||
$this->response = new Response;
|
|
||||||
$this->formatter = new XmlResponseFormatter;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private $xmlHead = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
|
||||||
* @param mixed $data the data to be formatted
|
|
||||||
* @param string $xml the expected XML body
|
private function addXmlHead(array $data)
|
||||||
* @dataProvider formatScalarDataProvider
|
|
||||||
*/
|
|
||||||
public function testFormatScalar($data, $xml)
|
|
||||||
{
|
{
|
||||||
$head = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
|
foreach ($data as &$item) {
|
||||||
$this->response->data = $data;
|
$item[1] = $this->xmlHead . $item[1];
|
||||||
$this->formatter->format($this->response);
|
}
|
||||||
$this->assertEquals($head . $xml, $this->response->content);
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function formatScalarDataProvider()
|
public function formatScalarDataProvider()
|
||||||
{
|
{
|
||||||
return [
|
return $this->addXmlHead([
|
||||||
[null, "<response></response>\n"],
|
|
||||||
[1, "<response>1</response>\n"],
|
[1, "<response>1</response>\n"],
|
||||||
['abc', "<response>abc</response>\n"],
|
['abc', "<response>abc</response>\n"],
|
||||||
[true, "<response>1</response>\n"],
|
[true, "<response>1</response>\n"],
|
||||||
["<>", "<response><></response>\n"],
|
["<>", "<response><></response>\n"],
|
||||||
];
|
]);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $data the data to be formatted
|
|
||||||
* @param string $xml the expected XML body
|
|
||||||
* @dataProvider formatArrayDataProvider
|
|
||||||
*/
|
|
||||||
public function testFormatArrays($data, $xml)
|
|
||||||
{
|
|
||||||
$head = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
|
|
||||||
$this->response->data = $data;
|
|
||||||
$this->formatter->format($this->response);
|
|
||||||
$this->assertEquals($head . $xml, $this->response->content);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function formatArrayDataProvider()
|
public function formatArrayDataProvider()
|
||||||
{
|
{
|
||||||
return [
|
return $this->addXmlHead([
|
||||||
[[], "<response/>\n"],
|
[[], "<response/>\n"],
|
||||||
[[1, 'abc'], "<response><item>1</item><item>abc</item></response>\n"],
|
[[1, 'abc'], "<response><item>1</item><item>abc</item></response>\n"],
|
||||||
[[
|
[[
|
||||||
@@ -105,25 +66,12 @@ class XmlResponseFormatterTest extends \yiiunit\TestCase
|
|||||||
'c' => [2, '<>'],
|
'c' => [2, '<>'],
|
||||||
true,
|
true,
|
||||||
], "<response><a>1</a><b>abc</b><c><item>2</item><item><></item></c><item>1</item></response>\n"],
|
], "<response><a>1</a><b>abc</b><c><item>2</item><item><></item></c><item>1</item></response>\n"],
|
||||||
];
|
]);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $data the data to be formatted
|
|
||||||
* @param string $xml the expected XML body
|
|
||||||
* @dataProvider formatObjectDataProvider
|
|
||||||
*/
|
|
||||||
public function testFormatObjects($data, $xml)
|
|
||||||
{
|
|
||||||
$head = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
|
|
||||||
$this->response->data = $data;
|
|
||||||
$this->formatter->format($this->response);
|
|
||||||
$this->assertEquals($head . $xml, $this->response->content);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function formatObjectDataProvider()
|
public function formatObjectDataProvider()
|
||||||
{
|
{
|
||||||
return [
|
return $this->addXmlHead([
|
||||||
[new Post(123, 'abc'), "<response><Post><id>123</id><title>abc</title></Post></response>\n"],
|
[new Post(123, 'abc'), "<response><Post><id>123</id><title>abc</title></Post></response>\n"],
|
||||||
[[
|
[[
|
||||||
new Post(123, 'abc'),
|
new Post(123, 'abc'),
|
||||||
@@ -133,6 +81,6 @@ class XmlResponseFormatterTest extends \yiiunit\TestCase
|
|||||||
new Post(123, '<>'),
|
new Post(123, '<>'),
|
||||||
'a' => new Post(456, 'def'),
|
'a' => new Post(456, 'def'),
|
||||||
], "<response><Post><id>123</id><title><></title></Post><a><Post><id>456</id><title>def</title></Post></a></response>\n"],
|
], "<response><Post><id>123</id><title><></title></Post><a><Post><id>456</id><title>def</title></Post></a></response>\n"],
|
||||||
];
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user