Skip to content

Commit 94147f8

Browse files
committed
Fix for issue gnikyt#164
1 parent 3dd8574 commit 94147f8

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

src/ShopifyApp/Middleware/AuthShop.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,18 @@ public function handle(Request $request, Closure $next)
4747

4848
// Shop is OK, move on...
4949
$response = $next($request);
50-
if (!$response instanceof Response && !$response instanceof RedirectResponse) {
51-
// We need a response object to modify headers
52-
$response = new Response($response);
53-
}
50+
if (!$request->ajax()) {
51+
// Request is not AJAX, continue as normal
52+
if (!$response instanceof Response && !$response instanceof RedirectResponse) {
53+
// We need a response object to modify headers
54+
$response = new Response($response);
55+
}
5456

55-
if (Config::get('shopify-app.esdk_enabled')) {
56-
// Headers applicable to ESDK only
57-
$response->headers->set('P3P', 'CP="Not used"');
58-
$response->headers->remove('X-Frame-Options');
57+
if (Config::get('shopify-app.esdk_enabled')) {
58+
// Headers applicable to ESDK only
59+
$response->headers->set('P3P', 'CP="Not used"');
60+
$response->headers->remove('X-Frame-Options');
61+
}
5962
}
6063

6164
return $response;

tests/Middleware/AuthShopMiddlewareTest.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,23 @@ public function testHeadersForEsdkShouldBeAdjusted()
9797
$this->assertNull($result[0]->headers->get('x-frame-options'));
9898
}
9999

100+
public function testAjaxCallShouldNotAdjustResponse()
101+
{
102+
// Set a shop
103+
$shop = factory(Shop::class)->create();
104+
Session::put('shopify_domain', $shop->shopify_domain);
105+
106+
// Set the request
107+
$request = Request::instance();
108+
$request->headers->set('x-requested-with', 'XMLHttpRequest');
109+
110+
// Run the middleware
111+
$result = $this->runAuthShop(null, $request);
112+
113+
// Assert the headers were not modified
114+
$this->assertNull($result[0]);
115+
}
116+
100117
public function testHeadersForDisabledEsdk()
101118
{
102119
// Set a shop
@@ -138,10 +155,10 @@ public function testShouldSaveReturnUrl()
138155
// Request::swap($currentRequest);
139156
}
140157

141-
private function runAuthShop(Closure $cb = null)
158+
private function runAuthShop(Closure $cb = null, $requestInstance = null)
142159
{
143160
$called = false;
144-
$response = (new AuthShop())->handle(Request::instance(), function ($request) use (&$called, $cb) {
161+
$response = (new AuthShop())->handle($requestInstance ? $requestInstance : Request::instance(), function ($request) use (&$called, $cb) {
145162
$called = true;
146163

147164
if ($cb) {

0 commit comments

Comments
 (0)