cURLTest.php
3.98 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
* @group server
*/
class cURLTest extends PHPUnit_Framework_TestCase
{
const URL = 'http://localhost:8080';
public function setUp()
{
if (!getenv('CURL_TEST_SERVER_RUNNING')) {
$this->markTestSkipped('The web server is not running.');
}
if (!extension_loaded('curl')) {
$this->markTestSkipped('The curl extension is not installed.');
}
}
private function makeCurl()
{
return new anlutro\cURL\cURL;
}
/** @test */
public function successfulResponse()
{
$r = $this->makeCurl()->get(static::URL.'/success.php');
$this->assertEquals(200, $r->statusCode);
$this->assertEquals('200 OK', $r->statusText);
$this->assertEquals('OK', $r->body);
$this->assertNotNull($r->headers);
$this->assertNotNull($r->info);
}
/** @test */
public function failedResponse()
{
$r = $this->makeCurl()->get(static::URL.'/failure.php');
$this->assertEquals(500, $r->statusCode);
$this->assertEquals('500 Internal Server Error', $r->statusText);
$this->assertEquals('Failure', $r->body);
$this->assertNotNull($r->headers);
$this->assertNotNull($r->info);
}
/** @test */
public function queryRequestBody()
{
$r = $this->makeCurl()->post(static::URL.'/echo.php', array('foo' => 'bar'));
$this->assertEquals('foo=bar', $r->body);
}
/** @test */
public function queryRequestEmptyArrayBody()
{
$r = $this->makeCurl()->post(static::URL.'/echo.php', array());
$this->assertEquals('', $r->body);
}
/** @test */
public function queryRequestEmptyObjectBody()
{
$r = $this->makeCurl()->post(static::URL.'/echo.php', new \stdClass());
$this->assertEquals('', $r->body);
}
/** @test */
public function jsonRequestBody()
{
$r = $this->makeCurl()->jsonPost(static::URL.'/echo.php', array('foo' => 'bar'));
$this->assertEquals('{"foo":"bar"}', $r->body);
}
/** @test */
public function jsonRequestEmptyArrayBody()
{
$r = $this->makeCurl()->jsonPost(static::URL.'/echo.php', array());
$this->assertEquals('[]', $r->body);
}
/** @test */
public function jsonRequestEmptyObjectBody()
{
$r = $this->makeCurl()->jsonPost(static::URL.'/echo.php', new \stdClass());
$this->assertEquals('{}', $r->body);
}
/** @test */
public function rawRequestBody()
{
$r = $this->makeCurl()->rawPost(static::URL.'/echo.php', '<foo/>');
$this->assertEquals('<foo/>', $r->body);
}
/** @test */
public function rawRequestEmptyBody()
{
$r = $this->makeCurl()->rawPost(static::URL.'/echo.php', '');
$this->assertEquals('', $r->body);
}
/** @test */
public function fileUpload()
{
$file = __FILE__;
if (function_exists('curl_file_create')) {
$data = array('file' => curl_file_create($file));
} else {
$data = array('file' => '@'.$file);
}
$r = $this->makeCurl()->rawPost(static::URL.'/upload.php', $data);
$this->assertEquals(basename($file)."\t".filesize($file)."\n", $r->body);
}
/** @test */
public function throwsExceptionOnCurlError()
{
$this->setExpectedException('anlutro\cURL\cURLException', 'cURL request failed with error [7]:');
$this->makeCurl()->get('http://0.0.0.0');
}
/** @test */
public function throwsExceptionWithMissingUrl()
{
$this->setExpectedException('BadMethodCallException', 'Missing argument 1 ($url) for anlutro\cURL\cURL::get');
$this->makeCurl()->get();
}
/** @test */
public function throwsExceptionWhenDataProvidedButNotAllowed()
{
$this->setExpectedException('InvalidArgumentException', 'HTTP method [options] does not allow POST data.');
$this->makeCurl()->options('http://localhost', array('foo' => 'bar'));
}
/** @test */
public function defaultHeadersAreAdded()
{
$curl = $this->makeCurl();
$curl->setDefaultHeaders(array('foo' => 'bar'));
$request = $curl->newRequest('post', 'localhost');
$this->assertEquals('bar', $request->getHeader('foo'));
}
/** @test */
public function defaultOptionsAreAdded()
{
$curl = $this->makeCurl();
$curl->setDefaultOptions(array('foo' => 'bar'));
$request = $curl->newRequest('post', 'localhost');
$this->assertEquals('bar', $request->getOption('foo'));
}
}