diff --git a/.github/README.md b/.github/README.md index b7ee0eb2..0f1b194e 100644 --- a/.github/README.md +++ b/.github/README.md @@ -494,6 +494,8 @@ Sometimes you will need to send custom form data like XML or a stream. In this c add the `HasBody` trait to your request class. After that, add a `defineBody` method on your request class. This method should return the raw body that you intend to send to the server. You should also specify headers so the receiving server can understand what Content-Type you are sending. +[If you are sending XML, consider using the HasXMLBody trait.](#xml-data) + ```php setQuery([ ]); ``` -### Other Plugins Available +## XML Data +If you are sending XML data to an XML service, you can use the `HasXMLBody` trait on your request which will automatically add the headers that you need to send XML. Once you add the trait, +make sure to add the `defineXMLBody` method where you can return your XML as a string. + +```php +'; + } +} +``` + +### All Plugins Available - AcceptsJson +- AlwaysThrowsOnErrors +- HasJsonBody +- HasBody +- HasXMLBody +- HasFormParams +- HasMultipartBody +- HasQueryParams - HasTimeout - WithDebugData - DisablesSSLVerification (Please be careful with this) diff --git a/src/Exceptions/SaloonHasBodyException.php b/src/Exceptions/SaloonTraitExistsException.php similarity index 53% rename from src/Exceptions/SaloonHasBodyException.php rename to src/Exceptions/SaloonTraitExistsException.php index b203c21c..2063945c 100644 --- a/src/Exceptions/SaloonHasBodyException.php +++ b/src/Exceptions/SaloonTraitExistsException.php @@ -4,6 +4,7 @@ use \Exception; -class SaloonHasBodyException extends Exception +class SaloonTraitExistsException extends Exception { + // } diff --git a/src/Traits/Features/HasBody.php b/src/Traits/Features/HasBody.php index 41b870f9..07623c40 100644 --- a/src/Traits/Features/HasBody.php +++ b/src/Traits/Features/HasBody.php @@ -2,8 +2,7 @@ namespace Sammyjo20\Saloon\Traits\Features; -use Sammyjo20\Saloon\Exceptions\SaloonHasBodyException; -use Sammyjo20\Saloon\Http\SaloonRequest; +use Sammyjo20\Saloon\Exceptions\SaloonTraitExistsException; trait HasBody { @@ -11,15 +10,9 @@ trait HasBody * Define any form body. * * @return void - * @throws SaloonHasBodyException - * @throws \Sammyjo20\Saloon\Exceptions\SaloonInvalidConnectorException */ public function bootHasBodyFeature(): void { - if ($this instanceof SaloonRequest && $this->traitExistsOnConnector(HasBody::class)) { - throw new SaloonHasBodyException('You can not have the HasBody trait on both the request and the connector at the same time.'); - } - $this->addConfig('body', $this->defineBody()); } diff --git a/src/Traits/Features/HasXMLBody.php b/src/Traits/Features/HasXMLBody.php new file mode 100644 index 00000000..8bbace1b --- /dev/null +++ b/src/Traits/Features/HasXMLBody.php @@ -0,0 +1,33 @@ +addHeader('Accept', 'application/xml'); + $this->addHeader('Content-Type', 'application/xml'); + + $this->addConfig('body', $this->defineXMLBody()); + } + + /** + * Define your XML body + * + * @return null + */ + public function defineXMLBody() + { + return null; + } +} diff --git a/tests/Resources/Requests/HasXMLRequest.php b/tests/Resources/Requests/HasXMLRequest.php new file mode 100644 index 00000000..7c3bedf3 --- /dev/null +++ b/tests/Resources/Requests/HasXMLRequest.php @@ -0,0 +1,42 @@ +'; + } +} diff --git a/tests/Unit/Features/HasBodyFeatureTest.php b/tests/Unit/Features/HasBodyFeatureTest.php index a250459c..5e1382a6 100644 --- a/tests/Unit/Features/HasBodyFeatureTest.php +++ b/tests/Unit/Features/HasBodyFeatureTest.php @@ -1,8 +1,6 @@ send(); }); - -test('it throws an exception if you try to add the hasBody trait to both the connector and the request', function () { - $request = new HasBodyConnectorRequest; - - $this->expectException(SaloonHasBodyException::class); - - $request->send(); -}); diff --git a/tests/Unit/Features/HasXMLBodyFeatureTest.php b/tests/Unit/Features/HasXMLBodyFeatureTest.php new file mode 100644 index 00000000..686928d7 --- /dev/null +++ b/tests/Unit/Features/HasXMLBodyFeatureTest.php @@ -0,0 +1,23 @@ +hydrate(); + + expect($requestManager->getHeaders())->toHaveKey('Accept', 'application/xml'); + expect($requestManager->getHeaders())->toHaveKey('Content-Type', 'application/xml'); + + $request->addHandler('hasBodyHandler', function (callable $handler) { + return function (RequestInterface $request, array $options) use ($handler) { + expect($request->getBody()->getContents())->toEqual(''); + + return $handler($request, $options); + }; + }); +});