From 206fe756d87625c40261b2ca4360757af61dc257 Mon Sep 17 00:00:00 2001 From: Elliot Anderson Date: Sun, 7 Dec 2025 10:12:36 -0500 Subject: [PATCH] Fix issue #299: body() should not overwrite expected_type --- src/Httpful/Request.php | 5 ++-- tests/Httpful/HttpfulTest.php | 43 +++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/Httpful/Request.php b/src/Httpful/Request.php index 39c3882..cd9ddbb 100644 --- a/src/Httpful/Request.php +++ b/src/Httpful/Request.php @@ -343,13 +343,12 @@ public function authenticateWithCert($cert, $key, $passphrase = null, $encoding /** * Set the body of the request * @param mixed $payload - * @param string $mimeType currently, sets the sends AND expects mime type although this - * behavior may change in the next minor release (as it is a potential breaking change). + * @param string $mimeType $mimeType sets the Content-Type for the request body (does not affect expected response type) * @return Request */ public function body($payload, $mimeType = null) { - $this->mime($mimeType); + $this->contentType($mimeType); $this->payload = $payload; // Iserntentially don't call _serializePayload yet. Wait until // we actually send off the request to convert payload to string. diff --git a/tests/Httpful/HttpfulTest.php b/tests/Httpful/HttpfulTest.php index a03ac5f..a633f03 100644 --- a/tests/Httpful/HttpfulTest.php +++ b/tests/Httpful/HttpfulTest.php @@ -529,6 +529,49 @@ public function testShorthandMimeDefinition() $this->assertEquals(Mime::JSON, $r->expected_type); } + /** + * Test that expectsJson() before body() correctly preserves expected response type + * This fixes issue #299: calling expectsJson() before body() should not break response parsing. + */ + public function testExpectsJsonBeforeBodyPreservesExpectedType() + { + // Simulate the issue: expectsJson() then body() with FORM mime type + $req = Request::post('http://example.com/') + ->expectsJson() + ->body(['abc' => 123], Mime::FORM); + + + // verify that expected_type is still JSON (not overwritten to FORM) + $this->assertEquals(Mime::JSON, $req->expected_type); + + // Verify that content_type is FORM (for the request body) + $this->assertEquals(Mime::FORM, $req->content_type); + + // create a mock JSON response to verify it parses correctly + $jsonResponse = '{"exampleResponseFromServer": "data processed successfully"}'; + $response = new Response($jsonResponse, self::SAMPLE_JSON_HEADER, $req); + + // The response should be parsed as JSON (not as form data) + $this->assertIsObject($response->body); + $this->assertEquals('data processed successfully', $response->body->exampleResponseFromServer); + } + + /** + * Test that body() with mime type only sets content_type, not expected_type + */ + public function testBodyOnlySetsContentType() + { + $req = Request::post('http://example.com/') + ->expectsXml() + ->body(['key' => 'value'], Mime::JSON); + + // expected_type should remain XML + $this->assertEquals(Mime::XML, $req->expected_type); + + // content_type should be JSON (for the request body) + $this->assertEquals(Mime::JSON, $req->content_type); + } + public function testOverrideXmlHandler() { // Lazy test...