Skip to content

Commit

Permalink
Enable StackPHP Support
Browse files Browse the repository at this point in the history
Add another optional constructor argument so that the entire middleware can be configured when the original object instance is not available. Add tests to make sure that the extra constructor argument for the response header works as expected, and update the README to reflect the changes to the RequestId API including example usage of how to integrate the library with StackPHP.
  • Loading branch information
Zander Baldwin authored and zanbaldwin committed Aug 20, 2015
1 parent d408d12 commit 4b854ce
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 5 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,34 @@ It is also possible to change response header's name:
```php5
$stack->enableResponseHeader('My-Custom-Request-Id');
```

If you don't have access to the `RequestId` object instance (StackPHP, for example) the response header can be set via
the fourth argument of the `RequestId` constructor method.

```php5
$generator = new UuidRequestIdGenerator(1337);
$stack = new RequestId($kernel, $generator, 'X-Request-Id', 'My-Custom-Request-Id');
```

The third argument, for reference, is the name of the header:
- That will be checked for a value before falling back to generating a new request ID,
- Used to store the resulting request ID inside Symfony's request object.

## StackPHP's Middleware Builder
If you are already using [StackPHP](http://stackphp.com), just push the `RequestId` class into the builder.

```php5
$kernel = new AppKernel('dev', true);

$generator = new UuidRequestIdGenerator(1337);
$stack = (new Stack\Builder)
->push('Qandidate\Stack\RequestId', $generator, 'X-Request-Id', 'X-Request-Id')
->resolve($kernel);

$kernel->loadClassCache();

$request = Request::createFromGlobals();
$response = $stack->handle($request);
$response->send();
$kernel->terminate($request, $response);
```
15 changes: 10 additions & 5 deletions src/Qandidate/Stack/RequestId.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@ class RequestId implements HttpKernelInterface
private $header;
private $responseHeader;

public function __construct(HttpKernelInterface $app, RequestIdGenerator $generator, $header = 'X-Request-Id')
{
$this->app = $app;
$this->generator = $generator;
$this->header = $header;
public function __construct(
HttpKernelInterface $app,
RequestIdGenerator $generator,
$header = 'X-Request-Id',
$responseHeader = null
) {
$this->app = $app;
$this->generator = $generator;
$this->header = $header;
$this->responseHeader = $responseHeader;
}

/**
Expand Down
42 changes: 42 additions & 0 deletions test/Qandidate/Stack/RequestIdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,48 @@ public function it_sets_the_request_id_in_a_custom_response_header_if_given()
$this->assertSame('yolo', $response->headers->get('Request-Id'));
}

/**
* @test
*/
public function it_can_set_the_response_header_from_the_constructor_argument()
{
$this->requestIdGenerator->expects($this->any())
->method('generate')
->will($this->returnValue('yolo'));

$responseHeader = 'Request-Id';

$this->stackedApp->enableResponseHeader($responseHeader);
$normalResponse = $this->stackedApp->handle($this->createRequest());

$alternateStackedApp = new RequestId($this->app, $this->requestIdGenerator, $this->header, $responseHeader);
$alternateResponse = $alternateStackedApp->handle($this->createRequest());

$this->assertSame('yolo', $alternateResponse->headers->get($responseHeader));
$this->assertSame(
$normalResponse->headers->get($responseHeader),
$alternateResponse->headers->get($responseHeader)
);
}

/**
* @test
*/
public function it_can_override_the_response_header_argument_with_the_response_header_method()
{
$this->requestIdGenerator->expects($this->any())
->method('generate')
->will($this->returnValue('yolo'));

$alternateStackedApp = new RequestId($this->app, $this->requestIdGenerator, $this->header, 'Bad-Request-Id');
$alternateStackedApp->enableResponseHeader('Good-Request-Id');

$response = $alternateStackedApp->handle($this->CreateRequest());

$this->assertFalse($response->headers->has('Bad-Request-Id'));
$this->assertTrue($response->headers->has('Good-Request-Id'));
}

/**
* @test
*/
Expand Down

0 comments on commit 4b854ce

Please sign in to comment.