Permalink
Browse files

Added support for Form & Header parameters.

  • Loading branch information...
1 parent 7eb87ba commit 64764e9be21a9de18368ac474994c1f026fe7305 @thizzle committed Feb 23, 2012
Showing with 94 additions and 3 deletions.
  1. +19 −0 src/Sonno/Dispatcher/Dispatcher.php
  2. +75 −3 tests/Sonno/Test/Application/ApplicationTest.php
@@ -97,10 +97,15 @@ public function dispatch(Route $route)
// construct a flat array of method arguments for the resource method
$pathParamValues = $this->_uriInfo->getPathParameters();
$queryParamValues = $this->_uriInfo->getQueryParameters();
+ $headerParamValues = $this->_request->getHeaders();
$pathParams = $route->getPathParams() ?: array();
$queryParams = $route->getQueryParams() ?: array();
+ $headerParams = $route->getHeaderParams() ?: array();
+ $formParams = $route->getFormParams() ?: array();
$resourceMethodArgs = array();
+ parse_str($this->_request->getRequestBody(), $formParamValues);
+
foreach ($reflMethod->getParameters() as $idx => $reflParam) {
$parameterName = $reflParam->getName();
@@ -117,6 +122,20 @@ public function dispatch(Route $route)
) {
$resourceMethodArgs[$idx] = $queryParamValues[$parameterName];
}
+
+ // search for an argument value in the Header parameter collection
+ if (in_array($parameterName, $headerParams)
+ && isset($headerParamValues[$parameterName])
+ ) {
+ $resourceMethodArgs[$idx] = $headerParamValues[$parameterName];
+ }
+
+ // search for an argument value in the Form parameter collection
+ if (in_array($parameterName, $formParams)
+ && isset($formParamValues[$parameterName])
+ ) {
+ $resourceMethodArgs[$idx] = $formParamValues[$parameterName];
+ }
}
// inject Context variables into the resource class instance
@@ -37,16 +37,25 @@ protected function setUp()
/**
* Generate a mock \Sonno\Http\Request\RequestInterface object.
*
- * @param $method string The HTTP request method.
- * @param $uri string The HTTP request URI.
+ * @param string $method The HTTP request method.
+ * @param string $uri The HTTP request URI.
+ * @param string $contentType The HTTP request body content type.
+ * @param string $selectedVariant The variant that the content negotiation
+ * should select.
+ * @param array $queryParams The HTTP request query string parameters.
+ * @param array $headers The array of HTTP header names to values.
+ * @param string $contentBody The HTTP request body content.
+ *
* @return Sonno\Http\Request\RequestInterface
*/
protected function buildMockRequest(
$method = null,
$uri = null,
$contentType = null,
$selectedVariant = null,
- $queryParams = array()
+ $queryParams = array(),
+ $headers = array(),
+ $contentBody = null
) {
$request = $this
->getMockBuilder('Sonno\Http\Request\Request')
@@ -93,11 +102,23 @@ protected function buildMockRequest(
}));
}
+ if ($contentBody) {
+ $request
+ ->expects($this->any())
+ ->method('getRequestBody')
+ ->will($this->returnValue($contentBody));
+ }
+
$request
->expects($this->any())
->method('getQueryParams')
->will($this->returnValue($queryParams));
+ $request
+ ->expects($this->any())
+ ->method('getHeaders')
+ ->will($this->returnValue($headers));
+
return $request;
}
@@ -403,6 +424,32 @@ public function testCookieParam()
*/
public function testFormParam()
{
+ $config = $this->buildMockConfiguration(array(
+ array(
+ 'path' => '/test/paper',
+ 'httpMethod' => 'GET',
+ 'resourceClassName' => 'Sonno\Test\Application\Asset\TestResource',
+ 'resourceMethodName' => 'modifyString',
+ 'produces' => array('text/plain'),
+ 'contexts' => array('_incomingRequest' => 'Request'),
+ 'formParams' => array('str'),
+ 'queryParams' => array('op'))
+ ), '/service/v1');
+ $request = $this->buildMockRequest(
+ 'GET',
+ '/service/v1/test/paper',
+ null,
+ new Variant('text/plain'),
+ array('op' => 'upper'),
+ array(),
+ 'str=hackeysackey'
+ );
+
+ $app = new Application($config);
+ $response = $app->run($request);
+
+ $this->assertEquals(200, $response->getStatusCode());
+ $this->assertEquals("HACKEYSACKEY|GET", $response->getContent());
}
/**
@@ -414,6 +461,31 @@ public function testFormParam()
*/
public function testHeaderParam()
{
+ $config = $this->buildMockConfiguration(array(
+ array(
+ 'path' => '/test/{str}',
+ 'httpMethod' => 'GET',
+ 'resourceClassName' => 'Sonno\Test\Application\Asset\TestResource',
+ 'resourceMethodName' => 'modifyString',
+ 'produces' => array('text/plain'),
+ 'contexts' => array('_incomingRequest' => 'Request'),
+ 'headerParams' => array('str'),
+ 'queryParams' => array('op'))
+ ), '/service/v1');
+ $request = $this->buildMockRequest(
+ 'GET',
+ '/service/v1/test/camelCase',
+ null,
+ new Variant('text/plain'),
+ array('op' => 'upper'),
+ array('str' => 'Cat')
+ );
+
+ $app = new Application($config);
+ $response = $app->run($request);
+
+ $this->assertEquals(200, $response->getStatusCode());
+ $this->assertEquals("CAT|GET", $response->getContent());
}
/**

0 comments on commit 64764e9

Please sign in to comment.