Skip to content

Commit 4bb9ab9

Browse files
committed
Sources
1 parent 529da6c commit 4bb9ab9

6 files changed

+151
-0
lines changed

.gitignore

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

.travis.yml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
language: php
2+
3+
matrix:
4+
fast_finish: true
5+
include:
6+
- php: 5.5
7+
- php: 5.6
8+
- php: 7
9+
- php: hhvm
10+
allow_failures:
11+
- php: 7
12+
- php: hhvm
13+
14+
install:
15+
- travis_retry composer install --no-interaction --ignore-platform-reqs --prefer-source
16+
- composer info -i
17+
18+
script:
19+
- ./vendor/bin/phpunit

composer.json

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "php-middleware/block-robots",
3+
"description": "Middleware to avoid search engine indexing with PSR-7",
4+
"type": "library",
5+
"keywords": [
6+
"middleware",
7+
"psr",
8+
"psr-7",
9+
"seo",
10+
"robots"
11+
],
12+
"require": {
13+
"php": ">=5.5",
14+
"psr/http-message": "^1.0"
15+
},
16+
"require-dev": {
17+
"phpunit/phpunit": "^4.8.6",
18+
"zendframework/zend-diactoros": "^1.1.3"
19+
},
20+
"autoload": {
21+
"psr-4": {
22+
"PhpMiddleware\\BlockRobots\\": "src/"
23+
}
24+
},
25+
"autoload-dev": {
26+
"psr-4": {
27+
"PhpMiddlewareTest\\BlockRobots\\": "test/"
28+
}
29+
}
30+
}

phpunit.xml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit bootstrap="./vendor/autoload.php" colors="true">
4+
<testsuites>
5+
<testsuite name="PhpMiddleware\\BlockRobots Tests">
6+
<directory>./test</directory>
7+
</testsuite>
8+
</testsuites>
9+
10+
<filter>
11+
<whitelist processUncoveredFilesFromWhitelist="true">
12+
<directory suffix=".php">./src</directory>
13+
</whitelist>
14+
</filter>
15+
</phpunit>

src/BlockRobotsMiddleware.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace PhpMiddleware\BlockRobots;
4+
5+
use Psr\Http\Message\ResponseInterface;
6+
use Psr\Http\Message\ServerRequestInterface;
7+
8+
class BlockRobotsMiddleware
9+
{
10+
const ROBOTS_HEADER = 'X-Robots-Tag';
11+
12+
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $out = null)
13+
{
14+
if ($request->getUri()->getPath() === '/robots.txt') {
15+
$response->getBody()->write('Disallow: /');
16+
return $response->withHeader('Content-Type', 'text/plain');
17+
}
18+
/* @var $out ResponseInterface */
19+
$response = $out === null ? $response : $out($request, $response);
20+
21+
return $response->withHeader(self::ROBOTS_HEADER, 'noindex, nofollow');
22+
}
23+
}

test/BlockRobotsMiddlewareTest.php

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace PhpMiddlewareTest\BlockRobots;
4+
5+
use PhpMiddleware\BlockRobots\BlockRobotsMiddleware;
6+
use Psr\Http\Message\ResponseInterface;
7+
use Zend\Diactoros\Response;
8+
use Zend\Diactoros\ServerRequest;
9+
10+
class BlockRobotsMiddlewareTest extends \PHPUnit_Framework_TestCase
11+
{
12+
protected $middleware;
13+
14+
protected function setUp()
15+
{
16+
$this->middleware = new BlockRobotsMiddleware();
17+
}
18+
19+
public function testNoIndexHeader()
20+
{
21+
$request = new ServerRequest();
22+
$response = new Response();
23+
$calledOut = false;
24+
25+
$outFunction = function ($request, $response) use (&$calledOut) {
26+
$calledOut = true;
27+
28+
return $response;
29+
};
30+
31+
/* @var $result ResponseInterface */
32+
$result = call_user_func($this->middleware, $request, $response, $outFunction);
33+
34+
$this->assertTrue($calledOut, 'Out was not called');
35+
$this->assertNotSame($response, $result);
36+
$this->assertEquals('noindex, nofollow', $result->getHeaderLine(BlockRobotsMiddleware::ROBOTS_HEADER));
37+
}
38+
39+
public function testRobots()
40+
{
41+
$uri = new \Zend\Diactoros\Uri('http://foo/robots.txt');
42+
$request = new ServerRequest();
43+
$request = $request->withUri($uri);
44+
$response = new Response();
45+
$calledOut = false;
46+
47+
$outFunction = function ($request, $response) use (&$calledOut) {
48+
$calledOut = true;
49+
50+
return $response;
51+
};
52+
53+
/* @var $result ResponseInterface */
54+
$result = call_user_func($this->middleware, $request, $response, $outFunction);
55+
56+
$this->assertFalse($calledOut, 'Out was called');
57+
$this->assertNotSame($response, $result);
58+
$this->assertFalse($result->hasHeader(BlockRobotsMiddleware::ROBOTS_HEADER));
59+
$this->assertEquals('Disallow: /', (string) $result->getBody());
60+
$this->assertEquals('text/plain', $result->getHeaderLine('Content-Type'));
61+
}
62+
}

0 commit comments

Comments
 (0)