Skip to content

Commit 682c63d

Browse files
authored
Add HTTP endpoint (#1)
1 parent be1c28c commit 682c63d

File tree

3 files changed

+106
-1
lines changed

3 files changed

+106
-1
lines changed

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
}
2828
},
2929
"require": {
30-
"php": ">7.0"
30+
"php": ">7.0",
31+
"yoanm/jsonrpc-server-sdk": "^1.0",
32+
"symfony/http-foundation": "^3.0 || ^4.0"
3133
},
3234
"require-dev": {
3335
"behat/behat": "~3.0",
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
namespace Yoanm\SymfonyJsonRpcHttpServer\Infra\Endpoint;
3+
4+
use Symfony\Component\HttpFoundation\Request;
5+
use Symfony\Component\HttpFoundation\Response;
6+
use Yoanm\JsonRpcServer\Infra\Endpoint\JsonRpcEndpoint as SDKJsonRpcEndpoint;
7+
8+
/**
9+
* Class JsonRpcHttpEndpoint
10+
*/
11+
class JsonRpcHttpEndpoint
12+
{
13+
/** @var SdkJsonRpcEndpoint */
14+
private $sdkEndpoint;
15+
16+
public function __construct(SDKJsonRpcEndpoint $sdkEndpoint)
17+
{
18+
$this->sdkEndpoint = $sdkEndpoint;
19+
}
20+
21+
public function index(Request $request) : Response
22+
{
23+
if (Request::METHOD_POST !== $request->getMethod()) {
24+
return new Response('A JSON-RPC HTTP call must use POST', Response::HTTP_METHOD_NOT_ALLOWED);
25+
}
26+
27+
return new Response(
28+
$this->sdkEndpoint->index($request->getContent())
29+
);
30+
}
31+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
namespace Tests\Functional\Infra\Endpoint\JsonRpcHttpEndpoint;
3+
4+
use PHPUnit\Framework\TestCase;
5+
use Prophecy\Argument;
6+
use Prophecy\Prophecy\ObjectProphecy;
7+
use Symfony\Component\HttpFoundation\Request;
8+
use Symfony\Component\HttpFoundation\Response;
9+
use Yoanm\SymfonyJsonRpcHttpServer\Infra\Endpoint\JsonRpcHttpEndpoint;
10+
use Yoanm\JsonRpcServer\Infra\Endpoint\JsonRpcEndpoint as SDKJsonRpcEndpoint;
11+
12+
/**
13+
* @covers \Yoanm\SymfonyJsonRpcHttpServer\Infra\Endpoint\JsonRpcHttpEndpoint
14+
*/
15+
class JsonRpcHttpEndpointTest extends TestCase
16+
{
17+
/** @var JsonRpcHttpEndpoint */
18+
private $endpoint;
19+
20+
/** @var SdkJsonRpcEndpoint|ObjectProphecy */
21+
private $sdkEndpoint;
22+
23+
protected function setUp()
24+
{
25+
$this->sdkEndpoint = $this->prophesize(SdkJsonRpcEndpoint::class);
26+
27+
$this->endpoint = new JsonRpcHttpEndpoint(
28+
$this->sdkEndpoint->reveal()
29+
);
30+
}
31+
32+
public function testShouldHandleRequestContentAndReturnA200ResponseContainingSDKEndpointReturnedValue()
33+
{
34+
$requestContent = 'request-content';
35+
$expextedResponseContent = 'expected-response-content';
36+
37+
/** @var Request|ObjectProphecy $request */
38+
$request = $this->prophesize(Request::class);
39+
40+
$request->getMethod()
41+
->willReturn(Request::METHOD_POST)
42+
->shouldBeCalled();
43+
44+
$request->getContent()
45+
->willReturn($requestContent)
46+
->shouldBeCalled();
47+
48+
$this->sdkEndpoint->index($requestContent)
49+
->willReturn($expextedResponseContent)
50+
->shouldBeCalled();
51+
52+
$response = $this->endpoint->index($request->reveal());
53+
54+
$this->assertSame(Response::HTTP_OK, $response->getStatusCode());
55+
$this->assertSame($expextedResponseContent, $response->getContent());
56+
}
57+
58+
public function testShouldCheckIfRequestUsePostMethodAndReturnErrorResponseIfNot()
59+
{
60+
/** @var Request|ObjectProphecy $request */
61+
$request = $this->prophesize(Request::class);
62+
63+
$request->getMethod()
64+
->willReturn(Request::METHOD_GET)
65+
->shouldBeCalled();
66+
67+
$response = $this->endpoint->index($request->reveal());
68+
69+
$this->assertSame(Response::HTTP_METHOD_NOT_ALLOWED, $response->getStatusCode());
70+
$this->assertSame('A JSON-RPC HTTP call must use POST', $response->getContent());
71+
}
72+
}

0 commit comments

Comments
 (0)