Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/Controller/OData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Flat3\Lodata\Controller;

use Flat3\Lodata\Exception\Protocol\ProtocolException;
use Flat3\Lodata\Helper\Constants;
use Illuminate\Routing\Controller;

Expand All @@ -24,12 +25,20 @@ public function handle(Request $request, Transaction $transaction, Async $job):
{
$transaction->initialize($request);

if ($transaction->hasPreference(Constants::respondAsync)) {
$job->setTransaction($transaction);
$job->dispatch();
}
try {
if ($transaction->hasPreference(Constants::respondAsync)) {
$job->setTransaction($transaction);
$job->dispatch();
}

return $transaction->execute();
} catch (ProtocolException $e) {
if ($e->getHttpCode() < 400) {
return $e->toResponse($request);
}

return $transaction->execute();
throw $e;
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Exception/Protocol/AcceptedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ class AcceptedException extends ProtocolException
protected $httpCode = Response::HTTP_ACCEPTED;
protected $odataCode = 'accepted';
protected $message = 'Accepted';
protected $suppressContent = true;
}
14 changes: 13 additions & 1 deletion src/Exception/Protocol/ProtocolException.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,16 @@ public function toError()
];
}

/**
* Get the HTTP Code associated to the exception
*
* @return int
*/
public function getHttpCode(): int
{
return $this->httpCode;
}

/**
* Get the original exception that caused this exception
*
Expand Down Expand Up @@ -188,7 +198,9 @@ public function toResponse($request = null): Response
$response->setProtocolVersion('1.1');
$response->setStatusCode($this->httpCode);
$response->headers->replace($this->headers);
$response->headers->set(Constants::contentType, MediaType::json);
if (!$this->suppressContent) {
$response->headers->set(Constants::contentType, MediaType::json);
}

return $response;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Helpers/UseODataAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ trait UseODataAssertions
{
protected function assertODataError(Request $request, int $code): TestResponse
{
$emptyCodes = [Response::HTTP_NO_CONTENT, Response::HTTP_FOUND, Response::HTTP_NOT_MODIFIED];
$emptyCodes = [Response::HTTP_ACCEPTED, Response::HTTP_NO_CONTENT, Response::HTTP_FOUND, Response::HTTP_NOT_MODIFIED];

try {
$response = $this->req($request);
Expand Down Expand Up @@ -146,4 +146,4 @@ protected function assertResultCount(TestResponse $response, int $count)
{
$this->assertEquals($count, count(json_decode($response->streamedContent())->value));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
],
"cache-control": [
"no-cache, private"
],
"content-type": [
"application/json"
]
},
"status": 204
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
],
"cache-control": [
"no-cache, private"
],
"content-type": [
"application/json"
]
},
"status": 204
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
],
"cache-control": [
"no-cache, private"
],
"content-type": [
"application/json"
]
},
"status": 204
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
],
"cache-control": [
"no-cache, private"
],
"content-type": [
"application/json"
]
},
"status": 204
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
],
"cache-control": [
"no-cache, private"
],
"content-type": [
"application/json"
]
},
"status": 204
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
],
"cache-control": [
"no-cache, private"
],
"content-type": [
"application/json"
]
},
"status": 204
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
],
"cache-control": [
"no-cache, private"
],
"content-type": [
"application/json"
]
},
"status": 204
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
],
"cache-control": [
"no-cache, private"
],
"content-type": [
"application/json"
]
},
"status": 204
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
],
"cache-control": [
"no-cache, private"
],
"content-type": [
"application/json"
]
},
"status": 204
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
],
"cache-control": [
"no-cache, private"
],
"content-type": [
"application/json"
]
},
"status": 204
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
],
"cache-control": [
"no-cache, private"
],
"content-type": [
"application/json"
]
},
"status": 204
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
],
"cache-control": [
"no-cache, private"
],
"content-type": [
"application/json"
]
},
"status": 204
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
],
"cache-control": [
"no-cache, private"
],
"content-type": [
"application/json"
]
},
"status": 204
Expand Down
16 changes: 9 additions & 7 deletions tests/__snapshots__/Protocol/AsyncTest__test_async__1.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{
"error": {
"code": "accepted",
"message": "Accepted",
"target": null,
"details": [],
"innererror": {}
}
"headers": {
"location": [
"http://localhost/odata/_lodata/monitor/00000000-0000-0000-0000-000000000001"
],
"cache-control": [
"no-cache, private"
]
},
"status": 202
}
19 changes: 12 additions & 7 deletions tests/__snapshots__/Protocol/AsyncTest__test_async__2.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
{
"error": {
"code": "accepted",
"message": "Accepted",
"target": null,
"details": [],
"innererror": {}
}
"headers": {
"cache-control": [
"no-cache, private"
],
"content-type": [
"application/json;metadata=minimal"
],
"odata-version": [
"4.01"
]
},
"status": 200
}
20 changes: 8 additions & 12 deletions tests/__snapshots__/Protocol/AsyncTest__test_async__3.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
{
"headers": {
"location": [
"http://localhost/odata/_lodata/monitor/00000000-0000-0000-0000-000000000001"
],
"cache-control": [
"no-cache, private"
],
"content-type": [
"application/json"
]
},
"status": 202
"@context": "http://localhost/odata/$metadata",
"value": [
{
"name": "passengers",
"kind": "EntitySet",
"url": "passengers"
}
]
}
20 changes: 8 additions & 12 deletions tests/__snapshots__/Protocol/AsyncTest__test_async__4.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
{
"headers": {
"cache-control": [
"no-cache, private"
],
"content-type": [
"application/json;metadata=minimal"
],
"odata-version": [
"4.01"
]
},
"status": 200
"@context": "http://localhost/odata/$metadata",
"value": [
{
"name": "passengers",
"kind": "EntitySet",
"url": "passengers"
}
]
}
23 changes: 15 additions & 8 deletions tests/__snapshots__/Protocol/AsyncTest__test_async__5.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
{
"@context": "http://localhost/odata/$metadata",
"value": [
{
"name": "passengers",
"kind": "EntitySet",
"url": "passengers"
}
]
"headers": {
"cache-control": [
"no-cache, private"
],
"content-type": [
"application/json;metadata=minimal"
],
"odata-version": [
"4.01"
],
"asyncresult": [
"200"
]
},
"status": 200
}
15 changes: 7 additions & 8 deletions tests/__snapshots__/Protocol/AsyncTest__test_async__6.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{
"@context": "http://localhost/odata/$metadata",
"value": [
{
"name": "passengers",
"kind": "EntitySet",
"url": "passengers"
}
]
"error": {
"code": "not_found",
"message": "Not found",
"target": null,
"details": [],
"innererror": {}
}
}
16 changes: 9 additions & 7 deletions tests/__snapshots__/Protocol/AsyncTest__test_async_batch__1.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{
"error": {
"code": "accepted",
"message": "Accepted",
"target": null,
"details": [],
"innererror": {}
}
"headers": {
"location": [
"http://localhost/odata/_lodata/monitor/00000000-0000-0000-0000-000000000001"
],
"cache-control": [
"no-cache, private"
]
},
"status": 202
}
19 changes: 12 additions & 7 deletions tests/__snapshots__/Protocol/AsyncTest__test_async_batch__2.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
{
"error": {
"code": "accepted",
"message": "Accepted",
"target": null,
"details": [],
"innererror": {}
}
"headers": {
"cache-control": [
"no-cache, private"
],
"content-type": [
"multipart/mixed;boundary=00000000-0000-0000-0000-000000000002"
],
"odata-version": [
"4.01"
]
},
"status": 200
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

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

HTTP/1.1 404 Not Found
content-type: application/json

{"error":{"code":"no_handler","message":"No route handler was able to process this request","target":null,"details":[],"innererror":{}}}
--00000000-0000-0000-0000-000000000002--
Loading