-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathResponseSimulator.php
More file actions
132 lines (111 loc) · 3.14 KB
/
Copy pathResponseSimulator.php
File metadata and controls
132 lines (111 loc) · 3.14 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
<?php
namespace GT\Fetch\Test\Helper;
use GT\Curl\CurlInterface;
class ResponseSimulator {
const RANDOM_BODY_WORDS = ["pursuit","forest","gravel","timber","wonder","eject","slogan","monkey","construct","earthquake","respect","publish","forward","circle","summer","define","highlight","refuse","salon","theater","lily","earwax","variant","account","resource"];
static protected $headerCallback;
static protected $bodyCallback;
static protected $headerBuffer;
static protected $bodyBuffer;
static protected $started = false;
/**
* @var true
*/
private static bool $redirectAdded = false;
static public function setHeaderCallback(callable $callback) {
self::$headerCallback = $callback;
}
static public function setBodyCallback(callable $callback) {
self::$bodyCallback = $callback;
}
static public function start() {
self::$started = true;
self::$redirectAdded = false;
self::$headerBuffer = self::generateHeaders();
self::$bodyBuffer = self::generateBody();
}
static protected function generateHeaders():array {
$headers = [];
$headers []= "HTTP/0.0 999 OK";
$headers []= "Date: " . date("D, d M Y H:i:s T");
$headers []= "Repository: PhpGt/Fetch";
$length = rand(1, 10);
for($i = 0; $i < $length; $i++) {
$randIndex = array_rand(self::RANDOM_BODY_WORDS);
$key = self::RANDOM_BODY_WORDS[$randIndex];
$value = uniqid();
$headers []= "$key: $value";
}
foreach($headers as $i => $h) {
$headers[$i] .= "\r\n";
}
$headers []= "\r\n";
return $headers;
}
static protected function generateBody():string {
if(strlen(self::$bodyBuffer ?? "") > 0) {
return self::$bodyBuffer;
}
$body = "";
$length = rand(10, 100);
for($i = 0; $i < $length; $i++) {
$randIndex = array_rand(self::RANDOM_BODY_WORDS);
$body .= self::RANDOM_BODY_WORDS[$randIndex];
$body .= " ";
}
return $body;
}
static public function hasStarted():bool {
return self::$started;
}
static public function sendChunk(CurlInterface $ch):int {
$url = $ch->getInfo(CURLINFO_EFFECTIVE_URL);
if($url === "test://should-follow-redirect" && !self::$redirectAdded) {
self::$headerBuffer = [
"HTTP/1.1 303 See Other\r\n",
"Content-Type: text/html\r\n",
"Location: /redirected\r\n",
"\r\n",
"HTTP/1.1 200 OK\r\n",
"Content-Type: application/json\r\n",
"X-Final-Response: true\r\n",
"\r\n",
];
self::$bodyBuffer = "{}";
self::$redirectAdded = true;
}
if($url === "test://should-redirect") {
$locationRedirect = "Location: /redirected\r\n";
if(!self::$redirectAdded) {
array_splice(self::$headerBuffer, 1, 0, $locationRedirect);
self::$redirectAdded = true;
}
}
if(!empty(self::$headerBuffer)) {
$data = array_shift(self::$headerBuffer);
call_user_func(
self::$headerCallback,
$ch->getHandle(),
$data
);
return 1;
}
elseif(!empty(self::$bodyBuffer)) {
$data = self::$bodyBuffer;
self::$bodyBuffer = "";
call_user_func(
self::$bodyCallback,
$ch->getHandle(),
$data
);
return 1;
}
else {
self::$started = false;
return 0;
}
}
static public function setExpectedBody(string $body) {
self::$bodyBuffer = $body;
}
}