This repository has been archived by the owner on Jun 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
140 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -199,7 +199,7 @@ private function getUrl() | |
* | ||
* @return string The request body. | ||
*/ | ||
private function buildRequestBody(\DTS\eBaySDK\Types\BaseType $request) | ||
protected function buildRequestBody(\DTS\eBaySDK\Types\BaseType $request) | ||
{ | ||
if (!$request->hasAttachment()) { | ||
return $request->toRequestXml(); | ||
|
@@ -230,7 +230,7 @@ private function buildXopDocument(\DTS\eBaySDK\Types\BaseType $request) | |
/** | ||
* Builds the attachment part of the request body string. | ||
* | ||
* @param array $attachment The attachement | ||
* @param array $attachment The attachment | ||
* | ||
* @return string The attachment part of request body. | ||
*/ | ||
|
@@ -247,6 +247,44 @@ private function buildAttachmentBody(array $attachment) | |
); | ||
} | ||
|
||
/** | ||
* Builds the XML payload part of a multipart/form-data request body. | ||
* | ||
* @param \DTS\eBaySDK\Types\BaseType $request Request object containing the request information. | ||
* | ||
* @return string The XML payload part of a multipart/form-data request body. | ||
*/ | ||
protected function buildMultipartFormDataXMLPayload(\DTS\eBaySDK\Types\BaseType $request) | ||
{ | ||
return sprintf( | ||
'%s%s%s', | ||
'--boundary'.self::CRLF, | ||
'Content-Disposition: form-data; name="XML Payload"'.self::CRLF.self::CRLF, | ||
$request->toRequestXml().self::CRLF | ||
); | ||
} | ||
|
||
|
||
/** | ||
* Builds the file part of a multipart/form-data request body. | ||
* | ||
* @param string $name | ||
* @param array $attachment | ||
* | ||
* @return string The file part of a multipart/form-data request body. | ||
*/ | ||
protected function buildMultipartFormDataFilePayload($name, $attachment) | ||
{ | ||
return sprintf( | ||
'%s%s%s%s%s', | ||
'--boundary'.self::CRLF, | ||
'Content-Disposition: form-data; name="'.$name.'"; filename="picture"'.self::CRLF, | ||
'Content-Type: '.$attachment['mimeType'].self::CRLF.self::CRLF, | ||
$attachment['data'].self::CRLF, | ||
'--boundary--' | ||
); | ||
} | ||
|
||
/** | ||
* Helper function that builds the HTTP request headers. | ||
* | ||
|
@@ -258,7 +296,7 @@ private function buildAttachmentBody(array $attachment) | |
*/ | ||
private function buildRequestHeaders($name, $request, $body) | ||
{ | ||
$headers = $this->getEbayHeaders($name); | ||
$headers = []; | ||
|
||
if ($request->hasAttachment()) { | ||
$headers['Content-Type'] = 'multipart/related;boundary=MIME_boundary;type="application/xop+xml";start="<[email protected]>";start-info="text/xml"'; | ||
|
@@ -272,7 +310,7 @@ private function buildRequestHeaders($name, $request, $body) | |
|
||
$headers['Content-Length'] = strlen($body); | ||
|
||
return $headers; | ||
return array_merge($headers, $this->getEbayHeaders($name)); | ||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--boundary | ||
Content-Disposition: form-data; name="XML Payload" | ||
|
||
<?xml version="1.0" encoding="UTF-8"?> | ||
<UploadSiteHostedPicturesRequest xmlns="urn:ebay:apis:eBLBaseComponents"><PictureName>Example Picture</PictureName></UploadSiteHostedPicturesRequest> | ||
--boundary | ||
Content-Disposition: form-data; name="Example Picture"; filename="picture" | ||
Content-Type: image/jpeg | ||
|
||
ABC123 | ||
--boundary-- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
namespace DTS\eBaySDK\Types\Test; | ||
|
||
use DTS\eBaySDK\Trading\Services; | ||
use DTS\eBaySDK\Trading\Types; | ||
use DTS\eBaySDK\Test\Mocks\HttpHandler; | ||
|
||
class MultipartFormDataTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
private $httpHandler; | ||
private $service; | ||
private $request; | ||
private $requestXml; | ||
|
||
protected function setUp() | ||
{ | ||
/** | ||
* Use a class that will fake sending requests and getting responses. | ||
* The idea is that all the information needed to make the request is | ||
* passed to the client by the service. What we want to test is that the | ||
* is actually passed correctly. We are not testing the sending of the request | ||
* over the internet. | ||
* The HttpHandler contains properties that will be set when the service | ||
* makes the request. We can test these properties to check what the service is passing. | ||
*/ | ||
$this->httpHandler = new HttpHandler(); | ||
$this->service = new Services\TradingService([ | ||
'apiVersion' => '123', | ||
'credentials' => ['appId' => '', 'certId' => '', 'devId' => ''], | ||
'siteId' => 0, | ||
'httpHandler' => $this->httpHandler | ||
]); | ||
$this->request = new Types\UploadSiteHostedPicturesRequestType(); | ||
$this->request->PictureName = 'Example Picture'; | ||
$this->requestXml = rtrim(file_get_contents(__DIR__.'/../../Mocks/MultipartFormDataRequest')); | ||
} | ||
|
||
public function testHttpHeadersAreCreated() | ||
{ | ||
$this->request->attachment('ABC123', 'image/jpeg'); | ||
$this->service->uploadSiteHostedPictures($this->request); | ||
$this->assertArrayHasKey('Content-Type', $this->httpHandler->headers); | ||
$this->assertEquals('multipart/form-data;boundary="boundary"', $this->httpHandler->headers['Content-Type']); | ||
$this->assertArrayHasKey('Content-Length', $this->httpHandler->headers); | ||
$this->assertEquals(strlen($this->requestXml), $this->httpHandler->headers['Content-Length']); | ||
} | ||
|
||
public function testMultipartFormDataIsCreated() | ||
{ | ||
$this->request->attachment('ABC123', 'image/jpeg'); | ||
$this->service->uploadSiteHostedPictures($this->request); | ||
$this->assertEquals($this->requestXml, $this->httpHandler->body); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters