Basic.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. class RequestsTest_Auth_Basic extends PHPUnit_Framework_TestCase {
  3. public static function transportProvider() {
  4. $transports = array(
  5. array('Requests_Transport_fsockopen'),
  6. array('Requests_Transport_cURL'),
  7. );
  8. return $transports;
  9. }
  10. /**
  11. * @dataProvider transportProvider
  12. */
  13. public function testUsingArray($transport) {
  14. if (!call_user_func(array($transport, 'test'))) {
  15. $this->markTestSkipped($transport . ' is not available');
  16. return;
  17. }
  18. $options = array(
  19. 'auth' => array('user', 'passwd'),
  20. 'transport' => $transport,
  21. );
  22. $request = Requests::get(httpbin('/basic-auth/user/passwd'), array(), $options);
  23. $this->assertEquals(200, $request->status_code);
  24. $result = json_decode($request->body);
  25. $this->assertEquals(true, $result->authenticated);
  26. $this->assertEquals('user', $result->user);
  27. }
  28. /**
  29. * @dataProvider transportProvider
  30. */
  31. public function testUsingInstantiation($transport) {
  32. if (!call_user_func(array($transport, 'test'))) {
  33. $this->markTestSkipped($transport . ' is not available');
  34. return;
  35. }
  36. $options = array(
  37. 'auth' => new Requests_Auth_Basic(array('user', 'passwd')),
  38. 'transport' => $transport,
  39. );
  40. $request = Requests::get(httpbin('/basic-auth/user/passwd'), array(), $options);
  41. $this->assertEquals(200, $request->status_code);
  42. $result = json_decode($request->body);
  43. $this->assertEquals(true, $result->authenticated);
  44. $this->assertEquals('user', $result->user);
  45. }
  46. /**
  47. * @dataProvider transportProvider
  48. */
  49. public function testPOSTUsingInstantiation($transport) {
  50. if (!call_user_func(array($transport, 'test'))) {
  51. $this->markTestSkipped($transport . ' is not available');
  52. return;
  53. }
  54. $options = array(
  55. 'auth' => new Requests_Auth_Basic(array('user', 'passwd')),
  56. 'transport' => $transport,
  57. );
  58. $data = 'test';
  59. $request = Requests::post(httpbin('/post'), array(), $data, $options);
  60. $this->assertEquals(200, $request->status_code);
  61. $result = json_decode($request->body);
  62. $auth = $result->headers->Authorization;
  63. $auth = explode(' ', $auth);
  64. $this->assertEquals(base64_encode('user:passwd'), $auth[1]);
  65. $this->assertEquals('test', $result->data);
  66. }
  67. /**
  68. * @expectedException Requests_Exception
  69. */
  70. public function testMissingPassword() {
  71. $auth = new Requests_Auth_Basic(array('user'));
  72. }
  73. }