CallbackTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. namespace OSS\Tests;
  3. use OSS\Core\OssException;
  4. use OSS\OssClient;
  5. require_once __DIR__ . DIRECTORY_SEPARATOR . 'TestOssClientBase.php';
  6. class CallbackTest extends TestOssClientBase
  7. {
  8. public function testMultipartUploadCallbackNormal()
  9. {
  10. $object = "multipart-callback-test.txt";
  11. $copiedObject = "multipart-callback-test.txt.copied";
  12. $this->ossClient->putObject($this->bucket, $copiedObject, file_get_contents(__FILE__));
  13. /**
  14. * step 1. 初始化一个分块上传事件, 也就是初始化上传Multipart, 获取upload id
  15. */
  16. try {
  17. $upload_id = $this->ossClient->initiateMultipartUpload($this->bucket, $object);
  18. } catch (OssException $e) {
  19. $this->assertFalse(true);
  20. }
  21. /*
  22. * step 2. uploadPartCopy
  23. */
  24. $copyId = 1;
  25. $eTag = $this->ossClient->uploadPartCopy($this->bucket, $copiedObject, $this->bucket, $object, $copyId, $upload_id);
  26. $upload_parts[] = array(
  27. 'PartNumber' => $copyId,
  28. 'ETag' => $eTag,
  29. );
  30. try {
  31. $listPartsInfo = $this->ossClient->listParts($this->bucket, $object, $upload_id);
  32. $this->assertNotNull($listPartsInfo);
  33. } catch (OssException $e) {
  34. $this->assertTrue(false);
  35. }
  36. /**
  37. * step 3.
  38. */
  39. $json =
  40. '{
  41. "callbackUrl":"oss-demo.aliyuncs.com:23450",
  42. "callbackHost":"oss-cn-hangzhou.aliyuncs.com",
  43. "callbackBody":"{\"mimeType\":${mimeType},\"size\":${size},\"x:var1\":${x:var1},\"x:var2\":${x:var2}}",
  44. "callbackBodyType":"application/json"
  45. }';
  46. $var =
  47. '{
  48. "x:var1":"value1",
  49. "x:var2":"值2"
  50. }';
  51. $options = array(OssClient::OSS_CALLBACK => $json,
  52. OssClient::OSS_CALLBACK_VAR => $var
  53. );
  54. try {
  55. $result = $this->ossClient->completeMultipartUpload($this->bucket, $object, $upload_id, $upload_parts, $options);
  56. $this->assertEquals("200", $result['info']['http_code']);
  57. $this->assertEquals("{\"Status\":\"OK\"}", $result['body']);
  58. } catch (OssException $e) {
  59. $this->assertTrue(false);
  60. }
  61. }
  62. public function testMultipartUploadCallbackFailed()
  63. {
  64. $object = "multipart-callback-test.txt";
  65. $copiedObject = "multipart-callback-test.txt.copied";
  66. $this->ossClient->putObject($this->bucket, $copiedObject, file_get_contents(__FILE__));
  67. /**
  68. * step 1. 初始化一个分块上传事件, 也就是初始化上传Multipart, 获取upload id
  69. */
  70. try {
  71. $upload_id = $this->ossClient->initiateMultipartUpload($this->bucket, $object);
  72. } catch (OssException $e) {
  73. $this->assertFalse(true);
  74. }
  75. /*
  76. * step 2. uploadPartCopy
  77. */
  78. $copyId = 1;
  79. $eTag = $this->ossClient->uploadPartCopy($this->bucket, $copiedObject, $this->bucket, $object, $copyId, $upload_id);
  80. $upload_parts[] = array(
  81. 'PartNumber' => $copyId,
  82. 'ETag' => $eTag,
  83. );
  84. try {
  85. $listPartsInfo = $this->ossClient->listParts($this->bucket, $object, $upload_id);
  86. $this->assertNotNull($listPartsInfo);
  87. } catch (OssException $e) {
  88. $this->assertTrue(false);
  89. }
  90. /**
  91. * step 3.
  92. */
  93. $json =
  94. '{
  95. "callbackUrl":"www.baidu.com",
  96. "callbackHost":"oss-cn-hangzhou.aliyuncs.com",
  97. "callbackBody":"{\"mimeType\":${mimeType},\"size\":${size},\"x:var1\":${x:var1},\"x:var2\":${x:var2}}",
  98. "callbackBodyType":"application/json"
  99. }';
  100. $var =
  101. '{
  102. "x:var1":"value1",
  103. "x:var2":"值2"
  104. }';
  105. $options = array(OssClient::OSS_CALLBACK => $json,
  106. OssClient::OSS_CALLBACK_VAR => $var
  107. );
  108. try {
  109. $result = $this->ossClient->completeMultipartUpload($this->bucket, $object, $upload_id, $upload_parts, $options);
  110. $this->assertTrue(false);
  111. } catch (OssException $e) {
  112. $this->assertTrue(true);
  113. $this->assertEquals("203", $e->getHTTPStatus());
  114. }
  115. }
  116. public function testPutObjectCallbackNormal()
  117. {
  118. //json
  119. {
  120. $json =
  121. '{
  122. "callbackUrl":"oss-demo.aliyuncs.com:23450",
  123. "callbackHost":"oss-cn-hangzhou.aliyuncs.com",
  124. "callbackBody":"{\"mimeType\":${mimeType},\"size\":${size}}",
  125. "callbackBodyType":"application/json"
  126. }';
  127. $options = array(OssClient::OSS_CALLBACK => $json);
  128. $this->putObjectCallbackOk($options, "200");
  129. }
  130. //url
  131. {
  132. $url =
  133. '{
  134. "callbackUrl":"oss-demo.aliyuncs.com:23450",
  135. "callbackHost":"oss-cn-hangzhou.aliyuncs.com",
  136. "callbackBody":"bucket=${bucket}&object=${object}&etag=${etag}&size=${size}&mimeType=${mimeType}&imageInfo.height=${imageInfo.height}&imageInfo.width=${imageInfo.width}&imageInfo.format=${imageInfo.format}",
  137. "callbackBodyType":"application/x-www-form-urlencoded"
  138. }';
  139. $options = array(OssClient::OSS_CALLBACK => $url);
  140. $this->putObjectCallbackOk($options, "200");
  141. }
  142. // Unspecified typre
  143. {
  144. $url =
  145. '{
  146. "callbackUrl":"oss-demo.aliyuncs.com:23450",
  147. "callbackHost":"oss-cn-hangzhou.aliyuncs.com",
  148. "callbackBody":"bucket=${bucket}&object=${object}&etag=${etag}&size=${size}&mimeType=${mimeType}&imageInfo.height=${imageInfo.height}&imageInfo.width=${imageInfo.width}&imageInfo.format=${imageInfo.format}"
  149. }';
  150. $options = array(OssClient::OSS_CALLBACK => $url);
  151. $this->putObjectCallbackOk($options, "200");
  152. }
  153. //json and body is chinese
  154. {
  155. $json =
  156. '{
  157. "callbackUrl":"oss-demo.aliyuncs.com:23450",
  158. "callbackHost":"oss-cn-hangzhou.aliyuncs.com",
  159. "callbackBody":"{\" 春水碧于天,画船听雨眠。\":\"垆边人似月,皓腕凝霜雪。\"}",
  160. "callbackBodyType":"application/json"
  161. }';
  162. $options = array(OssClient::OSS_CALLBACK => $json);
  163. $this->putObjectCallbackOk($options, "200");
  164. }
  165. //url and body is chinese
  166. {
  167. $url =
  168. '{
  169. "callbackUrl":"oss-demo.aliyuncs.com:23450",
  170. "callbackHost":"oss-cn-hangzhou.aliyuncs.com",
  171. "callbackBody":"春水碧于天,画船听雨眠。垆边人似月,皓腕凝霜雪",
  172. "callbackBodyType":"application/x-www-form-urlencoded"
  173. }';
  174. $options = array(OssClient::OSS_CALLBACK => $url);
  175. $this->putObjectCallbackOk($options, "200");
  176. }
  177. //json and add callback_var
  178. {
  179. $json =
  180. '{
  181. "callbackUrl":"oss-demo.aliyuncs.com:23450",
  182. "callbackHost":"oss-cn-hangzhou.aliyuncs.com",
  183. "callbackBody":"{\"mimeType\":${mimeType},\"size\":${size},\"x:var1\":${x:var1},\"x:var2\":${x:var2}}",
  184. "callbackBodyType":"application/json"
  185. }';
  186. $var =
  187. '{
  188. "x:var1":"value1",
  189. "x:var2":"aliyun.com"
  190. }';
  191. $options = array(OssClient::OSS_CALLBACK => $json,
  192. OssClient::OSS_CALLBACK_VAR => $var
  193. );
  194. $this->putObjectCallbackOk($options, "200");
  195. }
  196. //url and add callback_var
  197. {
  198. $url =
  199. '{
  200. "callbackUrl":"oss-demo.aliyuncs.com:23450",
  201. "callbackHost":"oss-cn-hangzhou.aliyuncs.com",
  202. "callbackBody":"bucket=${bucket}&object=${object}&etag=${etag}&size=${size}&mimeType=${mimeType}&imageInfo.height=${imageInfo.height}&imageInfo.width=${imageInfo.width}&imageInfo.format=${imageInfo.format}&my_var1=${x:var1}&my_var2=${x:var2}",
  203. "callbackBodyType":"application/x-www-form-urlencoded"
  204. }';
  205. $var =
  206. '{
  207. "x:var1":"value1凌波不过横塘路,但目送,芳",
  208. "x:var2":"值2"
  209. }';
  210. $options = array(OssClient::OSS_CALLBACK => $url,
  211. OssClient::OSS_CALLBACK_VAR => $var
  212. );
  213. $this->putObjectCallbackOk($options, "200");
  214. }
  215. }
  216. public function testPutCallbackWithCallbackFailed()
  217. {
  218. {
  219. $json =
  220. '{
  221. "callbackUrl":"http://www.baidu.com",
  222. "callbackHost":"oss-cn-hangzhou.aliyuncs.com",
  223. "callbackBody":"{\"mimeType\":${mimeType},\"size\":${size}}",
  224. "callbackBodyType":"application/json"
  225. }';
  226. $options = array(OssClient::OSS_CALLBACK => $json);
  227. $this->putObjectCallbackFailed($options, "203");
  228. }
  229. {
  230. $url =
  231. '{
  232. "callbackUrl":"http://www.baidu.com",
  233. "callbackHost":"oss-cn-hangzhou.aliyuncs.com",
  234. "callbackBody":"bucket=${bucket}&object=${object}&etag=${etag}&size=${size}&mimeType=${mimeType}&imageInfo.height=${imageInfo.height}&imageInfo.width=${imageInfo.width}&imageInfo.format=${imageInfo.format}&my_var1=${x:var1}&my_var2=${x:var2}",
  235. "callbackBodyType":"application/x-www-form-urlencoded"
  236. }';
  237. $options = array(OssClient::OSS_CALLBACK => $url);
  238. $this->putObjectCallbackFailed($options, "203");
  239. }
  240. }
  241. private function putObjectCallbackOk($options, $status)
  242. {
  243. $object = "oss-php-sdk-callback-test.txt";
  244. $content = file_get_contents(__FILE__);
  245. try {
  246. $result = $this->ossClient->putObject($this->bucket, $object, $content, $options);
  247. $this->assertEquals($status, $result['info']['http_code']);
  248. $this->assertEquals("{\"Status\":\"OK\"}", $result['body']);
  249. } catch (OssException $e) {
  250. $this->assertFalse(true);
  251. }
  252. }
  253. private function putObjectCallbackFailed($options, $status)
  254. {
  255. $object = "oss-php-sdk-callback-test.txt";
  256. $content = file_get_contents(__FILE__);
  257. try {
  258. $result = $this->ossClient->putObject($this->bucket, $object, $content, $options);
  259. $this->assertTrue(false);
  260. } catch (OssException $e) {
  261. $this->assertEquals($status, $e->getHTTPStatus());
  262. $this->assertTrue(true);
  263. }
  264. }
  265. public function setUp()
  266. {
  267. parent::setUp();
  268. }
  269. }