Browse Source

Throw NotFoundException when extension-based content negotation fails

Corey Taylor 3 years ago
parent
commit
ebc8a80304
2 changed files with 17 additions and 0 deletions
  1. 2 0
      src/Controller/Controller.php
  2. 15 0
      tests/TestCase/Controller/ControllerTest.php

+ 2 - 0
src/Controller/Controller.php

@@ -826,6 +826,8 @@ class Controller implements EventListenerInterface, EventDispatcherInterface
                     return $typeMap[$extType];
                 }
             }
+
+            throw new NotFoundException();
         }
 
         // Use accept header based negotiation.

+ 15 - 0
tests/TestCase/Controller/ControllerTest.php

@@ -21,6 +21,7 @@ use Cake\Controller\Exception\MissingActionException;
 use Cake\Core\Configure;
 use Cake\Event\Event;
 use Cake\Event\EventInterface;
+use Cake\Http\Exception\NotFoundException;
 use Cake\Http\Response;
 use Cake\Http\ServerRequest;
 use Cake\Routing\Router;
@@ -396,6 +397,20 @@ class ControllerTest extends TestCase
         $this->assertTextStartsWith('<?xml', $response->getBody() . '', 'Body should be xml');
     }
 
+    public function testRenderViewClassesMineExtMissingView()
+    {
+        $request = new ServerRequest([
+            'url' => '/',
+            'environment' => [],
+            'params' => ['plugin' => null, 'controller' => 'ContentTypes', 'action' => 'all', '_ext' => 'json'],
+        ]);
+        $controller = new ContentTypesController($request, new Response());
+        $controller->plain();
+
+        $this->expectException(NotFoundException::class);
+        $response = $controller->render();
+    }
+
     /**
      * test view rendering changing response
      */