Skip to content

Commit 905660f

Browse files
committed
Implemented: Adds test for CURLOPT_READFUNCTION curl option setter.
1 parent 6c84a60 commit 905660f

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/VCR/Util/CurlHelper.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ public static function setCurlOptionOnRequest(Request $request, $option, $value,
164164
// Guzzle provides a callback to let curl read the body string.
165165
// To get the body, this callback is called manually.
166166
$bodySize = $request->getCurlOptions()->get(CURLOPT_INFILESIZE);
167+
Assertion::notEmpty($bodySize, "To set a CURLOPT_READFUNCTION, CURLOPT_INFILESIZE must be set.");
167168
$body = $value($curlHandle, fopen('php://memory', 'r'), $bodySize);
168169
$request->setBody($body);
169170
break;

tests/VCR/Util/CurlHelperTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,34 @@ public function testSetCurlOptionOnRequestSetMultipleHeadersTwice()
7373
);
7474
$this->assertEquals($expected, $request->getHeaders());
7575
}
76+
77+
public function testSetCurlOptionReadFunctionMissingSize()
78+
{
79+
$this->setExpectedException('\VCR\VCRException', 'To set a CURLOPT_READFUNCTION, CURLOPT_INFILESIZE must be set.');
80+
$request = new Request('POST', 'example.com');
81+
82+
CurlHelper::setCurlOptionOnRequest($request, CURLOPT_READFUNCTION, null, curl_init());
83+
84+
$this->assertEquals($expected, $request->getBody());
85+
}
86+
87+
public function testSetCurlOptionReadFunction()
88+
{
89+
$expected = 'test body';
90+
$request = new Request('POST', 'example.com');
91+
92+
$test = $this;
93+
$callback = function ($curlHandle, $fileHandle, $size) use ($test, $expected) {
94+
$test->assertInternalType('resource', $curlHandle);
95+
$test->assertInternalType('resource', $fileHandle);
96+
$test->assertEquals(strlen($expected), $size);
97+
98+
return $expected;
99+
};
100+
101+
CurlHelper::setCurlOptionOnRequest($request, CURLOPT_INFILESIZE, strlen($expected));
102+
CurlHelper::setCurlOptionOnRequest($request, CURLOPT_READFUNCTION, $callback, curl_init());
103+
104+
$this->assertEquals($expected, $request->getBody());
105+
}
76106
}

0 commit comments

Comments
 (0)