Skip to content

Commit

Permalink
handle not 200 responses
Browse files Browse the repository at this point in the history
  • Loading branch information
medilies committed Aug 13, 2024
1 parent f3751b3 commit b2103e4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/Dompurify/DompurifyService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use Medilies\Xssless\Exceptions\XsslessException;
use Medilies\Xssless\Interfaces\ConfigInterface;
use Medilies\Xssless\Interfaces\HasSetupInterface;
use Medilies\Xssless\Interfaces\ServiceInterface;
Expand Down Expand Up @@ -43,8 +44,14 @@ public function send(string $html): string
RequestOptions::JSON => [
'html' => $html,
],
RequestOptions::HTTP_ERRORS => false,
]);

if ($res->getStatusCode() !== 200) {
// TODO: log or detail content
throw new XsslessException('Failed to communicate with the service');
}

return $res->getBody();
}

Expand Down
20 changes: 16 additions & 4 deletions tests/Dompurify/DompurifyServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use GuzzleHttp\Exception\ConnectException;
use Medilies\Xssless\Dompurify\DompurifyService;
use Medilies\Xssless\Dompurify\DompurifyServiceConfig;
use Medilies\Xssless\Exceptions\XsslessException;
use Medilies\Xssless\Xssless;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Exception\ProcessTimedOutException;
Expand Down Expand Up @@ -46,6 +47,19 @@
expect(fn () => $cleaner->send($dirty))->toThrow(ConnectException::class);
});

it('throws response in not Ok', function () {
$cleaner = (new Xssless)->using(new DompurifyServiceConfig(
binary: __DIR__.'/js-mocks/service-respond-not-ok.mjs',
port: 63002,
));

$service = $cleaner->start();

expect(fn () => $cleaner->clean('foo'))->toThrow(XsslessException::class);

$service->stop();
});

// ----------------------------------------------------------------------------
// Real setup and clean
// ----------------------------------------------------------------------------
Expand All @@ -71,11 +85,9 @@
})->depends('setup()');

test('clean()', function () {
$config = new DompurifyServiceConfig(
$cleaner = (new Xssless)->using(new DompurifyServiceConfig(
port: 63001, // for parallel tests
);

$cleaner = (new Xssless)->using($config);
));

$service = $cleaner->start();

Expand Down
24 changes: 24 additions & 0 deletions tests/Dompurify/js-mocks/service-respond-not-ok.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import http from 'http';

if (process.argv.length < 4) {
console.error(`Usage: ${process.argv[0]} ${process.argv[1]} <host> <port>`);
process.exit(1);
}

const HOST = process.argv[2];
const PORT = process.argv[3];

/**
* @param {http.IncomingMessage} req
* @param {http.ServerResponse} res
*/
const requestListener = function (req, res) {
res.writeHead(422);
res.end("Meh");
};

const server = http.createServer(requestListener);

server.listen(PORT, HOST, () => {
console.log(`Server is running on http://${HOST}:${PORT}`);
});

0 comments on commit b2103e4

Please sign in to comment.