-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathFsockFetchWebdataTest.php
133 lines (118 loc) · 3.04 KB
/
FsockFetchWebdataTest.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
namespace ElkArte\Http;
use PHPUnit\Framework\TestCase;
class FsockFetchWebdataTest extends TestCase
{
protected $fetch_testcases = [];
protected $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->post_testcases = array(
//array(
// 'https://www.google.com',
// array('gs_taif0' => 'elkarte'),
// 405,
// 'all we know',
//),
array(
'https://www.elkarte.net/community/index.php?action=search;sa=results',
array('search' => 'stuff', 'search_selection' => 'all', 'advanced' => 0),
[200, 403],
'let you access this section',
),
);
// url
// expected return code
// expected in output
// redirects
$this->fetch_testcases = array(
array(
'https://developer.mozilla.org/en-US/',
200,
'Resources for <u>Developers</u>',
),
array(
'http://www.google.com/elkarte',
404,
),
array(
'http://elkarte.github.io/addons/',
200,
'Addons to extend',
1
),
);
}
/**
* 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 Fsockopen fetching
*/
public function testFsockFetch()
{
// Start Fsockopen, pass some default values for a test
$fsock = new FsockFetchWebdata(array(), 3);
foreach ($this->fetch_testcases as $testcase)
{
// Fetch a page
$fsock->get_url_data($testcase[0]);
// Check for correct results
if (!empty($testcase[1]))
{
$this->assertEquals($testcase[1], $fsock->result('code'), 'FetchCodeError:: ' . $testcase[0]);
}
if (!empty($testcase[2]))
{
$this->assertStringContainsString($testcase[2], $fsock->result('body'), 'FetchBodyError:: ' . $testcase[0]);
}
if (!empty($testcase[3]))
{
$this->assertEquals($testcase[3], $fsock->result('redirects'), 'FetchRedirectError:: ' . $testcase[0]);
}
}
}
/**
* Test Fsockopen with posting data
*/
public function testFsockPost()
{
// Start fsock, pass some default values for a test
$fsock = new FsockFetchWebdata(array(), 3);
foreach ($this->post_testcases as $testcase)
{
$fsock->get_url_data($testcase[0], $testcase[1]);
// Temporary, the SSL Cert failed to renew on ElkArte
if ($fsock->result('code') === 405)
{
$this->assertEquals(405, $fsock->result('code'), 'PostCodeError:: ' . $testcase[0]);
}
else
{
// Check for correct fetch
if (!empty($testcase[2]))
{
$this->assertContains($fsock->result('code'), $testcase[2], 'PostCodeError:: ' . $testcase[0]);
}
if (!empty($testcase[3]) && $fsock->result('code') == 200)
{
$this->assertStringContainsString($testcase[3], $fsock->result('body'), 'PostBodyError:: ' . $testcase[0]);
}
}
}
}
}