ContextTest.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace HttpSignatures\tests;
  3. use GuzzleHttp\Psr7\Request;
  4. use HttpSignatures\Context;
  5. class ContextTest extends \PHPUnit_Framework_TestCase
  6. {
  7. private $context;
  8. public function setUp()
  9. {
  10. $this->context = new Context([
  11. 'keys' => ['pda' => 'secret'],
  12. 'algorithm' => 'hmac-sha256',
  13. 'headers' => ['(request-target)', 'date'],
  14. ]);
  15. }
  16. public function testSigner()
  17. {
  18. $message = new Request('GET', '/path?query=123', ['date' => 'today', 'accept' => 'llamas']);
  19. $message = $this->context->signer()->sign($message);
  20. $expectedString = implode(',', [
  21. 'keyId="pda"',
  22. 'algorithm="hmac-sha256"',
  23. 'headers="(request-target) date"',
  24. 'signature="SFlytCGpsqb/9qYaKCQklGDvwgmrwfIERFnwt+yqPJw="',
  25. ]);
  26. $this->assertEquals(
  27. $expectedString,
  28. $message->getHeader('Signature')[0]
  29. );
  30. $this->assertEquals(
  31. 'Signature ' . $expectedString,
  32. $message->getHeader('Authorization')[0]
  33. );
  34. }
  35. public function testVerifier()
  36. {
  37. $message = $this->context->signer()->sign(new Request('GET', '/path?query=123', [
  38. 'Signature' => 'keyId="pda",algorithm="hmac-sha1",headers="date",signature="x"',
  39. 'Date' => 'x',
  40. ]));
  41. // assert it works without errors; correctness of results tested elsewhere.
  42. $this->assertTrue(is_bool($this->context->verifier()->isValid($message)));
  43. }
  44. }