Skip to content

Commit

Permalink
Merge pull request #15 from pestphp/all-http-verbs
Browse files Browse the repository at this point in the history
All http verbs
  • Loading branch information
nunomaduro authored Dec 6, 2023
2 parents 4e732ed + ad9ebeb commit 42934bc
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 15 deletions.
29 changes: 27 additions & 2 deletions bin/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,42 @@ export default () => {

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 payload = options.payload ? options.payload : {};
let method = options.method ? options.method : 'get';

switch (method) {
case 'delete':
http.del(url, null, {
headers: { 'user-agent': userAgent },
});
break;
case 'get':
http.get(url, {
headers: { 'user-agent': userAgent },
});
break;
case 'head':
http.head(url, {
headers: { 'user-agent': userAgent },
});
break;
case 'options':
http.options(url, Object.keys(payload).length ? JSON.stringify(payload) : null, {
headers: { 'user-agent': userAgent },
});
break;
case 'patch':
http.patch(url, Object.keys(payload).length ? JSON.stringify(payload) : null, {
headers: { 'user-agent': userAgent },
});
break;
case 'put':
http.put(url, Object.keys(payload).length ? JSON.stringify(payload) : null, {
headers: { 'user-agent': userAgent },
});
break;
case 'post':
http.post(url, payload, {
http.post(url, JSON.stringify(payload), {
headers: { 'user-agent': userAgent },
});
break;
Expand Down
77 changes: 77 additions & 0 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ public function duration(int $seconds): self
return $this;
}

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

return $this;
}

/**
* Force the test to use get method
*/
Expand All @@ -81,6 +91,55 @@ public function get(): self
return $this;
}

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

return $this;
}

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

return $this;
}

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

return $this;
}

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

return $this;
}

/**
* Force the test to use post method
*
Expand All @@ -94,6 +153,24 @@ public function post(array $payload): self
return $this;
}

/**
* Getter for the method property
*/
public function method(): string
{
return $this->method;
}

/**
* Getter for the payload property
*
* @return array<string, mixed>
*/
public function payload(): array
{
return $this->payload;
}

/**
* Specifies that run should run with the given number of concurrent requests.
*/
Expand Down
61 changes: 48 additions & 13 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,62 @@ public function handleArguments(array $arguments): array
$run->concurrently((int) str_replace('--concurrency=', '', $argument));
}

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

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

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

if (str_starts_with($argument, '--options=')) {
$run->options($this->extractPayload('options', $argument));
} elseif ($argument === '--options') {
$run->options();
}

if (str_starts_with($argument, '--patch=')) {
$run->patch($this->extractPayload('patch', $argument));
} elseif ($argument === '--patch') {
$run->patch();
}

if (str_starts_with($argument, '--put=')) {
$run->put($this->extractPayload('put', $argument));
} elseif ($argument === '--put') {
$run->put();
}

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->post($this->extractPayload('post', $argument));
}
}

$run->dd();
}

/**
* Extracts the payload from the argument.
*
* @return array<string, mixed>
*/
private function extractPayload(string $method, string $argument): array
{
try {
return (array) json_decode(str_replace("--{$method}=", '', $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: --{$method}='{\"name\": \"Nuno\"}'",
]);

exit(0);
}
}
}
74 changes: 74 additions & 0 deletions tests/Unit/Factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

it('correctly use get method by default', function () {
expect($this->stress->method())->toBe('get');
});

it('correctly sets the delete method', function () {
$this->stress->delete();

expect($this->stress->method())->toBe('delete');
});

it('correctly sets the get method', function () {
$this->stress->get();

expect($this->stress->method())->toBe('get');
});

it('correctly sets the head method', function () {
$this->stress->head();

expect($this->stress->method())->toBe('head');
});

it('correctly sets the options method without payload', function () {
$this->stress->options();

expect($this->stress->method())->toBe('options')
->and($this->stress->payload())->toBe([]);
});

it('correctly sets the options method with payload', function () {
$this->stress->options(['foo' => 'bar']);

expect($this->stress->method())->toBe('options')
->and($this->stress->payload())->toBe(['foo' => 'bar']);
});

it('correctly sets the patch method without payload', function () {
$this->stress->patch();

expect($this->stress->method())->toBe('patch')
->and($this->stress->payload())->toBe([]);
});

it('correctly sets the patch method with payload', function () {
$this->stress->patch(['foo' => 'bar']);

expect($this->stress->method())->toBe('patch')
->and($this->stress->payload())->toBe(['foo' => 'bar']);
});

it('correctly sets the put method without payload', function () {
$this->stress->put();

expect($this->stress->method())->toBe('put')
->and($this->stress->payload())->toBe([]);
});

it('correctly sets the put method with payload', function () {
$this->stress->put(['foo' => 'bar']);

expect($this->stress->method())->toBe('put')
->and($this->stress->payload())->toBe(['foo' => 'bar']);
});

it('correctly sets the post method', function () {
$this->stress->post(['foo' => 'bar']);

expect($this->stress->method())->toBe('post')
->and($this->stress->payload())->toBe(['foo' => 'bar']);
});

0 comments on commit 42934bc

Please sign in to comment.