authentication_test.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. // $Id: authentication_test.php 1748 2008-04-14 01:50:41Z lastcraft $
  3. require_once(dirname(__FILE__) . '/../autorun.php');
  4. require_once(dirname(__FILE__) . '/../authentication.php');
  5. require_once(dirname(__FILE__) . '/../http.php');
  6. Mock::generate('SimpleHttpRequest');
  7. class TestOfRealm extends UnitTestCase {
  8. function testWithinSameUrl() {
  9. $realm = new SimpleRealm(
  10. 'Basic',
  11. new SimpleUrl('http://www.here.com/path/hello.html'));
  12. $this->assertTrue($realm->isWithin(
  13. new SimpleUrl('http://www.here.com/path/hello.html')));
  14. }
  15. function testInsideWithLongerUrl() {
  16. $realm = new SimpleRealm(
  17. 'Basic',
  18. new SimpleUrl('http://www.here.com/path/'));
  19. $this->assertTrue($realm->isWithin(
  20. new SimpleUrl('http://www.here.com/path/hello.html')));
  21. }
  22. function testBelowRootIsOutside() {
  23. $realm = new SimpleRealm(
  24. 'Basic',
  25. new SimpleUrl('http://www.here.com/path/'));
  26. $this->assertTrue($realm->isWithin(
  27. new SimpleUrl('http://www.here.com/path/more/hello.html')));
  28. }
  29. function testOldNetscapeDefinitionIsOutside() {
  30. $realm = new SimpleRealm(
  31. 'Basic',
  32. new SimpleUrl('http://www.here.com/path/'));
  33. $this->assertFalse($realm->isWithin(
  34. new SimpleUrl('http://www.here.com/pathmore/hello.html')));
  35. }
  36. function testInsideWithMissingTrailingSlash() {
  37. $realm = new SimpleRealm(
  38. 'Basic',
  39. new SimpleUrl('http://www.here.com/path/'));
  40. $this->assertTrue($realm->isWithin(
  41. new SimpleUrl('http://www.here.com/path')));
  42. }
  43. function testDifferentPageNameStillInside() {
  44. $realm = new SimpleRealm(
  45. 'Basic',
  46. new SimpleUrl('http://www.here.com/path/hello.html'));
  47. $this->assertTrue($realm->isWithin(
  48. new SimpleUrl('http://www.here.com/path/goodbye.html')));
  49. }
  50. function testNewUrlInSameDirectoryDoesNotChangeRealm() {
  51. $realm = new SimpleRealm(
  52. 'Basic',
  53. new SimpleUrl('http://www.here.com/path/hello.html'));
  54. $realm->stretch(new SimpleUrl('http://www.here.com/path/goodbye.html'));
  55. $this->assertTrue($realm->isWithin(
  56. new SimpleUrl('http://www.here.com/path/index.html')));
  57. $this->assertFalse($realm->isWithin(
  58. new SimpleUrl('http://www.here.com/index.html')));
  59. }
  60. function testNewUrlMakesRealmTheCommonPath() {
  61. $realm = new SimpleRealm(
  62. 'Basic',
  63. new SimpleUrl('http://www.here.com/path/here/hello.html'));
  64. $realm->stretch(new SimpleUrl('http://www.here.com/path/there/goodbye.html'));
  65. $this->assertTrue($realm->isWithin(
  66. new SimpleUrl('http://www.here.com/path/here/index.html')));
  67. $this->assertTrue($realm->isWithin(
  68. new SimpleUrl('http://www.here.com/path/there/index.html')));
  69. $this->assertTrue($realm->isWithin(
  70. new SimpleUrl('http://www.here.com/path/index.html')));
  71. $this->assertFalse($realm->isWithin(
  72. new SimpleUrl('http://www.here.com/index.html')));
  73. $this->assertFalse($realm->isWithin(
  74. new SimpleUrl('http://www.here.com/paths/index.html')));
  75. $this->assertFalse($realm->isWithin(
  76. new SimpleUrl('http://www.here.com/pathindex.html')));
  77. }
  78. }
  79. class TestOfAuthenticator extends UnitTestCase {
  80. function testNoRealms() {
  81. $request = new MockSimpleHttpRequest();
  82. $request->expectNever('addHeaderLine');
  83. $authenticator = new SimpleAuthenticator();
  84. $authenticator->addHeaders($request, new SimpleUrl('http://here.com/'));
  85. }
  86. function &createSingleRealm() {
  87. $authenticator = new SimpleAuthenticator();
  88. $authenticator->addRealm(
  89. new SimpleUrl('http://www.here.com/path/hello.html'),
  90. 'Basic',
  91. 'Sanctuary');
  92. $authenticator->setIdentityForRealm('www.here.com', 'Sanctuary', 'test', 'secret');
  93. return $authenticator;
  94. }
  95. function testOutsideRealm() {
  96. $request = new MockSimpleHttpRequest();
  97. $request->expectNever('addHeaderLine');
  98. $authenticator = &$this->createSingleRealm();
  99. $authenticator->addHeaders(
  100. $request,
  101. new SimpleUrl('http://www.here.com/hello.html'));
  102. }
  103. function testWithinRealm() {
  104. $request = new MockSimpleHttpRequest();
  105. $request->expectOnce('addHeaderLine');
  106. $authenticator = &$this->createSingleRealm();
  107. $authenticator->addHeaders(
  108. $request,
  109. new SimpleUrl('http://www.here.com/path/more/hello.html'));
  110. }
  111. function testRestartingClearsRealm() {
  112. $request = new MockSimpleHttpRequest();
  113. $request->expectNever('addHeaderLine');
  114. $authenticator = &$this->createSingleRealm();
  115. $authenticator->restartSession();
  116. $authenticator->addHeaders(
  117. $request,
  118. new SimpleUrl('http://www.here.com/hello.html'));
  119. }
  120. function testDifferentHostIsOutsideRealm() {
  121. $request = new MockSimpleHttpRequest();
  122. $request->expectNever('addHeaderLine');
  123. $authenticator = &$this->createSingleRealm();
  124. $authenticator->addHeaders(
  125. $request,
  126. new SimpleUrl('http://here.com/path/hello.html'));
  127. }
  128. }
  129. ?>