Skip to content

Commit

Permalink
Merge pull request #31 from danizord/hotfix/add-handled-by-header
Browse files Browse the repository at this point in the history
Add X-HANDLED-BY header
  • Loading branch information
bakura10 authored Sep 1, 2016
2 parents fbe0fcf + 5efc565 commit 84af8ad
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 21 deletions.
5 changes: 4 additions & 1 deletion src/Middleware/WorkerMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ public function __invoke(
->withAttribute(self::MESSAGE_PAYLOAD_ATTRIBUTE, $payload)
->withAttribute(self::MESSAGE_NAME_ATTRIBUTE, $name);

return $pipeline($request, $response);
/** @var ResponseInterface $response */
$response = $pipeline($request, $response);

return $response->withHeader('X-HANDLED-BY', 'ZfrEbWorker');
}

/**
Expand Down
25 changes: 5 additions & 20 deletions test/Middleware/WorkerMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,37 +93,22 @@ public function testDispatchesMappedMiddlewares($mappedMiddlewares, int $expecte
$container->get($mappedMiddleware)->shouldBeCalled()->willReturn([$this, 'incrementMiddleware']);
}

$outWasCalled = false;
$responseFromOut = new Response();

$out = function ($request, ResponseInterface $response) use (&$outWasCalled, $expectedCounter, $responseFromOut) {
$outWasCalled = true;

$out = function ($request, ResponseInterface $response) use ($expectedCounter) {
$this->assertEquals('default-queue', $request->getAttribute(WorkerMiddleware::MATCHED_QUEUE_ATTRIBUTE));
$this->assertEquals('123abc', $request->getAttribute(WorkerMiddleware::MESSAGE_ID_ATTRIBUTE));
$this->assertEquals('message-name', $request->getAttribute(WorkerMiddleware::MESSAGE_NAME_ATTRIBUTE));
$this->assertEquals(['id' => 123], $request->getAttribute(WorkerMiddleware::MESSAGE_PAYLOAD_ATTRIBUTE));
$this->assertEquals($expectedCounter, $request->getAttribute('counter', 0));
$this->assertEquals($expectedCounter, $response->hasHeader('counter') ? $response->getHeaderLine('counter') : 0);

return $responseFromOut;
return $response->withAddedHeader('foo', 'bar');
};

/** @var ResponseInterface $returnedResponse */
$returnedResponse = $middleware($request, $response, $out);

$this->assertTrue($outWasCalled, 'Make sure that $out middleware was called');
$this->assertSame($responseFromOut, $returnedResponse, 'Make sure that it returns response from $out');
}

public function testReturnsResponseIfNoOutMiddlewareIsProvided()
{
$middleware = new WorkerMiddleware(['message-name' => []], $this->prophesize(ContainerInterface::class)->reveal());
$request = $this->createRequest();
$response = new Response();

$returnedResponse = $middleware($request, $response);

$this->assertSame($response, $returnedResponse);
$this->assertEquals('bar', $returnedResponse->getHeaderLine('foo'), 'Make sure that $out was called');
$this->assertEquals('ZfrEbWorker', $returnedResponse->getHeaderLine('X-HANDLED-BY'), 'Make sure that it adds the X-HANDLED-BY header');
}

public function incrementMiddleware(ServerRequestInterface $request, ResponseInterface $response, callable $next): ResponseInterface
Expand Down

0 comments on commit 84af8ad

Please sign in to comment.