Repository navigation
Expand file tree
/
Copy pathFakePlatform.cpp
More file actions
223 lines (184 loc) · 5.5 KB
/
Copy pathFakePlatform.cpp
File metadata and controls
223 lines (184 loc) · 5.5 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
#include "FakePlatform.h"
#include <cctype>
#include <mutex>
#include <stdexcept>
#include <string>
#include <thread>
#include <utility>
#include <vector>
#include "ns/Platform.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;
}
bool StartsWith(const std::string& s, const char* prefix) {
return s.rfind(prefix, 0) == 0;
}
} // namespace
namespace fake_platform {
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 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