Navigation Menu

Skip to content

Commit

Permalink
Merge branch 'codeguy-4.x-remove-subrequest' into 4.x
Browse files Browse the repository at this point in the history
Closes #2078
Fixes #2044
  • Loading branch information
akrabat committed Nov 27, 2016
2 parents 76f63d2 + 263ce35 commit e94befb
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 88 deletions.
43 changes: 0 additions & 43 deletions Slim/App.php
Expand Up @@ -446,49 +446,6 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
return $notFoundHandler($request, $response);
}

/**
* Perform a sub-request from within an application route
*
* This method allows you to prepare and initiate a sub-request, run within
* the context of the current request. This WILL NOT issue a remote HTTP
* request. Instead, it will route the provided URL, method, headers,
* cookies, body, and server variables against the set of registered
* application routes. The result response object is returned.
*
* @param string $method The request method (e.g., GET, POST, PUT, etc.)
* @param string $path The request URI path
* @param string $query The request URI query string
* @param array $headers The request headers (key-value array)
* @param array $cookies The request cookies (key-value array)
* @param string $bodyContent The request body
* @param ResponseInterface $response The response object (optional)
* @return ResponseInterface
*/
public function subRequest(
$method,
$path,
$query = '',
array $headers = [],
array $cookies = [],
$bodyContent = '',
ResponseInterface $response = null
) {
$env = $this->container->get('environment');
$uri = Uri::createFromEnvironment($env)->withPath($path)->withQuery($query);
$headers = new Headers($headers);
$serverParams = $env->all();
$body = new Body(fopen('php://temp', 'r+'));
$body->write($bodyContent);
$body->rewind();
$request = new Request($method, $uri, $headers, $cookies, $serverParams, $body);

if (!$response) {
$response = $this->container->get('response');
}

return $this($request, $response);
}

/**
* Dispatch the router to find the route. Prepare the route for use.
*
Expand Down
45 changes: 0 additions & 45 deletions tests/AppTest.php
Expand Up @@ -1404,51 +1404,6 @@ public function testCurrentRequestAttributesAreNotLostWhenAddingRouteArgumentsRe
$this->assertEquals('1rob', (string)$resOut->getBody());
}

public function testInvokeSubRequest()
{
$app = new App();
$app->get('/foo', function ($req, $res) {
$res->write('foo');

return $res;
});

$newResponse = $subReq = $app->subRequest('GET', '/foo');

$this->assertEquals('foo', (string)$subReq->getBody());
$this->assertEquals(200, $newResponse->getStatusCode());
}

public function testInvokeSubRequestWithQuery()
{
$app = new App();
$app->get('/foo', function ($req, $res) {
$res->write("foo {$req->getParam('bar')}");

return $res;
});

$subReq = $app->subRequest('GET', '/foo', 'bar=bar');

$this->assertEquals('foo bar', (string)$subReq->getBody());
}

public function testInvokeSubRequestUsesResponseObject()
{
$app = new App();
$app->get('/foo', function ($req, $res) {
$res->write("foo {$req->getParam('bar')}");

return $res;
});

$resp = new Response(201);
$newResponse = $subReq = $app->subRequest('GET', '/foo', 'bar=bar', [], [], '', $resp);

$this->assertEquals('foo bar', (string)$subReq->getBody());
$this->assertEquals(201, $newResponse->getStatusCode());
}

// TODO: Test finalize()

// TODO: Test run()
Expand Down

0 comments on commit e94befb

Please sign in to comment.