[Testing][HttpKernel] Added possibility to functional test raw body data
This commit is contained in:
parent
710a1e56b0
commit
1e3dc1479c
@ -179,11 +179,12 @@ abstract class Client
|
||||
* @param array $parameters The Request parameters
|
||||
* @param array $files The files
|
||||
* @param array $server The server parameters (HTTP headers are referenced with a HTTP_ prefix as PHP does)
|
||||
* @param string $content The raw body data
|
||||
* @param Boolean $changeHistory Whether to update the history or not (only used internally for back(), forward(), and reload())
|
||||
*
|
||||
* @return Crawler
|
||||
*/
|
||||
public function request($method, $uri, array $parameters = array(), array $files = array(), array $server = array(), $changeHistory = true)
|
||||
public function request($method, $uri, array $parameters = array(), array $files = array(), array $server = array(), $content = null, $changeHistory = true)
|
||||
{
|
||||
$uri = $this->getAbsoluteUri($uri);
|
||||
|
||||
@ -194,7 +195,7 @@ abstract class Client
|
||||
$server['HTTP_HOST'] = parse_url($uri, PHP_URL_HOST);
|
||||
$server['HTTPS'] = 'https' == parse_url($uri, PHP_URL_SCHEME);
|
||||
|
||||
$request = new Request($uri, $method, $parameters, $files, $this->cookieJar->getValues($uri), $server);
|
||||
$request = new Request($uri, $method, $parameters, $files, $this->cookieJar->getValues($uri), $server, $content);
|
||||
|
||||
$this->request = $this->filterRequest($request);
|
||||
|
||||
@ -378,6 +379,6 @@ abstract class Client
|
||||
*/
|
||||
protected function requestFromRequest(Request $request, $changeHistory = true)
|
||||
{
|
||||
return $this->request($request->getMethod(), $request->getUri(), $request->getParameters(), array(), $request->getFiles(), $request->getServer(), $changeHistory);
|
||||
return $this->request($request->getMethod(), $request->getUri(), $request->getParameters(), array(), $request->getFiles(), $request->getServer(), $request->getContent(), $changeHistory);
|
||||
}
|
||||
}
|
||||
|
@ -24,6 +24,7 @@ class Request
|
||||
protected $files;
|
||||
protected $cookies;
|
||||
protected $server;
|
||||
protected $content;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
@ -34,8 +35,9 @@ class Request
|
||||
* @param array $files An array of uploaded files
|
||||
* @param array $cookies An array of cookies
|
||||
* @param array $server An array of server parameters
|
||||
* @param string $content The raw body data
|
||||
*/
|
||||
public function __construct($uri, $method, array $parameters = array(), array $files = array(), array $cookies = array(), array $server = array())
|
||||
public function __construct($uri, $method, array $parameters = array(), array $files = array(), array $cookies = array(), array $server = array(), $content = null)
|
||||
{
|
||||
$this->uri = $uri;
|
||||
$this->method = $method;
|
||||
@ -43,6 +45,7 @@ class Request
|
||||
$this->files = $files;
|
||||
$this->cookies = $cookies;
|
||||
$this->server = $server;
|
||||
$this->content = $content;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -104,4 +107,14 @@ class Request
|
||||
{
|
||||
return $this->server;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the request raw body data.
|
||||
*
|
||||
* @return string The request raw body data.
|
||||
*/
|
||||
public function getContent()
|
||||
{
|
||||
return $this->content;
|
||||
}
|
||||
}
|
||||
|
@ -73,16 +73,17 @@ class Request
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $query The GET parameters
|
||||
* @param array $request The POST parameters
|
||||
* @param array $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
|
||||
* @param array $cookies The COOKIE parameters
|
||||
* @param array $files The FILES parameters
|
||||
* @param array $server The SERVER parameters
|
||||
* @param array $query The GET parameters
|
||||
* @param array $request The POST parameters
|
||||
* @param array $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
|
||||
* @param array $cookies The COOKIE parameters
|
||||
* @param array $files The FILES parameters
|
||||
* @param array $server The SERVER parameters
|
||||
* @param string $content The raw body data
|
||||
*/
|
||||
public function __construct(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array())
|
||||
public function __construct(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null)
|
||||
{
|
||||
$this->initialize($query, $request, $attributes, $cookies, $files, $server);
|
||||
$this->initialize($query, $request, $attributes, $cookies, $files, $server, $content);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -90,14 +91,15 @@ class Request
|
||||
*
|
||||
* This method also re-initializes all properties.
|
||||
*
|
||||
* @param array $query The GET parameters
|
||||
* @param array $request The POST parameters
|
||||
* @param array $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
|
||||
* @param array $cookies The COOKIE parameters
|
||||
* @param array $files The FILES parameters
|
||||
* @param array $server The SERVER parameters
|
||||
* @param array $query The GET parameters
|
||||
* @param array $request The POST parameters
|
||||
* @param array $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
|
||||
* @param array $cookies The COOKIE parameters
|
||||
* @param array $files The FILES parameters
|
||||
* @param array $server The SERVER parameters
|
||||
* @param string $content The raw body data
|
||||
*/
|
||||
public function initialize(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array())
|
||||
public function initialize(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null)
|
||||
{
|
||||
$this->request = new ParameterBag($request);
|
||||
$this->query = new ParameterBag($query);
|
||||
@ -107,7 +109,7 @@ class Request
|
||||
$this->server = new ServerBag($server);
|
||||
$this->headers = new HeaderBag($this->server->getHeaders());
|
||||
|
||||
$this->content = null;
|
||||
$this->content = $content;
|
||||
$this->languages = null;
|
||||
$this->charsets = null;
|
||||
$this->acceptableContentTypes = null;
|
||||
@ -138,10 +140,11 @@ class Request
|
||||
* @param array $cookies The request cookies ($_COOKIE)
|
||||
* @param array $files The request files ($_FILES)
|
||||
* @param array $server The server parameters ($_SERVER)
|
||||
* @param string $content The raw body data
|
||||
*
|
||||
* @return Request A Request instance
|
||||
*/
|
||||
static public function create($uri, $method = 'GET', $parameters = array(), $cookies = array(), $files = array(), $server = array())
|
||||
static public function create($uri, $method = 'GET', $parameters = array(), $cookies = array(), $files = array(), $server = array(), $content = null)
|
||||
{
|
||||
$defaults = array(
|
||||
'SERVER_NAME' => 'localhost',
|
||||
@ -204,7 +207,7 @@ class Request
|
||||
'QUERY_STRING' => $queryString,
|
||||
));
|
||||
|
||||
return new static($query, $request, array(), $cookies, $files, $server);
|
||||
return new static($query, $request, array(), $cookies, $files, $server, $content);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -100,7 +100,7 @@ EOF;
|
||||
$uri = '/'.$matches[2];
|
||||
}
|
||||
|
||||
return Request::create($uri, $request->getMethod(), $request->getParameters(), $request->getCookies(), $request->getFiles(), $request->getServer());
|
||||
return Request::create($uri, $request->getMethod(), $request->getParameters(), $request->getCookies(), $request->getFiles(), $request->getServer(), $request->getContent());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1146,11 +1146,11 @@ class Request
|
||||
protected $format;
|
||||
protected $session;
|
||||
static protected $formats;
|
||||
public function __construct(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array())
|
||||
public function __construct(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null)
|
||||
{
|
||||
$this->initialize($query, $request, $attributes, $cookies, $files, $server);
|
||||
$this->initialize($query, $request, $attributes, $cookies, $files, $server, $content);
|
||||
}
|
||||
public function initialize(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array())
|
||||
public function initialize(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null)
|
||||
{
|
||||
$this->request = new ParameterBag($request);
|
||||
$this->query = new ParameterBag($query);
|
||||
@ -1159,7 +1159,7 @@ class Request
|
||||
$this->files = new FileBag($files);
|
||||
$this->server = new ServerBag($server);
|
||||
$this->headers = new HeaderBag($this->server->getHeaders());
|
||||
$this->content = null;
|
||||
$this->content = $content;
|
||||
$this->languages = null;
|
||||
$this->charsets = null;
|
||||
$this->acceptableContentTypes = null;
|
||||
@ -1174,7 +1174,7 @@ class Request
|
||||
{
|
||||
return new static($_GET, $_POST, array(), $_COOKIE, $_FILES, $_SERVER);
|
||||
}
|
||||
static public function create($uri, $method = 'GET', $parameters = array(), $cookies = array(), $files = array(), $server = array())
|
||||
static public function create($uri, $method = 'GET', $parameters = array(), $cookies = array(), $files = array(), $server = array(), $content = null)
|
||||
{
|
||||
$defaults = array(
|
||||
'SERVER_NAME' => 'localhost',
|
||||
@ -1228,7 +1228,7 @@ class Request
|
||||
'REQUEST_URI' => $uri,
|
||||
'QUERY_STRING' => $queryString,
|
||||
));
|
||||
return new static($query, $request, array(), $cookies, $files, $server);
|
||||
return new static($query, $request, array(), $cookies, $files, $server, $content);
|
||||
}
|
||||
public function duplicate(array $query = null, array $request = null, array $attributes = null, array $cookies = null, array $files = null, array $server = null)
|
||||
{
|
||||
|
@ -1372,11 +1372,11 @@ class Request
|
||||
protected $format;
|
||||
protected $session;
|
||||
static protected $formats;
|
||||
public function __construct(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array())
|
||||
public function __construct(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null)
|
||||
{
|
||||
$this->initialize($query, $request, $attributes, $cookies, $files, $server);
|
||||
$this->initialize($query, $request, $attributes, $cookies, $files, $server, $content);
|
||||
}
|
||||
public function initialize(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array())
|
||||
public function initialize(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null)
|
||||
{
|
||||
$this->request = new ParameterBag($request);
|
||||
$this->query = new ParameterBag($query);
|
||||
@ -1385,7 +1385,7 @@ class Request
|
||||
$this->files = new FileBag($files);
|
||||
$this->server = new ServerBag($server);
|
||||
$this->headers = new HeaderBag($this->server->getHeaders());
|
||||
$this->content = null;
|
||||
$this->content = $content;
|
||||
$this->languages = null;
|
||||
$this->charsets = null;
|
||||
$this->acceptableContentTypes = null;
|
||||
@ -1400,7 +1400,7 @@ class Request
|
||||
{
|
||||
return new static($_GET, $_POST, array(), $_COOKIE, $_FILES, $_SERVER);
|
||||
}
|
||||
static public function create($uri, $method = 'GET', $parameters = array(), $cookies = array(), $files = array(), $server = array())
|
||||
static public function create($uri, $method = 'GET', $parameters = array(), $cookies = array(), $files = array(), $server = array(), $content = null)
|
||||
{
|
||||
$defaults = array(
|
||||
'SERVER_NAME' => 'localhost',
|
||||
@ -1454,7 +1454,7 @@ class Request
|
||||
'REQUEST_URI' => $uri,
|
||||
'QUERY_STRING' => $queryString,
|
||||
));
|
||||
return new static($query, $request, array(), $cookies, $files, $server);
|
||||
return new static($query, $request, array(), $cookies, $files, $server, $content);
|
||||
}
|
||||
public function duplicate(array $query = null, array $request = null, array $attributes = null, array $cookies = null, array $files = null, array $server = null)
|
||||
{
|
||||
|
@ -102,6 +102,18 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals('foo', $client->getResponse()->getContent(), '->getCrawler() returns the Response of the last request');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Symfony\Component\BrowserKit\Client::getContent
|
||||
*/
|
||||
public function testGetContent()
|
||||
{
|
||||
$json = '{"jsonrpc":"2.0","method":"echo","id":7,"params":["Hello World"]}';
|
||||
|
||||
$client = new TestClient();
|
||||
$client->request('POST', 'http://example.com/jsonrpc', array(), array(), array(), $json);
|
||||
$this->assertEquals($json, $client->getRequest()->getContent());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Symfony\Component\BrowserKit\Client::getCrawler
|
||||
*/
|
||||
|
@ -72,6 +72,10 @@ class RequestTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals('/foo', $request->getPathInfo());
|
||||
$this->assertEquals('test.com', $request->getHost());
|
||||
$this->assertEquals(90, $request->getPort());
|
||||
|
||||
$json = '{"jsonrpc":"2.0","method":"echo","id":7,"params":["Hello World"]}';
|
||||
$request = Request::create('http://example.com/jsonrpc', 'POST', array(), array(), array(), array(), $json);
|
||||
$this->assertEquals($json, $request->getContent());
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user