Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 2 additions & 3 deletions src/Httpful/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
43 changes: 43 additions & 0 deletions tests/Httpful/HttpfulTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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...
Expand Down