From f5cfec0656e864fc49606d75eea972052431ddca Mon Sep 17 00:00:00 2001 From: Darron Engelbrecht Date: Fri, 24 May 2024 13:30:56 +0200 Subject: [PATCH 1/2] Add custom headers to requests --- bin/run.js | 18 +++++++++++------- src/Factory.php | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/bin/run.js b/bin/run.js index dac430b..22def29 100644 --- a/bin/run.js +++ b/bin/run.js @@ -9,40 +9,44 @@ export default () => { let payload = options.payload ? options.payload : {}; let method = options.method ? options.method : 'get'; + let headers = options.headers ? options.headers : {} + + headers['user-agent'] = userAgent; + switch (method) { case 'delete': http.del(url, null, { - headers: { 'user-agent': userAgent }, + headers, }); break; case 'get': http.get(url, { - headers: { 'user-agent': userAgent }, + headers, }); break; case 'head': http.head(url, { - headers: { 'user-agent': userAgent }, + headers, }); break; case 'options': http.options(url, Object.keys(payload).length ? JSON.stringify(payload) : null, { - headers: { 'user-agent': userAgent }, + headers, }); break; case 'patch': http.patch(url, Object.keys(payload).length ? JSON.stringify(payload) : null, { - headers: { 'user-agent': userAgent }, + headers, }); break; case 'put': http.put(url, Object.keys(payload).length ? JSON.stringify(payload) : null, { - headers: { 'user-agent': userAgent }, + headers, }); break; case 'post': http.post(url, JSON.stringify(payload), { - headers: { 'user-agent': userAgent }, + headers, }); break; } diff --git a/src/Factory.php b/src/Factory.php index 708675c..4c98b41 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -38,6 +38,13 @@ final class Factory */ private array $payload = []; + /** + * The headers to send. + * + * @var array + */ + private array $headers = []; + /** * The computed result, if any. */ @@ -71,6 +78,18 @@ public function duration(int $seconds): self return $this; } + /** + * Specifies the headers to send with the request. + * + * @param array $headers + */ + public function headers(array $headers): self + { + $this->headers = $headers; + + return $this; + } + /** * Force the test to use delete method */ @@ -215,6 +234,7 @@ public function run(): Result 'duration' => sprintf('%ds', $this->duration), 'method' => $this->method, 'payload' => $this->payload, + 'headers' => $this->headers, 'throw' => true, ], $this->verbose, From 3816a12d43356ff56554693105f8b11c30651d69 Mon Sep 17 00:00:00 2001 From: Darron Engelbrecht Date: Fri, 24 May 2024 14:05:50 +0200 Subject: [PATCH 2/2] Add test for headers --- tests/Unit/Factory.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/Unit/Factory.php b/tests/Unit/Factory.php index d0814f4..5da6606 100644 --- a/tests/Unit/Factory.php +++ b/tests/Unit/Factory.php @@ -72,3 +72,16 @@ expect($this->stress->method())->toBe('post') ->and($this->stress->payload())->toBe(['foo' => 'bar']); }); + +it('correctly sets the headers', function (): void { + $this->stress->headers(['foo' => 'bar']); + + $class = new \ReflectionClass($this->stress); + $protected = $class->getProperty('headers'); + $protected->setAccessible(true); + + $instance = $this->stress; + $headers = $protected->getValue($instance); + + expect($headers)->toBe(['foo' => 'bar']); +});