1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89:
<?php
namespace Guzzle\Tests\Http\Message;
use Guzzle\Http\Client;
use Guzzle\Http\Message\PostFile;
class PostFileTest extends \Guzzle\Tests\GuzzleTestCase
{
public function testConstructorConfiguresPostFile()
{
$file = new PostFile('foo', __FILE__, 'x-foo', 'boo');
$this->assertEquals('foo', $file->getFieldName());
$this->assertEquals(__FILE__, $file->getFilename());
$this->assertEquals('boo', $file->getPostName());
$this->assertEquals('x-foo', $file->getContentType());
}
public function testRemovesLeadingAtSymbolFromPath()
{
$file = new PostFile('foo', '@' . __FILE__);
$this->assertEquals(__FILE__, $file->getFilename());
}
public function testEnsuresFileIsReadable()
{
$file = new PostFile('foo', '/foo/baz/bar');
}
public function testCanChangeContentType()
{
$file = new PostFile('foo', '@' . __FILE__);
$file->setContentType('Boo');
$this->assertEquals('Boo', $file->getContentType());
}
public function testCanChangeFieldName()
{
$file = new PostFile('foo', '@' . __FILE__);
$file->setFieldName('Boo');
$this->assertEquals('Boo', $file->getFieldName());
}
public function testReturnsCurlValueString()
{
$file = new PostFile('foo', __FILE__);
if (version_compare(phpversion(), '5.5.0', '<')) {
$this->assertContains('@' . __FILE__ . ';filename=PostFileTest.php;type=text/x-', $file->getCurlValue());
} else {
$c = $file->getCurlValue();
$this->assertEquals(__FILE__, $c->getFilename());
$this->assertEquals('PostFileTest.php', $c->getPostFilename());
$this->assertContains('text/x-', $c->getMimeType());
}
}
public function testReturnsCurlValueStringAndPostname()
{
$file = new PostFile('foo', __FILE__, null, 'NewPostFileTest.php');
if (version_compare(phpversion(), '5.5.0', '<')) {
$this->assertContains('@' . __FILE__ . ';filename=NewPostFileTest.php;type=text/x-', $file->getCurlValue());
} else {
$c = $file->getCurlValue();
$this->assertEquals(__FILE__, $c->getFilename());
$this->assertEquals('NewPostFileTest.php', $c->getPostFilename());
$this->assertContains('text/x-', $c->getMimeType());
}
}
public function testContentDispositionFilePathIsStripped()
{
$this->getServer()->flush();
$client = new Client($this->getServer()->getUrl());
$this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
$request = $client->post()->addPostFile('file', __FILE__);
$request->send();
$requests = $this->getServer()->getReceivedRequests(false);
$this->assertContains('POST / HTTP/1.1', $requests[0]);
$this->assertContains('Content-Disposition: form-data; name="file"; filename="PostFileTest.php"', $requests[0]);
}
}