Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow different verbs payload #8

Merged
merged 17 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions bin/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,24 @@ import http from 'k6/http';
export const options = JSON.parse(__ENV.PEST_STRESS_TEST_OPTIONS);

export default () => {
http.get(__ENV.PEST_STRESS_TEST_URL, {
headers: { 'user-agent': 'Pest Plugin Stressless (https://pestphp.com) + K6 (https://k6.io)' },
});

let userAgent = 'Pest Plugin Stressless (https://pestphp.com) + K6 (https://k6.io)';
let url = __ENV.PEST_STRESS_TEST_URL;
let payload = options.payload ? JSON.stringify(options.payload) : JSON.stringify({});
let method = options.method ? options.method : 'get';

switch (method) {
case 'get':
http.get(url, {
headers: { 'user-agent': userAgent },
});
break;
case 'post':
http.post(url, payload, {
headers: { 'user-agent': userAgent },
});
break;
}
}

export function handleSummary(data) {
Expand Down
37 changes: 37 additions & 0 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ final class Factory
*/
private int $duration = 5;

/**
* The HTTP method to use.
*/
private string $method = 'get';

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

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

/**
* Force the test to use get method
*/
public function get(): self
{
$this->method = 'get';

return $this;
}

/**
* Force the test to use post method
*
* @param array<string, mixed> $payload The payload to send with the POST request
*/
public function post(array $payload): self
{
$this->method = 'post';
$this->payload = $payload;

return $this;
}

/**
* Specifies that run should run with the given number of concurrent requests.
*/
Expand Down Expand Up @@ -101,6 +136,8 @@ public function run(): Result
[
'vus' => $this->concurrency,
'duration' => sprintf('%ds', $this->duration),
'method' => $this->method,
'payload' => $this->payload,
'throw' => true,
],
$this->verbose,
Expand Down
22 changes: 20 additions & 2 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,30 @@ public function handleArguments(array $arguments): array
if (str_starts_with($argument, '--duration=')) {
$run->duration((int) str_replace('--duration=', '', $argument));
}
}

foreach ($arguments as $argument) {
if (str_starts_with($argument, '--concurrency=')) {
$run->concurrently((int) str_replace('--concurrency=', '', $argument));
}

if ($argument === '--get') {
$run->get();
}

if (str_starts_with($argument, '--post=')) {
try {
$payload = (array) json_decode(str_replace('--post=', '', $argument),
true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException) {
View::render('components.badge', [
'type' => 'ERROR',
'content' => 'Invalid JSON payload. Please provide a valid JSON payload.'.
'Example: --payload=\'{"name": "Nuno"}\'',
]);

exit(0);
}
$run->post($payload);
}
}

$run->dd();
Expand Down
4 changes: 2 additions & 2 deletions src/Run.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class Run
/**
* Creates a new run instance.
*
* @param array{vus: int, duration: string} $options
* @param array{vus: int, duration: string, method: string, payload: array<string, mixed>, throw: boolean} $options
*/
public function __construct(
readonly private Url $url,
Expand Down Expand Up @@ -71,7 +71,7 @@ public function start(): Result
$process = new Process([
K6::make(), 'run', 'run.js', '--out', "json={$this->session->progressPath()}",
], $basePath.'/bin', [
'PEST_STRESS_TEST_OPTIONS' => json_encode($this->options, JSON_THROW_ON_ERROR),
'PEST_STRESS_TEST_OPTIONS' => json_encode($this->options, JSON_FORCE_OBJECT | JSON_THROW_ON_ERROR),
'PEST_STRESS_TEST_URL' => $this->url,
'PEST_STRESS_TEST_SUMMARY_PATH' => $this->session->summaryPath(),
]);
Expand Down
Loading