Skip to content

Commit

Permalink
Add custom headers to requests
Browse files Browse the repository at this point in the history
  • Loading branch information
DarronEngelbrecht committed Nov 23, 2024
1 parent 9ec437a commit f5cfec0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
18 changes: 11 additions & 7 deletions bin/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
20 changes: 20 additions & 0 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ final class Factory
*/
private array $payload = [];

/**
* The headers to send.
*
* @var array<string, string>
*/
private array $headers = [];

/**
* The computed result, if any.
*/
Expand Down Expand Up @@ -71,6 +78,18 @@ public function duration(int $seconds): self
return $this;
}

/**
* Specifies the headers to send with the request.
*
* @param array<string, string> $headers
*/
public function headers(array $headers): self
{
$this->headers = $headers;

return $this;
}

/**
* Force the test to use delete method
*/
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit f5cfec0

Please sign in to comment.