-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathCurlFetchWebdataTest.php
117 lines (103 loc) · 2.46 KB
/
CurlFetchWebdataTest.php
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
<?php
/**
* TestCase class for \ElkArte\Http\CurlFetchWebdata
*/
namespace ElkArte\Http;
use PHPUnit\Framework\TestCase;
class CurlFetchWebdataTest extends TestCase
{
protected $curl_fetch_testcases = [];
protected $curl_post_testcases = [];
protected $backupGlobalsExcludeList = ['user_info'];
/**
* Prepare what is necessary to use in these tests.
*
* setUp() is run automatically by the testing framework before each test method.
*/
protected function setUp(): void
{
// url
// post data
// expected return code
// expected in output
$this->curl_post_testcases = array(
//array(
// 'https://www.google.com',
// array('gs_taif0' => 'elkarte'),
// 404,
// 'all we know',
//),
array(
'https://duckduckgo.com/html',
array('q' => 'stargate+sg1 site:www.imdb.com', 'b' => ''),
[200, 403],
'TV Series',
),
);
// url
// expected return code
// expected in output
$this->curl_fetch_testcases = array(
array(
'https://developer.mozilla.org/en-US/',
200,
'Resources for <u>Developers</u>',
),
array(
'http://www.google.com/elkarte',
404,
),
);
}
/**
* cleanup data we no longer need at the end of the tests in this class.
* tearDown() is run automatically by the testing framework after each test method.
*/
protected function tearDown(): void
{
}
/**
* Test curl fetching
*/
public function testCurlFetch()
{
// Start curl, pass some default values for a test
$curl = new CurlFetchWebdata(array(CURLOPT_RETURNTRANSFER => 1), 1);
foreach ($this->curl_fetch_testcases as $testcase)
{
// Fetch a page
$curl->get_url_data($testcase[0]);
// Check for correct results
if (!empty($testcase[1]))
{
$this->assertEquals($testcase[1], $curl->result('code'));
}
if (!empty($testcase[2]))
{
$this->assertStringContainsString($testcase[2], $curl->result('body'));
}
}
}
/**
* Test curl with posting
*/
public function testCurlPost()
{
// Start curl, pass some default values for a test
$curl = new CurlFetchWebdata();
foreach ($this->curl_post_testcases as $testcase)
{
// Post to a page
$curl->get_url_data($testcase[0], $testcase[1]);
// Check for correct fetch
if (!empty($testcase[2]))
{
$this->assertContains($curl->result('code'), $testcase[2]);
}
if (!empty($testcase[3]) && $curl->result('code') == 200)
{
$this->assertStringContainsString($testcase[3], $curl->result('body'));
}
}
}
}