Repository navigation
Expand file tree
/
Copy pathFakePlatform.cpp
More file actions
324 lines (268 loc) · 8 KB
/
Copy pathFakePlatform.cpp
File metadata and controls
324 lines (268 loc) · 8 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include "FakePlatform.h"
#include <cctype>
#include <memory>
#include <mutex>
#include <stdexcept>
#include <string>
#include <thread>
#include <unordered_map>
#include <utility>
#include <vector>
#include "ns/AppFiles.h"
#include "ns/LoaderConfig.h"
#include "ns/Platform.h"
#include "ns/Thread.h"
#include "ns/http/ModuleTransport.h"
namespace {
std::mutex& Mutex() {
static std::mutex* mutex = new std::mutex();
return *mutex;
}
std::vector<std::string>& Logged() {
static auto* messages = new std::vector<std::string>();
return *messages;
}
std::vector<std::string>& Traced() {
static auto* messages = new std::vector<std::string>();
return *messages;
}
bool traceEnabled = true;
nativescript::common::http::HttpLoaderConfig& Config() {
static auto* config = new nativescript::common::http::HttpLoaderConfig();
return *config;
}
struct FakeFile {
std::string contents;
std::string freshnessToken;
bool readable = true;
};
std::unordered_map<std::string, FakeFile>& Files() {
static auto* files = new std::unordered_map<std::string, FakeFile>();
return *files;
}
nativescript::common::LoaderConfig& LoaderConfigStorage() {
static auto* config = new nativescript::common::LoaderConfig();
return *config;
}
std::function<void()>& RunJobHook() {
static auto* hook = new std::function<void()>();
return *hook;
}
bool StartsWith(const std::string& s, const char* prefix) {
return s.rfind(prefix, 0) == 0;
}
} // namespace
namespace fake_platform {
std::atomic<int> threadPriorityCalls{0};
std::atomic<int> runJobCalls{0};
std::atomic<int> statCalls{0};
nativescript::common::LoaderConfig& MutableLoaderConfig() {
return LoaderConfigStorage();
}
void SetFile(const std::string& path, std::string contents,
std::string freshnessToken, bool readable) {
std::lock_guard<std::mutex> lock(Mutex());
Files()[path] =
FakeFile{std::move(contents), std::move(freshnessToken), readable};
}
void RemoveFile(const std::string& path) {
std::lock_guard<std::mutex> lock(Mutex());
Files().erase(path);
}
void ClearFiles() {
std::lock_guard<std::mutex> lock(Mutex());
Files().clear();
}
std::vector<std::string> LoggedMessages() {
std::lock_guard<std::mutex> lock(Mutex());
return Logged();
}
std::vector<std::string> TracedMessages() {
std::lock_guard<std::mutex> lock(Mutex());
return Traced();
}
void ClearMessages() {
std::lock_guard<std::mutex> lock(Mutex());
Logged().clear();
Traced().clear();
}
void SetTraceEnabled(bool enabled) {
std::lock_guard<std::mutex> lock(Mutex());
traceEnabled = enabled;
}
void SetRunJobHook(std::function<void()> hook) {
std::lock_guard<std::mutex> lock(Mutex());
RunJobHook() = std::move(hook);
}
void SetConfig(const http::HttpLoaderConfig& config) {
std::lock_guard<std::mutex> lock(Mutex());
Config() = config;
}
http::AttemptOutcome Ok(const std::string& url, const std::string& body) {
http::AttemptOutcome outcome;
outcome.arrived = true;
outcome.status = 200;
outcome.contentType = "text/javascript";
outcome.body = body;
outcome.finalUrl = url;
return outcome;
}
http::AttemptOutcome FakeTransport::Respond(
const http::AttemptRequest& request) {
Responder responder;
{
std::lock_guard<std::mutex> lock(mutex_);
requests_.push_back(request);
responder = responder_;
}
return responder(request);
}
http::AttemptOutcome FakeTransport::FetchBlocking(
const http::AttemptRequest& request) {
return Respond(request);
}
void FakeTransport::FetchAsync(http::AttemptRequest request,
std::function<void(http::AttemptOutcome)> done) {
if (throwFromFetchAsync) {
throw std::runtime_error("refused");
}
std::thread([this, request = std::move(request), done = std::move(done)] {
done(Respond(request));
}).detach();
}
http::CacheOpResult FakeTransport::ClearAll() {
clearAllCalls++;
if (!clearResult.ok && clearResult.error == "throw") {
throw std::runtime_error("disk full");
}
return clearResult;
}
http::CacheOpResult FakeTransport::ClearOrigin(const std::string& origin) {
std::lock_guard<std::mutex> lock(mutex_);
clearedOrigins_.push_back(origin);
return http::CacheOpResult{};
}
void FakeTransport::Reset() {
std::lock_guard<std::mutex> lock(mutex_);
requests_.clear();
clearedOrigins_.clear();
responder_ = [](const http::AttemptRequest& request) {
return Ok(request.url, "export {}");
};
schedulesItself = false;
throwFromFetchAsync = false;
clearAllCalls = 0;
postDelayedCalls = 0;
clearResult = http::CacheOpResult{};
}
std::vector<http::AttemptRequest> FakeTransport::Requests() {
std::lock_guard<std::mutex> lock(mutex_);
return requests_;
}
std::vector<std::string> FakeTransport::ClearedOrigins() {
std::lock_guard<std::mutex> lock(mutex_);
return clearedOrigins_;
}
void FakeTransport::SetResponder(Responder responder) {
std::lock_guard<std::mutex> lock(mutex_);
responder_ = std::move(responder);
}
FakeTransport& Transport() {
static auto* transport = [] {
auto* t = new FakeTransport();
t->Reset();
return t;
}();
return *transport;
}
} // namespace fake_platform
namespace nativescript::common::platform {
void Log(std::string_view message) {
std::lock_guard<std::mutex> lock(Mutex());
Logged().emplace_back(message);
}
bool TraceEnabled(TraceCategory) {
std::lock_guard<std::mutex> lock(Mutex());
return traceEnabled;
}
void Trace(TraceCategory, std::string_view message) {
std::lock_guard<std::mutex> lock(Mutex());
Traced().emplace_back(message);
}
// Enough of WHATWG URL parsing for the tests: http(s) URLs pass through with
// the scheme and host lowercased, anything else is not a URL.
std::string NormalizeHttpModuleUrl(const std::string& input) {
std::string url = input;
const size_t schemeEnd = url.find("://");
if (schemeEnd == std::string::npos) {
return "";
}
const size_t hostEnd = url.find_first_of("/?#", schemeEnd + 3);
const size_t lowerEnd = hostEnd == std::string::npos ? url.size() : hostEnd;
for (size_t i = 0; i < lowerEnd; i++) {
url[i] =
static_cast<char>(std::tolower(static_cast<unsigned char>(url[i])));
}
if (!StartsWith(url, "http://") && !StartsWith(url, "https://")) {
return "";
}
if (hostEnd == std::string::npos) {
url += "/";
}
return url;
}
std::string HttpUrlOrigin(const std::string& url) {
const std::string normalized = NormalizeHttpModuleUrl(url);
if (normalized.empty()) {
return "";
}
const size_t schemeEnd = normalized.find("://");
const size_t hostEnd = normalized.find_first_of("/?#", schemeEnd + 3);
return normalized.substr(0, hostEnd);
}
} // namespace nativescript::common::platform
namespace nativescript::common::platform {
http::HttpLoaderConfig ReadHttpLoaderConfig() {
std::lock_guard<std::mutex> lock(Mutex());
return Config();
}
http::ModuleTransport& HttpTransport() { return fake_platform::Transport(); }
} // namespace nativescript::common::platform
namespace nativescript::common::platform {
std::shared_ptr<const LoaderConfig> LoaderSnapshot() {
return std::make_shared<const LoaderConfig>(LoaderConfigStorage());
}
std::optional<FileInfo> Stat(std::string_view logicalPath) {
fake_platform::statCalls++;
std::lock_guard<std::mutex> lock(Mutex());
auto it = Files().find(std::string(logicalPath));
if (it == Files().end()) {
return std::nullopt;
}
return FileInfo{true, it->second.contents.size(), it->second.freshnessToken};
}
bool ReadFile(std::string_view logicalPath, std::string& out) {
std::lock_guard<std::mutex> lock(Mutex());
auto it = Files().find(std::string(logicalPath));
if (it == Files().end() || !it->second.readable) {
return false;
}
out = it->second.contents;
return true;
}
void ApplyThreadPriority(ThreadPriority) {
fake_platform::threadPriorityCalls++;
}
void RunJob(const std::function<void()>& job) {
fake_platform::runJobCalls++;
std::function<void()> hook;
{
std::lock_guard<std::mutex> lock(Mutex());
hook = RunJobHook();
}
if (hook) {
hook();
}
job();
}
} // namespace nativescript::common::platform