Skip to content

Commit

Permalink
Adding Token::getHeader(), so it will be easier to retrive a header v…
Browse files Browse the repository at this point in the history
…alue.
  • Loading branch information
lcobucci committed Apr 1, 2015
1 parent 1429700 commit 87894b7
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,30 @@ public function getHeaders()
return $this->headers;
}

/**
* Returns the value of a token header
*
* @param string $name
*
* @return mixed
*
* @throws OutOfBoundsException
*/
public function getHeader($name)
{
if (!isset($this->headers[$name])) {
throw new OutOfBoundsException('Requested header is not configured');
}

$header = $this->headers[$name];

if ($header instanceof Claim) {
return $header->getValue();
}

return $header;
}

/**
* Returns the token claim set
*
Expand Down
46 changes: 46 additions & 0 deletions test/TokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,52 @@ public function setEncoderMustConfigureTheEncoderAttribute()
$this->assertAttributeSame($this->encoder, 'encoder', $token);
}

/**
* @test
*
* @uses Lcobucci\JWT\Token::__construct
*
* @covers Lcobucci\JWT\Token::getHeader
*
* @expectedException \OutOfBoundsException
*/
public function getHeaderMustRaiseExceptionWhenHeaderIsNotConfigured()
{
$token = new Token(['test' => 'testing']);

$token->getHeader('testing');
}

/**
* @test
*
* @uses Lcobucci\JWT\Token::__construct
*
* @covers Lcobucci\JWT\Token::getHeader
*/
public function getHeaderMustReturnTheRequestedHeader()
{
$token = new Token(['test' => 'testing']);

$this->assertEquals('testing', $token->getHeader('test'));
}

/**
* @test
*
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\Claim\Basic::__construct
* @uses Lcobucci\JWT\Claim\Basic::getValue
*
* @covers Lcobucci\JWT\Token::getHeader
*/
public function getHeaderMustReturnValueWhenItIsAReplicatedClaim()
{
$token = new Token(['jti' => new EqualsTo('jti', 1)]);

$this->assertEquals(1, $token->getHeader('jti'));
}

/**
* @test
*
Expand Down

0 comments on commit 87894b7

Please sign in to comment.