Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
27pchrisl committed Jan 22, 2024
1 parent 5ddd640 commit 71df6ea
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 7 deletions.
17 changes: 12 additions & 5 deletions src/Transaction/MultipartDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,16 @@ public function parseDocuments(): self
foreach ($documents as $document) {
$multipart = new self();

list($headers, $body) = array_map('trim', explode("\r\n\r\n", $document, 2));

foreach (explode("\r\n", $headers) as $header) {
list($headers, $body) = array_map(
'trim',
explode(
"\n\n",
str_replace("\r\n", "\n", $document),
2
)
);

foreach (explode("\n", $headers) as $header) {
list($key, $value) = explode(':', $header, 2);
$key = strtolower($key);
$multipart->headers[$key] = $multipart->headers[$key] ?? [];
Expand All @@ -140,7 +147,7 @@ public function parseDocuments(): self
*/
public function toRequest(): Request
{
$httpRequest = explode("\r\n", $this->body);
$httpRequest = explode("\n", $this->body);
$requestLine = array_shift($httpRequest);

list($method, $requestURI, $httpVersion) = array_pad(explode(' ', $requestLine), 3, '');
Expand Down Expand Up @@ -181,7 +188,7 @@ public function toRequest(): Request
$headers[$key] = trim($value);
}

$body = implode("\r\n", $httpRequest);
$body = implode("\n", $httpRequest);

$request = Request::create($uri, $method, [], [], [], [], $body);
$request->headers->replace($headers);
Expand Down
4 changes: 2 additions & 2 deletions tests/Helpers/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ public function post(): self
return $this->method(\Illuminate\Http\Request::METHOD_POST);
}

public function multipart(string $body): self
public function multipart(string $body, bool $convertNewlines = true): self
{
$this->header('accept', 'multipart/mixed');
$this->body = str_replace("\n", "\r\n", $body);
$this->body = $convertNewlines ? str_replace("\n", "\r\n", $body) : $body;

return $this;
}
Expand Down
21 changes: 21 additions & 0 deletions tests/Protocol/BatchMultipartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,27 @@ public function test_relative_path()
);
}

public function test_non_crlf_newlines()
{
$this->assertTextMetadataResponse(
(new Request)
->path('/$batch')
->header('content-type', 'multipart/mixed; boundary=batch_36522ad7-fc75-4b56-8c71-56071383e77b')
->post()
->multipart(<<<MULTIPART
--batch_36522ad7-fc75-4b56-8c71-56071383e77b
Content-Type: application/http
GET flights(1)
Host: localhost
--batch_36522ad7-fc75-4b56-8c71-56071383e77b--
MULTIPART, false
)
);
}

public function test_prefer_metadata()
{
$this->assertTextMetadataResponse(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

--00000000-0000-0000-0000-000000000002
content-type: application/http

HTTP/1.0 200 OK
content-type: application/json;metadata=minimal
etag: W/"a714e2db58e276ecdcfcd98c25a0085a1465f571f780135a3ef2e976ec3a0afe"

{"@context":"http://localhost/odata/$metadata#flights/$entity","id":1,"origin":"lhr","destination":"lax","gate":null,"duration":"PT11H25M0S"}
--00000000-0000-0000-0000-000000000002--
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"headers": {
"cache-control": [
"no-cache, private"
],
"content-type": [
"multipart/mixed;boundary=00000000-0000-0000-0000-000000000002"
],
"odata-version": [
"4.01"
]
},
"status": 200
}

0 comments on commit 71df6ea

Please sign in to comment.