-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(http): add new requester to help generate http request for phpstorm
- Loading branch information
Showing
3 changed files
with
85 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace APITester\Requester; | ||
|
||
use APITester\Util\Json; | ||
use Nyholm\Psr7\Response; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
final class HttpDumpRequester extends Requester | ||
{ | ||
/** | ||
* @var ResponseInterface[] | ||
*/ | ||
private array $responses = []; | ||
|
||
public function request(RequestInterface $request, string $id): void | ||
{ | ||
$httpDump = $this->requestToHttp($request, $id); | ||
|
||
echo "\n" . $httpDump . "\n"; | ||
|
||
$response = new Response(200, [], 'Simulated response for ' . $id); | ||
$this->responses[$id] = $response; | ||
} | ||
|
||
public function getResponse(string $id): ResponseInterface | ||
{ | ||
return $this->responses[$id]; | ||
} | ||
|
||
public static function getName(): string | ||
{ | ||
return 'http-dump'; | ||
} | ||
|
||
private function requestToHttp(RequestInterface $request, string $requestName): string | ||
{ | ||
$dump = "###\n"; | ||
$dump .= $request->getMethod() . ' ' . '{{url}}' . $request->getUri() . "\n"; | ||
|
||
foreach ($request->getHeaders() as $name => $values) { | ||
foreach ($values as $value) { | ||
$dump .= "{$name}: {$value}\n"; | ||
} | ||
} | ||
|
||
$body = (string) $request->getBody(); | ||
if (!empty($body)) { | ||
if (Json::isJson($body)) { | ||
$body = Json::prettify($body); | ||
} | ||
$dump .= "\n" . $body . "\n"; | ||
} | ||
|
||
return $dump; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters