Skip to content

Commit

Permalink
Close #26
Browse files Browse the repository at this point in the history
  • Loading branch information
neomerx committed Jan 30, 2019
1 parent 41ea83c commit a51345f
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Expand Up @@ -4,6 +4,8 @@ php:
- 5.6
- 7.0
- 7.1
- 7.2
- 7.3
matrix:
include:
- php: 5.6
Expand Down
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -22,6 +22,8 @@ composer require neomerx/cors-illuminate

> For Lumen skip this step and see step 2.2
> For Laravel 5.5+ skip this step and see step 3
Add CORS provider by adding the following line to your `config/app.php` file
```php
<?php
Expand Down
2 changes: 1 addition & 1 deletion src/Adapters/IlluminateRequestToPsr7.php
@@ -1,7 +1,7 @@
<?php namespace Neomerx\CorsIlluminate\Adapters;

/**
* Copyright 2015-2017 info@neomerx.com
* Copyright 2015-2019 info@neomerx.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
28 changes: 25 additions & 3 deletions src/CorsMiddleware.php
@@ -1,7 +1,7 @@
<?php namespace Neomerx\CorsIlluminate;

/**
* Copyright 2015-2017 info@neomerx.com
* Copyright 2015-2019 info@neomerx.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,6 +22,7 @@
use \Psr\Http\Message\RequestInterface;
use \Neomerx\Cors\Contracts\AnalyzerInterface;
use \Neomerx\Cors\Contracts\AnalysisResultInterface;
use \Neomerx\Cors\Contracts\Constants\CorsResponseHeaders;
use \Neomerx\CorsIlluminate\Adapters\IlluminateRequestToPsr7;
use \Illuminate\Contracts\Container\Container as ContainerInterface;

Expand Down Expand Up @@ -68,14 +69,15 @@ public function handle(Request $request, Closure $next)
break;

case AnalysisResultInterface::TYPE_PRE_FLIGHT_REQUEST:
$response = new Response(null, Response::HTTP_OK, $cors->getResponseHeaders());
$headers = $this->getPrepareCorsHeaders($cors->getResponseHeaders());
$response = new Response(null, Response::HTTP_OK, $headers);
break;

case AnalysisResultInterface::TYPE_ACTUAL_REQUEST:
/** @var Response $response */
$response = $next($request);
// merge CORS headers to response
foreach ($cors->getResponseHeaders() as $name => $value) {
foreach ($this->getPrepareCorsHeaders($cors->getResponseHeaders()) as $name => $value) {
$response->headers->set($name, $value, false);
}
break;
Expand Down Expand Up @@ -130,4 +132,24 @@ protected function getRequestAdapter(Request $request)
{
return new IlluminateRequestToPsr7($request);
}

/**
* There is an issue with IE which cannot work with multiple 'Access-Control-Expose-Headers' and
* requires it them to be comma separated. Chrome and Firefox seem to be not affected.
*
* @param array $headers
*
* @return array
*
* @see https://github.com/neomerx/cors-psr7/issues/31
*/
protected function getPrepareCorsHeaders($headers)
{
if (array_key_exists(CorsResponseHeaders::EXPOSE_HEADERS, $headers) === true) {
$headers[CorsResponseHeaders::EXPOSE_HEADERS] =
implode(', ', $headers[CorsResponseHeaders::EXPOSE_HEADERS]);
}

return $headers;
}
}
2 changes: 1 addition & 1 deletion src/Providers/LaravelServiceProvider.php
@@ -1,7 +1,7 @@
<?php namespace Neomerx\CorsIlluminate\Providers;

/**
* Copyright 2015-2017 info@neomerx.com
* Copyright 2015-2019 info@neomerx.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/Providers/LumenServiceProvider.php
@@ -1,7 +1,7 @@
<?php namespace Neomerx\CorsIlluminate\Providers;

/**
* Copyright 2015-2017 info@neomerx.com
* Copyright 2015-2019 info@neomerx.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/Settings/Settings.php
@@ -1,7 +1,7 @@
<?php namespace Neomerx\CorsIlluminate\Settings;

/**
* Copyright 2015-2017 info@neomerx.com
* Copyright 2015-2019 info@neomerx.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion tests/Adapters/IlluminateRequestToPsr7Test.php
@@ -1,7 +1,7 @@
<?php namespace Neomerx\Tests\CorsIlluminate\Adapters;

/**
* Copyright 2015-2017 info@neomerx.com
* Copyright 2015-2019 info@neomerx.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion tests/BaseTestCase.php
@@ -1,7 +1,7 @@
<?php namespace Neomerx\Tests\CorsIlluminate;

/**
* Copyright 2015-2017 info@neomerx.com
* Copyright 2015-2019 info@neomerx.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
12 changes: 10 additions & 2 deletions tests/CorsMiddlewareTest.php
@@ -1,7 +1,7 @@
<?php namespace Neomerx\Tests\CorsIlluminate;

/**
* Copyright 2015-2017 info@neomerx.com
* Copyright 2015-2019 info@neomerx.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,7 @@
use \Neomerx\CorsIlluminate\CorsMiddleware;
use \Neomerx\Cors\Contracts\AnalyzerInterface;
use \Neomerx\Cors\Contracts\AnalysisResultInterface;
use \Neomerx\Cors\Contracts\Constants\CorsResponseHeaders;
use \Illuminate\Contracts\Container\Container as ContainerInterface;

/**
Expand Down Expand Up @@ -115,12 +116,19 @@ public function testActualCors()
$this->mockAnalyzerCall('analyze', $this->analysisResult);
$this->mockContainerCall('instance', [AnalysisResultInterface::class, $this->analysisResult]);
$this->mockAnalysisCall('getRequestType', AnalysisResultInterface::TYPE_ACTUAL_REQUEST);
$this->mockAnalysisCall('getResponseHeaders', [$headerName => 'value 2']);
$this->mockAnalysisCall(
'getResponseHeaders',
[
$headerName => 'value 2',
CorsResponseHeaders::EXPOSE_HEADERS => ['expose1', 'expose2'],
]
);

/** @var Response $response */
$this->assertNotNull($response = $this->middleware->handle($this->request, $next));
$this->assertTrue($nextCalled);
$this->assertEquals(['value 1', 'value 2'], $response->headers->get($headerName, null, false));
$this->assertEquals('expose1, expose2', $response->headers->get(CorsResponseHeaders::EXPOSE_HEADERS));
}

/**
Expand Down
9 changes: 8 additions & 1 deletion tests/Providers/LaravelServiceProviderTest.php
@@ -1,7 +1,7 @@
<?php namespace Neomerx\Tests\CorsIlluminate\Providers;

/**
* Copyright 2015-2017 info@neomerx.com
* Copyright 2015-2019 info@neomerx.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,6 +20,7 @@
use \Mockery;
use \ArrayAccess;
use \ReflectionClass;
use ReflectionException;
use \ReflectionMethod;
use \Mockery\MockInterface;
use \Neomerx\Cors\Contracts\AnalyzerInterface;
Expand Down Expand Up @@ -101,6 +102,8 @@ public function testBoot()

/**
* Test create analysis strategy.
*
* @throws ReflectionException
*/
public function testGetCreateAnalysisStrategyClosure()
{
Expand Down Expand Up @@ -128,6 +131,8 @@ public function testGetCreateAnalysisStrategyClosure()

/**
* Test create analyzer.
*
* @throws ReflectionException
*/
public function testGetCreateAnalyzerClosure()
{
Expand Down Expand Up @@ -158,6 +163,8 @@ public function testGetCreateAnalyzerClosure()
* @param string $name
*
* @return ReflectionMethod
*
* @throws ReflectionException
*/
protected static function getMethod($name)
{
Expand Down
9 changes: 8 additions & 1 deletion tests/Providers/LumenServiceProviderTest.php
@@ -1,7 +1,7 @@
<?php namespace Neomerx\Tests\CorsIlluminate\Providers;

/**
* Copyright 2015-2017 info@neomerx.com
* Copyright 2015-2019 info@neomerx.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@
use \Mockery;
use \ArrayAccess;
use \ReflectionClass;
use ReflectionException;
use \ReflectionMethod;
use \Mockery\MockInterface;
use \Neomerx\Tests\CorsIlluminate\BaseTestCase;
Expand Down Expand Up @@ -53,6 +54,8 @@ protected function setUp()

/**
* Test configureCorsAnalyzer method.
*
* @throws ReflectionException
*/
public function testConfigureCorsAnalyzer()
{
Expand All @@ -70,6 +73,8 @@ public function testConfigureCorsAnalyzer()

/**
* Test registerPublishConfig method.
*
* @throws ReflectionException
*/
public function testRegisterPublishConfig()
{
Expand All @@ -81,6 +86,8 @@ public function testRegisterPublishConfig()
* @param string $name
*
* @return ReflectionMethod
*
* @throws ReflectionException
*/
protected static function getMethod($name)
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Settings/SettingsTest.php
@@ -1,7 +1,7 @@
<?php namespace Neomerx\Tests\CorsIlluminate\Settings;

/**
* Copyright 2015-2017 info@neomerx.com
* Copyright 2015-2019 info@neomerx.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down

0 comments on commit a51345f

Please sign in to comment.