diff --git a/bin/run.js b/bin/run.js index 1dd80da..dac430b 100644 --- a/bin/run.js +++ b/bin/run.js @@ -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; diff --git a/src/Factory.php b/src/Factory.php index b366369..8e6e64a 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -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 */ @@ -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 $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 $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 $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 * @@ -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 + */ + public function payload(): array + { + return $this->payload; + } + /** * Specifies that run should run with the given number of concurrent requests. */ diff --git a/src/Plugin.php b/src/Plugin.php index f80ec9c..a13eb0d 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -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 + */ + 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); + } + } } diff --git a/tests/Unit/Factory.php b/tests/Unit/Factory.php new file mode 100644 index 0000000..993354e --- /dev/null +++ b/tests/Unit/Factory.php @@ -0,0 +1,74 @@ +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']); +});