diff --git a/src/ShopifyApp/Middleware/AuthShop.php b/src/ShopifyApp/Middleware/AuthShop.php index bc45a7c0..e8bf1736 100644 --- a/src/ShopifyApp/Middleware/AuthShop.php +++ b/src/ShopifyApp/Middleware/AuthShop.php @@ -47,7 +47,7 @@ public function handle(Request $request, Closure $next) // Shop is OK, move on... $response = $next($request); - if (!$request->ajax()) { + if (($request->ajax() || $request->wantsJson() || $request->isJson()) === false) { // Request is not AJAX, continue as normal if (!$response instanceof Response && !$response instanceof RedirectResponse) { // We need a response object to modify headers diff --git a/tests/Middleware/AuthShopMiddlewareTest.php b/tests/Middleware/AuthShopMiddlewareTest.php index 2405ac0b..2fa1f39c 100644 --- a/tests/Middleware/AuthShopMiddlewareTest.php +++ b/tests/Middleware/AuthShopMiddlewareTest.php @@ -90,7 +90,7 @@ public function testHeadersForEsdkShouldBeAdjusted() Session::put('shopify_domain', $shop->shopify_domain); // Run the middleware - $result = $this->runAuthShop(); + $result = $this->runAuthShop(null, Request::instance()); // Assert the headers were modified $this->assertEquals('CP="Not used"', $result[0]->headers->get('p3p')); @@ -114,6 +114,23 @@ public function testAjaxCallShouldNotAdjustResponse() $this->assertNull($result[0]); } + public function testJsonCallShouldNotAdjustResponse() + { + // Set a shop + $shop = factory(Shop::class)->create(); + Session::put('shopify_domain', $shop->shopify_domain); + + // Set the request + $request = Request::instance(); + $request->headers->set('content-type', 'application/json'); + + // Run the middleware + $result = $this->runAuthShop(null, $request); + + // Assert the headers were not modified + $this->assertNull($result[0]); + } + public function testHeadersForDisabledEsdk() { // Set a shop @@ -124,7 +141,7 @@ public function testHeadersForDisabledEsdk() Config::set('shopify-app.esdk_enabled', false); // Run the middleware - $result = $this->runAuthShop(); + $result = $this->runAuthShop(null, Request::instance()); // Assert the headers were not modified $this->assertNull($result[0]->headers->get('p3p'));