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 16 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
56 changes: 56 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,48 @@ public function duration(int $seconds): self
return $this;
}

/**
* Specifies that the test should be run using the given HTTP method.
*/
public function method(string $method): self
devajmeireles marked this conversation as resolved.
Show resolved Hide resolved
{
$method = strtolower($method);

assert(in_array($method, ['get', 'post'], true), 'The method must be one of: get, post');

$this->method = $method;

return $this;
}

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

/**
* Force the test to use post method
*/
public function post(): self
nahime0 marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->method('post');
}

/**
* Specifies the payload to send for the test, if any.
*
* @param array<string, mixed> $payload
*/
public function payload(array $payload): self
{
$this->payload = $payload;

return $this;
}

/**
* Specifies that run should run with the given number of concurrent requests.
*/
Expand Down Expand Up @@ -101,6 +155,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
30 changes: 28 additions & 2 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,38 @@ 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 (str_starts_with($argument, '--method=')) {
nahime0 marked this conversation as resolved.
Show resolved Hide resolved
$run->method(str_replace('--method=', '', $argument));
}

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

if ($argument === '--post') {
nahime0 marked this conversation as resolved.
Show resolved Hide resolved
$run->post();
}

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

exit(0);
}
$run->payload($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