Skip to content

Commit d8066c9

Browse files
author
Hunter Skrasek
committed
Update Dispatcher to only throw InternalHttpException if the response is
not successful, and not a redirect. References dingo#998. Closes dingo#1014. References dingo#1024
1 parent 311a4a9 commit d8066c9

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

src/Dispatcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ protected function dispatch(InternalRequest $request)
539539

540540
$response = $this->router->dispatch($request);
541541

542-
if (! $response->isSuccessful()) {
542+
if (! $response->isSuccessful() && ! $response->isRedirection()) {
543543
throw new InternalHttpException($response);
544544
} elseif (! $this->raw) {
545545
$response = $response->getOriginalContent();

tests/DispatcherTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Dingo\Api\Tests;
44

5+
use Dingo\Api\Exception\ValidationHttpException;
56
use Mockery as m;
67
use Dingo\Api\Http;
78
use Dingo\Api\Auth\Auth;
@@ -368,4 +369,46 @@ public function testUsingRequestFacadeDoesNotCacheRequestInstance()
368369
$this->assertSame('bar', $response);
369370
$this->assertNull(RequestFacade::input('foo'));
370371
}
372+
373+
public function testRedirectResponseThrowsException()
374+
{
375+
$this->router->version('v1', function () {
376+
$this->router->get('redirect', function () {
377+
return new \Illuminate\Http\RedirectResponse('redirect-test');
378+
});
379+
});
380+
381+
$response = $this->dispatcher->get('redirect');
382+
$this->assertInstanceOf('Illuminate\Http\RedirectResponse', $response);
383+
$this->assertSame('redirect-test', $response->getTargetUrl());
384+
}
385+
386+
/**
387+
* @expectedException \Dingo\Api\Exception\InternalHttpException
388+
*/
389+
public function testNotOkJsonResponseThrowsException()
390+
{
391+
$this->router->version('v1', function () {
392+
$this->router->get('json', function () {
393+
return new \Illuminate\Http\JsonResponse(['is' => 'json'], 422);
394+
});
395+
});
396+
397+
$this->dispatcher->get('json');
398+
}
399+
400+
/**
401+
* @expectedException \Dingo\Api\Exception\ValidationHttpException
402+
*/
403+
public function testFormRequestValidationFailureThrowsValidationException()
404+
{
405+
$this->router->version('v1', function () {
406+
$this->router->get('fail', function () {
407+
//Mocking the form validation call is challenging at the moment, so next best thing
408+
throw new ValidationHttpException(['foo' => 'bar']);
409+
});
410+
});
411+
412+
$this->dispatcher->get('fail');
413+
}
371414
}

tests/Stubs/RoutingAdapterStub.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Closure;
66
use Illuminate\Container\Container;
7+
use Illuminate\Http\JsonResponse;
78
use Illuminate\Http\Request;
89
use Dingo\Api\Http\Response;
910
use Dingo\Api\Contract\Routing\Adapter;
@@ -45,8 +46,10 @@ protected function findRouteClosure(array $action)
4546

4647
protected function prepareResponse($request, $response)
4748
{
48-
if (! $response instanceof Response && ! $response instanceof IlluminateResponse) {
49-
$response = new Response($response);
49+
if ($response instanceof IlluminateResponse) {
50+
$response = Response::makeFromExisting($response);
51+
} elseif ($response instanceof JsonResponse) {
52+
$response = Response::makeFromJson($response);
5053
}
5154

5255
return $response->prepare($request);

0 commit comments

Comments
 (0)