Skip to content

Commit 8743959

Browse files
author
Andrea De Pirro
committed
Laravel request accept json middleware
0 parents  commit 8743959

14 files changed

+302
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor/
2+
composer.lock
3+
.php_cs.cache

.php_cs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
$finder = PhpCsFixer\Finder::create()
3+
->in(__DIR__.'/src')
4+
->in(__DIR__.'/tests')
5+
;
6+
return PhpCsFixer\Config::create()
7+
->setRules([
8+
'@PSR2' => true,
9+
'array_syntax' => ['syntax' => 'short'],
10+
'concat_space' => ['spacing' => 'one'],
11+
'new_with_braces' => true,
12+
'no_blank_lines_after_phpdoc' => true,
13+
'no_empty_phpdoc' => true,
14+
'no_empty_comment' => true,
15+
'no_leading_import_slash' => true,
16+
'no_trailing_comma_in_singleline_array' => true,
17+
'no_unused_imports' => true,
18+
'ordered_imports' => ['importsOrder' => null, 'sortAlgorithm' => 'alpha'],
19+
'phpdoc_add_missing_param_annotation' => ['only_untyped' => true],
20+
'phpdoc_align' => true,
21+
'phpdoc_no_empty_return' => true,
22+
'phpdoc_order' => true,
23+
'phpdoc_scalar' => true,
24+
'phpdoc_to_comment' => true,
25+
'psr0' => false,
26+
'psr4' => true,
27+
'return_type_declaration' => ['space_before' => 'none'],
28+
'single_blank_line_before_namespace' => true,
29+
'single_quote' => true,
30+
'space_after_semicolon' => true,
31+
'ternary_operator_spaces' => true,
32+
'trailing_comma_in_multiline_array' => true,
33+
'trim_array_spaces' => true,
34+
'whitespace_after_comma_in_array' => true,
35+
])
36+
->setFinder($finder)
37+
;

.phpunit.result.cache

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
C:37:"PHPUnit\Runner\DefaultTestResultCache":333:{a:2:{s:7:"defects";a:1:{s:84:"Softonic\RequestContentDecompress\Tests\Middleware\RequestAcceptJsonTest::testHandle";i:4;}s:5:"times";a:2:{s:84:"Softonic\RequestContentDecompress\Tests\Middleware\RequestAcceptJsonTest::testHandle";d:0.196;s:77:"Softonic\RequestAcceptJson\Tests\Middleware\RequestAcceptJsonTest::testHandle";d:0.104;}}}

.scrutinizer.yml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
filter:
2+
paths: [src/*]
3+
excluded_paths: [tests/*]
4+
checks:
5+
php:
6+
code_rating: true
7+
tools:
8+
external_code_coverage:
9+
timeout: 600
10+
runs: 2
11+
php_code_coverage: false
12+
php_loc:
13+
enabled: true
14+
excluded_dirs: [tests, vendor]
15+
php_cpd:
16+
enabled: true
17+
excluded_dirs: [tests, vendor]

.travis.yml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
language: php
2+
3+
sudo: false
4+
5+
matrix:
6+
include:
7+
- php: 7.1
8+
env: COLLECT_COVERAGE=true VALIDATE_CODING_STYLE=true
9+
- php: 7.2
10+
env: COLLECT_COVERAGE=true VALIDATE_CODING_STYLE=true
11+
- php: 7.3
12+
env: COLLECT_COVERAGE=true VALIDATE_CODING_STYLE=true
13+
- php: master
14+
env: COLLECT_COVERAGE=true VALIDATE_CODING_STYLE=false
15+
allow_failures:
16+
- php: master
17+
fast_finish: true
18+
19+
cache:
20+
directories:
21+
- $HOME/.composer/cache
22+
23+
before_install:
24+
- travis_retry composer self-update
25+
26+
install:
27+
- travis_retry composer update --no-interaction --prefer-source
28+
29+
script:
30+
- composer phpunit
31+
32+
after_script:
33+
- if [ "$COLLECT_COVERAGE" == "true" ]; then wget https://scrutinizer-ci.com/ocular.phar && php ocular.phar code-coverage:upload --format=php-clover build/clover.xml; fi
34+
- if [ "$VALIDATE_CODING_STYLE" == "true" ]; then composer phpcs; fi

CODEOWNERS

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @akira28 @joskfg

LICENSE

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright 2019 Softonic International S.A.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.

README.md

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
Laravel request accept json middleware
2+
=====
3+
4+
[![Latest Version](https://img.shields.io/github/release/softonic/laravel-request-accept-json-middleware.svg?style=flat-square)](https://github.com/softonic/laravel-request-accept-json-middleware/releases)
5+
[![Software License](https://img.shields.io/badge/license-Apache%202.0-blue.svg?style=flat-square)](LICENSE.md)
6+
[![Build Status](https://img.shields.io/travis/softonic/laravel-request-accept-json-middleware/master.svg?style=flat-square)](https://travis-ci.org/softonic/laravel-request-accept-json-middleware)
7+
[![Coverage Status](https://img.shields.io/scrutinizer/coverage/g/softonic/laravel-request-accept-json-middleware.svg?style=flat-square)](https://scrutinizer-ci.com/g/softonic/laravel-request-accept-json-middleware/code-structure)
8+
[![Quality Score](https://img.shields.io/scrutinizer/g/softonic/laravel-request-accept-json-middleware.svg?style=flat-square)](https://scrutinizer-ci.com/g/softonic/laravel-request-accept-json-middleware)
9+
[![Total Downloads](https://img.shields.io/packagist/dt/softonic/laravel-request-accept-json-middleware.svg?style=flat-square)](https://packagist.org/packages/softonic/laravel-request-accept-json-middleware)
10+
11+
This middleware adds the ability to automatically add the Accept application/json header to every request
12+
13+
Installation
14+
-------
15+
16+
Via composer:
17+
```
18+
composer require softonic/laravel-request-accept-json-middleware
19+
```
20+
21+
Documentation
22+
-------
23+
24+
To use the middleware register it in `app/Http/Kernel.php`
25+
26+
```
27+
protected $middleware
28+
= [
29+
...
30+
RequestAcceptJson::class,
31+
...
32+
];
33+
```
34+
35+
From now on the header `Accept: application/json` will be automatically added to every request.
36+
37+
Testing
38+
-------
39+
40+
`softonic/laravel-request-accept-json-middleware` has a [PHPUnit](https://phpunit.de) test suite and a coding style compliance test suite using [PHP CS Fixer](http://cs.sensiolabs.org/).
41+
42+
To run the tests, run the following command from the project folder.
43+
44+
``` bash
45+
$ docker-compose run tests
46+
```
47+
48+
To run interactively using [PsySH](http://psysh.org/):
49+
``` bash
50+
$ docker-compose run psysh
51+
```
52+
53+
License
54+
-------
55+
56+
The Apache 2.0 license. Please see [LICENSE](LICENSE) for more information.
57+
58+
[PSR-2]: http://www.php-fig.org/psr/psr-2/
59+
[PSR-4]: http://www.php-fig.org/psr/psr-4/

build/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

composer.json

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "softonic/laravel-request-accept-json-middleware",
3+
"type": "library",
4+
"description": "Laravel middleware to add accept application/json to requests",
5+
"keywords": ["json", "request", "middleware", "laravel"],
6+
"license": "Apache-2.0",
7+
"homepage": "https://github.com/softonic/laravel-request-accept-json-middleware",
8+
"support": {
9+
"issues": "https://github.com/softonic/laravel-request-accept-json-middleware/issues"
10+
},
11+
"require": {
12+
"php": ">=7.2",
13+
"illuminate/http": "^5.8",
14+
"illuminate/contracts": "^5.8"
15+
},
16+
"require-dev": {
17+
"mockery/mockery": "^1.2",
18+
"phpunit/phpunit": "^8.0",
19+
"friendsofphp/php-cs-fixer": "^2.4"
20+
},
21+
"autoload": {
22+
"psr-4": {
23+
"Softonic\\RequestAcceptJson\\": "src/"
24+
}
25+
},
26+
"autoload-dev": {
27+
"psr-4": {
28+
"Softonic\\RequestAcceptJson\\Tests\\": "tests/"
29+
}
30+
},
31+
"scripts": {
32+
"tests": "phpunit --coverage-text; php-cs-fixer fix -v --diff --dry-run --allow-risky=yes;",
33+
"phpunit": "phpunit --coverage-text",
34+
"phpcs": "php-cs-fixer fix -v --diff --dry-run --allow-risky=yes;",
35+
"fix-cs": "php-cs-fixer fix -v --diff --allow-risky=yes;"
36+
}
37+
}
38+

docker-compose.yml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
version: '3.6'
2+
3+
services:
4+
tests:
5+
volumes:
6+
- ./:/app
7+
image: ricc/composer-prestissimo:latest
8+
command: composer run tests
9+
10+
fixcs:
11+
volumes:
12+
- ./:/app
13+
image: ricc/composer-prestissimo:latest
14+
command: composer run fix-cs
15+
16+
psysh:
17+
volumes:
18+
- ./:/app
19+
image: ricc/psysh:latest

phpunit.xml

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit bootstrap="vendor/autoload.php"
4+
backupGlobals="false"
5+
backupStaticAttributes="false"
6+
colors="true"
7+
verbose="true"
8+
convertErrorsToExceptions="true"
9+
convertNoticesToExceptions="true"
10+
convertWarningsToExceptions="true"
11+
processIsolation="false"
12+
stopOnFailure="false">
13+
14+
<testsuites>
15+
<testsuite name="All">
16+
<directory>tests</directory>
17+
</testsuite>
18+
</testsuites>
19+
20+
<filter>
21+
<whitelist>
22+
<directory suffix=".php">src</directory>
23+
</whitelist>
24+
</filter>
25+
26+
<logging>
27+
<log type="junit" target="build/report.junit.xml" />
28+
<log type="coverage-html" target="build/coverage" />
29+
<log type="coverage-text" target="build/coverage.txt" />
30+
<log type="coverage-clover" target="build/clover.xml" />
31+
</logging>
32+
33+
</phpunit>

src/Middleware/RequestAcceptJson.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Softonic\RequestAcceptJson\Middleware;
4+
5+
use Closure;
6+
use Illuminate\Http\Request;
7+
8+
class RequestAcceptJson
9+
{
10+
public function handle(Request $request, Closure $next)
11+
{
12+
$request->headers->set('Accept', 'application/json');
13+
14+
return $next($request);
15+
}
16+
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Softonic\RequestAcceptJson\Tests\Middleware;
4+
5+
use Illuminate\Http\Request;
6+
use PHPUnit\Framework\TestCase;
7+
use Softonic\RequestAcceptJson\Middleware\RequestAcceptJson;
8+
9+
class RequestAcceptJsonTest extends TestCase
10+
{
11+
public function testHandle()
12+
{
13+
$mockRequest = $this->getRequest();
14+
$middleware = new RequestAcceptJson();
15+
16+
$result = $middleware->handle(
17+
$mockRequest,
18+
function ($request) {
19+
return $request;
20+
}
21+
);
22+
$this->assertSame('application/json', $result->header('Accept'));
23+
}
24+
25+
private function getRequest()
26+
{
27+
return new Request([], [], [], [], [], [], '');
28+
}
29+
}

0 commit comments

Comments
 (0)