PageRenderTime 28ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/test/Buzz/Test/Util/CookieTest.php

http://github.com/kriswallsmith/Buzz
PHP | 151 lines | 113 code | 38 blank | 0 comment | 0 complexity | 52f9472651d7cd7933fe5cf91daff218 MD5 | raw file
  1. <?php
  2. namespace Buzz\Test\Cookie;
  3. use Buzz\Util\Cookie;
  4. use Buzz\Message;
  5. class CookieTest extends \PHPUnit_Framework_TestCase
  6. {
  7. public function testFromSetCookieHeaderSetsCookieAttributes()
  8. {
  9. $cookie = new Cookie();
  10. $cookie->fromSetCookieHeader('SESSION=asdf; expires='.date('r', strtotime('2000-01-01 00:00:00')).'; path=/; domain=.example.com; secure', 'www.example.com');
  11. $this->assertEquals('SESSION', $cookie->getName());
  12. $this->assertEquals('asdf', $cookie->getValue());
  13. $this->assertEquals(array(
  14. 'expires' => date('r', strtotime('2000-01-01 00:00:00')),
  15. 'path' => '/',
  16. 'domain' => '.example.com',
  17. 'secure' => null,
  18. ), $cookie->getAttributes());
  19. }
  20. public function testFromSetCookieHeaderFallsBackToIssuingDomain()
  21. {
  22. $cookie = new Cookie();
  23. $cookie->fromSetCookieHeader('SESSION=asdf', 'example.com');
  24. $this->assertEquals('example.com', $cookie->getAttribute(Cookie::ATTR_DOMAIN));
  25. }
  26. public function testToCookieHeaderFormatsACookieHeader()
  27. {
  28. $cookie = new Cookie();
  29. $cookie->setName('SESSION');
  30. $cookie->setValue('asdf');
  31. $this->assertEquals('Cookie: SESSION=asdf', $cookie->toCookieHeader());
  32. }
  33. public function testMatchesDomainMatchesSimpleDomains()
  34. {
  35. $cookie = new Cookie();
  36. $cookie->setAttribute('domain', 'nytimes.com');
  37. $this->assertTrue($cookie->matchesDomain('nytimes.com'));
  38. $this->assertFalse($cookie->matchesDomain('google.com'));
  39. }
  40. public function testMatchesDomainMatchesSubdomains()
  41. {
  42. $cookie = new Cookie();
  43. $cookie->setAttribute('domain', '.nytimes.com');
  44. $this->assertTrue($cookie->matchesDomain('nytimes.com'));
  45. $this->assertTrue($cookie->matchesDomain('blogs.nytimes.com'));
  46. $this->assertFalse($cookie->matchesDomain('google.com'));
  47. }
  48. public function testIsExpiredChecksMaxAge()
  49. {
  50. $cookie = new Cookie();
  51. $cookie->setAttribute('max-age', 60);
  52. $this->assertFalse($cookie->isExpired());
  53. $cookie = new Cookie();
  54. $cookie->setCreatedAt(strtotime('-1 hour'));
  55. $cookie->setAttribute('max-age', 60);
  56. $this->assertTrue($cookie->isExpired());
  57. }
  58. public function testIsExpiredChecksExpires()
  59. {
  60. $cookie = new Cookie();
  61. $cookie->setAttribute('expires', date('r', strtotime('+1 week')));
  62. $this->assertFalse($cookie->isExpired());
  63. $cookie = new Cookie();
  64. $cookie->setAttribute('expires', date('r', strtotime('-1 month')));
  65. $this->assertTrue($cookie->isExpired());
  66. }
  67. public function testMatchesPathChecksPath()
  68. {
  69. $cookie = new Cookie();
  70. $cookie->setAttribute('path', '/resource');
  71. $this->assertTrue($cookie->matchesPath('/resource/123'));
  72. $this->assertFalse($cookie->matchesPath('/login'));
  73. $cookie = new Cookie();
  74. $this->assertTrue($cookie->matchesPath('/resource/123'));
  75. }
  76. public function testMatchesRequestChecksDomain()
  77. {
  78. $request = new Message\Request();
  79. $request->setHost('http://example.com');
  80. $cookie = new Cookie();
  81. $cookie->setAttribute(Cookie::ATTR_DOMAIN, 'example.com');
  82. $this->assertTrue($cookie->matchesRequest($request));
  83. $cookie = new Cookie();
  84. $cookie->setAttribute(Cookie::ATTR_DOMAIN, 'foo.com');
  85. $this->assertFalse($cookie->matchesRequest($request));
  86. }
  87. public function testMatchesRequestChecksPath()
  88. {
  89. $request = new Message\Request();
  90. $request->setHost('http://example.com');
  91. $request->setResource('/foo/bar');
  92. $cookie = new Cookie();
  93. $cookie->setAttribute(Cookie::ATTR_DOMAIN, 'example.com');
  94. $cookie->setAttribute(Cookie::ATTR_PATH, '/foo');
  95. $this->assertTrue($cookie->matchesRequest($request));
  96. $cookie = new Cookie();
  97. $cookie->setAttribute(Cookie::ATTR_DOMAIN, 'example.com');
  98. $cookie->setAttribute(Cookie::ATTR_PATH, '/foo/bar/baz');
  99. $this->assertFalse($cookie->matchesRequest($request));
  100. }
  101. public function testMatchesRequestChecksSecureAttribute()
  102. {
  103. $request = new Message\Request();
  104. $request->setHost('https://example.com');
  105. $cookie = new Cookie();
  106. $cookie->setAttribute(Cookie::ATTR_DOMAIN, 'example.com');
  107. $cookie->setAttribute(Cookie::ATTR_SECURE, null);
  108. $this->assertTrue($cookie->matchesRequest($request));
  109. $request = new Message\Request();
  110. $request->setHost('http://example.com');
  111. $this->assertFalse($cookie->matchesRequest($request));
  112. }
  113. }