|
25 | 25 | use Hawkbit\Tests\TestAsset\SharedTestController; |
26 | 26 | use Hawkbit\Tests\TestAsset\TestController; |
27 | 27 | use Zend\Diactoros\Response\EmitterInterface; |
| 28 | +use Zend\Diactoros\Response\JsonResponse; |
28 | 29 | use Zend\Diactoros\Response\SapiStreamEmitter; |
29 | 30 | use Zend\Diactoros\ServerRequestFactory; |
30 | 31 |
|
@@ -385,6 +386,7 @@ public function testContentTypeDelegation() |
385 | 386 |
|
386 | 387 | $_SERVER['CONTENT_TYPE'] = 'application/json'; |
387 | 388 | $request = ServerRequestFactory::fromGlobals(); |
| 389 | + unset($_SERVER['CONTENT_TYPE']); |
388 | 390 |
|
389 | 391 | $response = $app->handle($request); |
390 | 392 |
|
@@ -451,5 +453,53 @@ public function testErrorHandler() |
451 | 453 | $this->assertInstanceOf('\League\Route\Http\Exception\NotFoundException', $app->getLastException()); |
452 | 454 | } |
453 | 455 |
|
| 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 | + |
454 | 504 |
|
455 | 505 | } |
0 commit comments