Skip to content

Commit

Permalink
fix(invalid-content-type): null body should not have content-type hea…
Browse files Browse the repository at this point in the history
…der (#20)

This commit bears a fix for the case when request body is null but the content-type header is being added unless specified in the spec.  This commit address the fix for this and does not allow to add content-type header for null body request.

closes #19
  • Loading branch information
sufyankhanrao authored Feb 2, 2023
1 parent 3f91e5a commit 2dcd333
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
12 changes: 7 additions & 5 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="tests/bootstrap.php"
colors="true"
Expand All @@ -11,15 +12,16 @@
stopOnError="false"
stopOnFailure="false"
verbose="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
>
<testsuites>
<testsuite name="SDK Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<coverage includeUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</include>
</coverage>
</phpunit>
5 changes: 5 additions & 0 deletions src/Request/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@ public function setBodyFormat(string $format, callable $serializer): void
if (!empty($this->parameters)) {
return;
}

if (is_null($this->body)) {
return;
}

$this->addContentType($format);
$this->body = Closure::fromCallable($serializer)($this->body);
}
Expand Down
43 changes: 38 additions & 5 deletions tests/ApiCallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,42 @@ public function testSendWithoutContentType()
$this->assertArrayNotHasKey('Accept', $result['body']['headers']);
}

public function testSendWithContentTypeWithBody()
{
$result = MockHelper::newApiCall()
->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}'))
->parameters(BodyParam::init(123))
->parameters(HeaderParam::init('content-type', 'MyContentType')))
->responseHandler(MockHelper::responseHandler()
->type(MockClass::class))
->execute();
$this->assertInstanceOf(MockClass::class, $result);
$this->assertEquals('MyContentType', $result->body['headers']['content-type']);
$result = MockHelper::newApiCall()
->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}')))
->execute();
$this->assertArrayNotHasKey('Accept', $result['body']['headers']);
}

public function testSendDisableContentTypeWithBody()
{
$result = MockHelper::newApiCall()
->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}'))
->parameters(BodyParam::init(123))
->disableContentType())
->responseHandler(MockHelper::responseHandler()
->type(MockClass::class))
->execute();
$this->assertInstanceOf(MockClass::class, $result);
$this->assertArrayNotHasKey('content-type', $result->body['headers']);
$this->assertArrayNotHasKey('Accept', $result->body['headers']);

$result = MockHelper::newApiCall()
->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}')))
->execute();
$this->assertArrayNotHasKey('Accept', $result['body']['headers']);
}

public function testSendWithCustomContentType()
{
$result = MockHelper::newApiCall()
Expand All @@ -237,7 +273,6 @@ public function testSendNoParams()
$this->assertEquals(RequestMethod::POST, $result->body['httpMethod']);
$this->assertEquals('http://my/path:3000/v1/simple/{tyu}', $result->body['queryUrl']);
$this->assertEquals('application/json', $result->body['headers']['Accept']);
$this->assertEquals('text/plain; charset=utf-8', $result->body['headers']['content-type']);
$this->assertEquals('headVal1', $result->body['headers']['additionalHead1']);
$this->assertEquals('headVal2', $result->body['headers']['additionalHead2']);
$this->assertStringStartsWith('my lang|1.*.*|', $result->body['headers']['user-agent']);
Expand Down Expand Up @@ -670,8 +705,7 @@ public function testSendXMLNullBodyParam()
->type(MockClass::class))
->execute();
$this->assertInstanceOf(MockClass::class, $result);
$this->assertEquals(Format::XML, $result->body['headers']['content-type']);
$this->assertEquals('<?xml version="1.0"?>' . "\n", $result->body['body']);
$this->assertNull($result->body['body']);
}

public function testSendXMLArrayBodyParam()
Expand Down Expand Up @@ -781,10 +815,9 @@ public function testReceiveApiResponse()
$this->assertStringContainsString('{"body":{"httpMethod":"Post","queryUrl":"http:\/\/my\/path:3000\/v1' .
'\/simple\/{tyu}","headers":{"additionalHead1":"headVal1","additionalHead2":"headVal2","user-agent":' .
'"my lang|1.*.*|', $result->getBody());
$this->assertStringContainsString(',"content-type":"text\/plain; charset=utf-8","Accept":"application\/json"' .
$this->assertStringContainsString(',"Accept":"application\/json"' .
'},"parameters":[],"parametersEncoded":[],"parametersMultipart":[],"body":null,' .
'"retryOption":"useGlobalSettings"},"additionalProperties":[]}', $result->getBody());
$this->assertEquals(['content-type' => 'application/json'], $result->getHeaders());
$this->assertEquals(200, $result->getStatusCode());
$this->assertTrue($result->isSuccess());
$this->assertNull($result->getReasonPhrase());
Expand Down

0 comments on commit 2dcd333

Please sign in to comment.