Skip to content

Commit

Permalink
Merge pull request #34 from xp-forge/feature/marshalled-payload
Browse files Browse the repository at this point in the history
Add TestCall::payload() which returns marshalled payload
  • Loading branch information
thekid authored Nov 1, 2024
2 parents 8cea06c + ba168ad commit fad00c7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/main/php/webservices/rest/Endpoint.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
* @test webservices.rest.unittest.ExecuteTest
*/
class Endpoint implements Traceable {
protected $base, $formats, $transfer, $marshalling, $connections;
public $marshalling;
protected $base, $formats, $transfer, $connections;
private $headers= [];
private $cat= null;

Expand Down
16 changes: 15 additions & 1 deletion src/main/php/webservices/rest/TestCall.class.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php namespace webservices\rest;

use io\streams\{InputStream, MemoryInputStream};
use io\streams\{InputStream, MemoryInputStream, MemoryOutputStream};
use util\data\Marshalling;
use webservices\rest\io\{Reader, Transmission};

Expand All @@ -18,6 +18,20 @@ public function __construct(RestRequest $request, Formats $formats, $marshalling
/** Returns the request associated with this call */
public function request(): RestRequest { return $this->request; }

/** @return var */
public function content() {
if (null !== $this->transfer) {
return $this->transfer;
} else if ($payload= $this->request->payload()) {
$stream= new MemoryOutputStream();
$output= $this->formats->named($this->request->header('Content-Type') ?? null);
$output->serialize($this->marshalling->marshal($payload->value()), $stream);
return $stream->bytes();
} else {
return null;
}
}

/**
* Writes given bytes
*
Expand Down
6 changes: 6 additions & 0 deletions src/test/php/webservices/rest/unittest/EndpointTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use peer\URL;
use test\{Assert, Expect, Test, Values};
use util\URI;
use util\data\Marshalling;
use webservices\rest\{Endpoint, RestResource};

class EndpointTest {
Expand Down Expand Up @@ -39,6 +40,11 @@ public function port($base, $expected) {
Assert::equals($expected, $this->newFixture($base)->base()->port());
}

#[Test]
public function marshalling() {
Assert::instance(Marshalling::class, $this->newFixture()->marshalling);
}

#[Test]
public function headers_empty_by_default() {
Assert::equals([], $this->newFixture()->headers());
Expand Down
15 changes: 15 additions & 0 deletions src/test/php/webservices/rest/unittest/TestEndpointTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,19 @@ public function yields_404_for_unrouted_paths() {
$fixture= new TestEndpoint([]);
Assert::equals(404, $fixture->resource('/')->get()->status());
}

#[Test]
public function content() {
$fixture= new TestEndpoint([
'POST /users' => function($call) {
$payload= json_decode($call->content(), true);
return $call->respond(201, 'Created', ['Location' => '/users/'.md5($payload['name'])]);
}
]);
$r= $fixture->resource('/users')->post(['name' => 'Test'], 'application/json');

Assert::equals(201, $r->status());
Assert::equals('/users/'.md5('Test'), $r->headers()['Location']);
}

}

0 comments on commit fad00c7

Please sign in to comment.