Skip to content

Commit 81f34e3

Browse files
author
Marco Bunge
committed
Fix #25
1 parent 8c5a921 commit 81f34e3

3 files changed

Lines changed: 55 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Hawkbit Changelog
22

3+
## 2.1.3
4+
5+
- Fix #25 Ajax request is always forcing JSON request and response
6+
37
## 2.1.2
48

59
- Fix #24 incorrect and buggy error handling

src/Application.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public function getRequest()
188188
public function getResponse($content = '')
189189
{
190190
//transform content by content type
191-
if ($this->isAjaxRequest() || $this->isJsonRequest()) {
191+
if ($this->isJsonRequest()) {
192192
if ($content instanceof Response\JsonResponse) {
193193
$content = json_decode($content->getBody());
194194
} elseif (!is_array($content)) {

tests/ApplicationTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Hawkbit\Tests\TestAsset\SharedTestController;
2626
use Hawkbit\Tests\TestAsset\TestController;
2727
use Zend\Diactoros\Response\EmitterInterface;
28+
use Zend\Diactoros\Response\JsonResponse;
2829
use Zend\Diactoros\Response\SapiStreamEmitter;
2930
use Zend\Diactoros\ServerRequestFactory;
3031

@@ -385,6 +386,7 @@ public function testContentTypeDelegation()
385386

386387
$_SERVER['CONTENT_TYPE'] = 'application/json';
387388
$request = ServerRequestFactory::fromGlobals();
389+
unset($_SERVER['CONTENT_TYPE']);
388390

389391
$response = $app->handle($request);
390392

@@ -451,5 +453,53 @@ public function testErrorHandler()
451453
$this->assertInstanceOf('\League\Route\Http\Exception\NotFoundException', $app->getLastException());
452454
}
453455

456+
public function testAjaxRequestRecogniseHTMLAutomatically()
457+
{
458+
459+
$app = new Application();
460+
$app->get('/', function (ServerRequestInterface $request, ResponseInterface $response) {
461+
$response->getBody()->write('<h1>HELLO</h1>');
462+
return $response;
463+
});
464+
465+
// emulate http request from basic app
466+
$_SERVER['CONTENT_TYPE'] = 'text/html';
467+
468+
// emulate xhr request
469+
$_SERVER['X_REQUESTED_WITH'] = 'xmlhttprequest';
470+
471+
$serverRequest = ServerRequestFactory::fromGlobals();
472+
473+
unset($_SERVER['CONTENT_TYPE']);
474+
unset($_SERVER['X_REQUESTED_WITH']);
475+
476+
$response = $app->handle($serverRequest);
477+
478+
$this->assertEquals('<h1>HELLO</h1>', $response->getBody()->__toString());
479+
$this->assertEquals('text/html', $app->getContentType());
480+
}
481+
482+
public function testAjaxRequestRecogniseHTML()
483+
{
484+
485+
$app = new Application();
486+
$app->get('/', function (ServerRequestInterface $request, ResponseInterface $response) {
487+
return new JsonResponse(['title' => 'HELLO']);
488+
});
489+
490+
$serverRequest = ServerRequestFactory::fromGlobals();
491+
492+
// set xhr header
493+
$serverRequest = $serverRequest
494+
->withHeader('x-requested-with', 'xmlhttprequest')
495+
// force content type to be json
496+
->withHeader('content-type', 'application/json');
497+
498+
$response = $app->handle($serverRequest);
499+
500+
$this->assertEquals('{"title":"HELLO"}', $response->getBody()->__toString());
501+
$this->assertEquals('application/json', $app->getContentType());
502+
}
503+
454504

455505
}

0 commit comments

Comments
 (0)