Skip to content

Commit eea9734

Browse files
Amend request body workings (#10)
* [f] correct method calls * [f#5] change how response body is hydrated closes #5 * [ch] add test run on git push * [f] add post for test * [ch] move `rewind` method to `end` event - The resource should have the file pointer reset after the request has finished but was left in the wrong place in error and pointed out by @kelunik. refs #10 * Change what gets called after install (#13) * [ch] remove linter and docker cmd's from install * [f] update with new method calls * [ch] update docker commands for ease of use * [f] update composer script * [ch] try to run tests via composer
1 parent 0597753 commit eea9734

File tree

4 files changed

+60
-16
lines changed

4 files changed

+60
-16
lines changed

captainhook.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
},
1717
"pre-push": {
1818
"enabled": false,
19-
"actions": []
19+
"actions": [
20+
{
21+
"action": "composer test"
22+
}
23+
]
2024
}
2125
}

src/Server.php

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -93,21 +93,31 @@ public function run()
9393
$response->writeHead(['Content-Type' => $request->getHeaders()['Accept'][0]]);
9494
$response->end($body);
9595
} else {
96-
$psr7Request = new ServerRequest(
97-
[],
98-
[],
99-
$request->getPath(),
100-
$request->getMethod(),
101-
(new Stream('php://input', 'w+')),
102-
$request->getHeaders(),
103-
$request->getHeader('cookie'),
104-
$request->getQueryParams()
105-
);
106-
107-
$slimResponse = $this->slimInstance->process($psr7Request, new SlimResponse());
108-
$response->writeHead($slimResponse->getStatusCode(), $slimResponse->getHeaders());
109-
$slimResponse->getBody()->rewind();
110-
$response->end($slimResponse->getBody()->getContents());
96+
$stream = new Stream('php://memory', 'w+');
97+
98+
$request->on('data', function ($data) use ($stream) {
99+
$stream->write($data);
100+
});
101+
102+
$request->on('end', function () use ($request, $response, $stream) {
103+
$stream->rewind();
104+
105+
$psr7Request = new ServerRequest(
106+
[],
107+
[],
108+
$request->getPath(),
109+
$request->getMethod(),
110+
$stream,
111+
$request->getHeaders(),
112+
$request->getHeader('cookie'),
113+
$request->getQueryParams()
114+
);
115+
116+
$slimResponse = $this->slimInstance->process($psr7Request, new SlimResponse());
117+
$response->writeHead($slimResponse->getStatusCode(), $slimResponse->getHeaders());
118+
$slimResponse->getBody()->rewind();
119+
$response->end($slimResponse->getBody()->getContents());
120+
});
111121
}
112122
});
113123

test/Integration/ServerStub.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@
1212
->write('<h1>Welcome to ReactiveSlim</h1>');
1313
});
1414

15+
$slim->post('/json-post', function(\Psr\Http\Message\RequestInterface $req, \Psr\Http\Message\ResponseInterface $res) {
16+
return $res
17+
->withHeader('Content-Type', 'application/json')
18+
->getBody()
19+
->write(
20+
$req->getBody()->getContents()
21+
);
22+
});
23+
1524
(new \ReactiveSlim\Server($slim))
1625
->setHost('0.0.0.0')
1726
->setPort(1351)

test/Integration/ServerTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,25 @@ public function test_response_body_is_as_expected()
1515
$response = $this->client->get($this->makeURL(''));
1616
$this->assertEquals('<h1>Welcome to ReactiveSlim</h1>', $response->getBody());
1717
}
18+
19+
public function test_Json_data_is_returned_in_the_body()
20+
{
21+
$data = json_encode(['name' => 'Jason']);
22+
$headers = [
23+
'Accept: application/json',
24+
'Content-Type: application/json',
25+
];
26+
27+
$ch = curl_init('http://0.0.0.0:1351/json-post');
28+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
29+
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
30+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
31+
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
32+
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
33+
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
34+
$result = curl_exec($ch);
35+
curl_close($ch);
36+
37+
$this->assertEquals('{"name":"Jason"}', $result);
38+
}
1839
}

0 commit comments

Comments
 (0)