ApiMediaFileUploadTest.php 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. class ApiMediaFileUploadTest extends BaseTest
  21. {
  22. public function testMediaFile()
  23. {
  24. $client = $this->getClient();
  25. $request = new Google_Http_Request('http://www.example.com', 'POST');
  26. $media = new Google_Http_MediaFileUpload(
  27. $client,
  28. $request,
  29. 'image/png',
  30. base64_decode('data:image/png;base64,a')
  31. );
  32. $this->assertEquals(0, $media->getProgress());
  33. $this->assertGreaterThan(0, strlen($request->getPostBody()));
  34. }
  35. public function testGetUploadType()
  36. {
  37. $client = $this->getClient();
  38. $request = new Google_Http_Request('http://www.example.com', 'POST');
  39. // Test resumable upload
  40. $media = new Google_Http_MediaFileUpload($client, $request, 'image/png', 'a', true);
  41. $params = array('mediaUpload' => array('value' => $media));
  42. $this->assertEquals('resumable', $media->getUploadType(null));
  43. // Test data *only* uploads
  44. $media = new Google_Http_MediaFileUpload($client, $request, 'image/png', 'a', false);
  45. $this->assertEquals('media', $media->getUploadType(null));
  46. // Test multipart uploads
  47. $media = new Google_Http_MediaFileUpload($client, $request, 'image/png', 'a', false);
  48. $this->assertEquals('multipart', $media->getUploadType(array('a' => 'b')));
  49. }
  50. public function testResultCode()
  51. {
  52. $client = $this->getClient();
  53. $request = new Google_Http_Request('http://www.example.com', 'POST');
  54. // Test resumable upload
  55. $media = new Google_Http_MediaFileUpload($client, $request, 'image/png', 'a', true);
  56. $this->assertEquals(null, $media->getHttpResultCode());
  57. }
  58. public function testProcess()
  59. {
  60. $client = $this->getClient();
  61. $data = 'foo';
  62. // Test data *only* uploads.
  63. $request = new Google_Http_Request('http://www.example.com', 'POST');
  64. $media = new Google_Http_MediaFileUpload($client, $request, 'image/png', $data, false);
  65. $this->assertEquals($data, $request->getPostBody());
  66. // Test resumable (meta data) - we want to send the metadata, not the app data.
  67. $request = new Google_Http_Request('http://www.example.com', 'POST');
  68. $reqData = json_encode("hello");
  69. $request->setPostBody($reqData);
  70. $media = new Google_Http_MediaFileUpload($client, $request, 'image/png', $data, true);
  71. $this->assertEquals(json_decode($reqData), $request->getPostBody());
  72. // Test multipart - we are sending encoded meta data and post data
  73. $request = new Google_Http_Request('http://www.example.com', 'POST');
  74. $reqData = json_encode("hello");
  75. $request->setPostBody($reqData);
  76. $media = new Google_Http_MediaFileUpload($client, $request, 'image/png', $data, false);
  77. $this->assertContains($reqData, $request->getPostBody());
  78. $this->assertContains(base64_encode($data), $request->getPostBody());
  79. }
  80. }