-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathMockedHttpClient.php
More file actions
119 lines (102 loc) · 4.3 KB
/
MockedHttpClient.php
File metadata and controls
119 lines (102 loc) · 4.3 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
<?php
namespace PostHog\Test;
use Closure;
use PostHog\HttpResponse;
use PostHog\Test\Assets\MockedResponses;
class MockedHttpClient extends \PostHog\HttpClient
{
public $calls;
private $flagEndpointResponse;
private $flagsEndpointResponse;
private $flagEndpointEtag;
private $flagEndpointResponseCode;
/** @var array|null Queue of responses for sequential calls */
private $flagEndpointResponseQueue;
/** @var int Response code for /flags/ endpoint (for error simulation) */
private $flagsEndpointResponseCode;
/** @var int Curl error number for /flags/ endpoint (for error simulation) */
private $flagsEndpointCurlErrno;
public function __construct(
string $host,
bool $useSsl = true,
int $maximumBackoffDuration = 10000,
bool $compressRequests = false,
bool $debug = false,
?Closure $errorHandler = null,
int $curlTimeoutMilliseconds = 750,
array $flagEndpointResponse = [],
array $flagsEndpointResponse = [],
?string $flagEndpointEtag = null,
int $flagEndpointResponseCode = 200,
int $flagsEndpointResponseCode = 200,
int $flagsEndpointCurlErrno = 0
) {
parent::__construct(
$host,
$useSsl,
$maximumBackoffDuration,
$compressRequests,
$debug,
$errorHandler,
$curlTimeoutMilliseconds
);
$this->flagEndpointResponse = $flagEndpointResponse;
$this->flagsEndpointResponse = !empty($flagsEndpointResponse) ? $flagsEndpointResponse : MockedResponses::FLAGS_REQUEST;
$this->flagEndpointEtag = $flagEndpointEtag;
$this->flagEndpointResponseCode = $flagEndpointResponseCode;
$this->flagEndpointResponseQueue = null;
$this->flagsEndpointResponseCode = $flagsEndpointResponseCode;
$this->flagsEndpointCurlErrno = $flagsEndpointCurlErrno;
}
/**
* Set a queue of responses for the local_evaluation endpoint
* Each call will consume the next response in the queue
*
* @param array $responses Array of ['response' => array, 'etag' => string|null, 'responseCode' => int]
*/
public function setFlagEndpointResponseQueue(array $responses): void
{
$this->flagEndpointResponseQueue = $responses;
}
public function sendRequest(string $path, ?string $payload, array $extraHeaders = [], array $requestOptions = []): HttpResponse
{
if (!isset($this->calls)) {
$this->calls = [];
}
array_push($this->calls, array("path" => $path, "payload" => $payload, "extraHeaders" => $extraHeaders, "requestOptions" => $requestOptions));
// Local evaluation endpoint: /flags/definitions?...
if (str_starts_with($path, "/flags/definitions")) {
// Check if we have a response queue
if ($this->flagEndpointResponseQueue !== null && !empty($this->flagEndpointResponseQueue)) {
$nextResponse = array_shift($this->flagEndpointResponseQueue);
$response = $nextResponse['response'] ?? [];
$etag = $nextResponse['etag'] ?? null;
$responseCode = $nextResponse['responseCode'] ?? 200;
// Handle 304 Not Modified - return empty body
if ($responseCode === 304) {
return new HttpResponse('', $responseCode, $etag);
}
return new HttpResponse(json_encode($response), $responseCode, $etag);
}
// Handle 304 Not Modified - return empty body
if ($this->flagEndpointResponseCode === 304) {
return new HttpResponse('', 304, $this->flagEndpointEtag);
}
return new HttpResponse(
json_encode($this->flagEndpointResponse),
$this->flagEndpointResponseCode,
$this->flagEndpointEtag
);
}
// Decide endpoint: /flags/?v=2
if (str_starts_with($path, "/flags/?")) {
return new HttpResponse(
json_encode($this->flagsEndpointResponse),
$this->flagsEndpointResponseCode,
null,
$this->flagsEndpointCurlErrno
);
}
return parent::sendRequest($path, $payload, $extraHeaders, $requestOptions);
}
}