HttpTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace Test\Integration;
  3. require_once dirname(__DIR__) . '/Setup.php';
  4. use Test\Setup;
  5. use Braintree;
  6. class HttpTest extends Setup
  7. {
  8. public function testProductionSSL()
  9. {
  10. try {
  11. Braintree\Configuration::environment('production');
  12. $this->setExpectedException('Braintree\Exception\Authentication');
  13. $http = new Braintree\Http(Braintree\Configuration::$global);
  14. $http->get('/');
  15. } catch (Braintree\Exception $e) {
  16. Braintree\Configuration::environment('development');
  17. throw $e;
  18. }
  19. Braintree\Configuration::environment('development');
  20. }
  21. public function testSandboxSSL()
  22. {
  23. try {
  24. Braintree\Configuration::environment('sandbox');
  25. $this->setExpectedException('Braintree\Exception\Authentication');
  26. $http = new Braintree\Http(Braintree\Configuration::$global);
  27. $http->get('/');
  28. } catch (Braintree\Exception $e) {
  29. Braintree\Configuration::environment('development');
  30. throw $e;
  31. }
  32. Braintree\Configuration::environment('development');
  33. }
  34. public function testSandboxSSLWithExplicitVersionSet()
  35. {
  36. try {
  37. Braintree\Configuration::environment('sandbox');
  38. Braintree\Configuration::sslVersion(6);
  39. $this->setExpectedException('Braintree\Exception\Authentication');
  40. $http = new Braintree\Http(Braintree\Configuration::$global);
  41. $http->get('/');
  42. } catch (Braintree\Exception $e) {
  43. Braintree\Configuration::environment('development');
  44. Braintree\Configuration::sslVersion(null);
  45. throw $e;
  46. }
  47. Braintree\Configuration::environment('development');
  48. Braintree\Configuration::sslVersion(null);
  49. }
  50. public function testSandboxSSLFailsWithIncompatibleSSLVersion()
  51. {
  52. try {
  53. Braintree\Configuration::environment('sandbox');
  54. Braintree\Configuration::sslVersion(3);
  55. $this->setExpectedException('Braintree\Exception\SSLCertificate', null, 35);
  56. $http = new Braintree\Http(Braintree\Configuration::$global);
  57. $http->get('/');
  58. } catch (Braintree\Exception $e) {
  59. Braintree\Configuration::environment('development');
  60. Braintree\Configuration::sslVersion(null);
  61. throw $e;
  62. }
  63. Braintree\Configuration::environment('development');
  64. Braintree\Configuration::sslVersion(null);
  65. }
  66. public function testSslError()
  67. {
  68. try {
  69. Braintree\Configuration::environment('sandbox');
  70. $this->setExpectedException('Braintree\Exception\SSLCertificate', null, 3);
  71. $http = new Braintree\Http(Braintree\Configuration::$global);
  72. $http->_doUrlRequest('get', '/malformed_url');
  73. } catch (Braintree\Exception $e) {
  74. Braintree\Configuration::environment('development');
  75. throw $e;
  76. }
  77. Braintree\Configuration::environment('development');
  78. }
  79. public function testAcceptGzipEncodingSetFalse()
  80. {
  81. $originalGzipEncoding = Braintree\Configuration::acceptGzipEncoding();
  82. Braintree\Configuration::acceptGzipEncoding(false);
  83. try {
  84. $result = Braintree\Customer::create([
  85. 'firstName' => 'Mike',
  86. 'lastName' => 'Jones',
  87. 'company' => 'Jones Co.',
  88. 'email' => 'mike.jones@example.com',
  89. 'phone' => '419.555.1234',
  90. 'fax' => '419.555.1235',
  91. 'website' => 'http://example.com'
  92. ]);
  93. $this->assertEquals(true, $result->success);
  94. $customer = $result->customer;
  95. $this->assertEquals('Mike', $customer->firstName);
  96. } catch(Braintree\Exception $e) {
  97. Braintree\Configuration::acceptGzipEncoding($originalGzipEncoding);
  98. throw $e;
  99. }
  100. Braintree\Configuration::acceptGzipEncoding($originalGzipEncoding);
  101. }
  102. public function testAcceptGzipEncodingSetToTrue()
  103. {
  104. $originalGzipEncoding = Braintree\Configuration::acceptGzipEncoding();
  105. Braintree\Configuration::acceptGzipEncoding(true);
  106. try {
  107. $result = Braintree\Customer::create([
  108. 'firstName' => 'Mike',
  109. 'lastName' => 'Jones',
  110. 'company' => 'Jones Co.',
  111. 'email' => 'mike.jones@example.com',
  112. 'phone' => '419.555.1234',
  113. 'fax' => '419.555.1235',
  114. 'website' => 'http://example.com'
  115. ]);
  116. $this->assertEquals(true, $result->success);
  117. $customer = $result->customer;
  118. $this->assertEquals('Mike', $customer->firstName);
  119. } catch(Braintree\Exception $e) {
  120. Braintree\Configuration::acceptGzipEncoding($originalGzipEncoding);
  121. throw $e;
  122. }
  123. Braintree\Configuration::acceptGzipEncoding($originalGzipEncoding);
  124. }
  125. public function testAuthorizationWithConfig()
  126. {
  127. $config = new Braintree\Configuration([
  128. 'environment' => 'development',
  129. 'merchant_id' => 'integration_merchant_id',
  130. 'publicKey' => 'badPublicKey',
  131. 'privateKey' => 'badPrivateKey'
  132. ]);
  133. $http = new Braintree\Http($config);
  134. $result = $http->_doUrlRequest('GET', $config->baseUrl() . '/merchants/integration_merchant_id/customers');
  135. $this->assertEquals(401, $result['status']);
  136. }
  137. public function testPostMultiPartUploadsFileSuccessfully()
  138. {
  139. $config = Braintree\Configuration::$global;
  140. $http = new Braintree\Http($config);
  141. $path = '/merchants/integration_merchant_id/document_uploads';
  142. $params = [
  143. 'document_upload[kind]' => 'evidence_document'
  144. ];
  145. $file = fopen(dirname(__DIR__) . '/fixtures/bt_logo.png', 'rb');
  146. $response = $http->postMultipart($path, $params, $file);
  147. $this->assertEquals('image/png', $response['documentUpload']['contentType']);
  148. $this->assertNotNull($response['documentUpload']['id']);
  149. }
  150. }