Revision bbc9fae4
Added by koszko over 1 year ago
test/haketilo_test/unit/test_repo_query.py | ||
---|---|---|
29 | 29 |
queried_url = 'https://example_a.com/something' |
30 | 30 |
|
31 | 31 |
def setup_view(execute_in_page, repo_urls, tab={'id': 0}): |
32 |
mock_cacher(execute_in_page)
|
|
32 |
execute_in_page(mock_cacher_code)
|
|
33 | 33 |
|
34 | 34 |
execute_in_page(load_script('html/repo_query.js')) |
35 | 35 |
execute_in_page( |
... | ... | |
185 | 185 |
elif message == 'failure_to_communicate': |
186 | 186 |
setup_view(execute_in_page, repo_urls) |
187 | 187 |
execute_in_page( |
188 |
'browser.tabs.sendMessage = () => Promise.resolve({error: "sth"});' |
|
189 |
) |
|
188 |
''' |
|
189 |
window.mock_cacher_fetch = |
|
190 |
() => {throw new Error("Something happened :o")}; |
|
191 |
''') |
|
190 | 192 |
show_and_wait_for_repo_entry() |
191 | 193 |
|
192 | 194 |
elem = execute_in_page('returnval(view.repo_entries[0].info_div);') |
... | ... | |
196 | 198 |
setup_view(execute_in_page, repo_urls) |
197 | 199 |
execute_in_page( |
198 | 200 |
''' |
199 |
const response = {ok: false, status: 405};
|
|
200 |
browser.tabs.sendMessage = () => Promise.resolve(response);
|
|
201 |
const response = new Response("", {status: 405});
|
|
202 |
window.mock_cacher_fetch = () => Promise.resolve(response);
|
|
201 | 203 |
''') |
202 | 204 |
show_and_wait_for_repo_entry() |
203 | 205 |
|
... | ... | |
208 | 210 |
setup_view(execute_in_page, repo_urls) |
209 | 211 |
execute_in_page( |
210 | 212 |
''' |
211 |
const response = {ok: true, status: 200, error_json: "sth"};
|
|
212 |
browser.tabs.sendMessage = () => Promise.resolve(response);
|
|
213 |
const response = new Response("sth", {status: 200});
|
|
214 |
window.mock_cacher_fetch = () => Promise.resolve(response);
|
|
213 | 215 |
''') |
214 | 216 |
show_and_wait_for_repo_entry() |
215 | 217 |
|
... | ... | |
220 | 222 |
setup_view(execute_in_page, repo_urls) |
221 | 223 |
execute_in_page( |
222 | 224 |
''' |
223 |
const response = { |
|
224 |
ok: true, |
|
225 |
status: 200, |
|
226 |
json: {$schema: "https://hydrilla.koszko.org/schemas/api_query_result-255.2.1.schema.json"} |
|
227 |
}; |
|
228 |
browser.tabs.sendMessage = () => Promise.resolve(response); |
|
225 |
const newer_schema_url = |
|
226 |
"https://hydrilla.koszko.org/schemas/api_query_result-255.2.1.schema.json"; |
|
227 |
const mocked_json_data = JSON.stringify({$schema: newer_schema_url}); |
|
228 |
const response = new Response(mocked_json_data, {status: 200}); |
|
229 |
window.mock_cacher_fetch = () => Promise.resolve(response); |
|
229 | 230 |
''') |
230 | 231 |
show_and_wait_for_repo_entry() |
231 | 232 |
|
... | ... | |
236 | 237 |
setup_view(execute_in_page, repo_urls) |
237 | 238 |
execute_in_page( |
238 | 239 |
''' |
239 |
const response = { |
|
240 |
ok: true, |
|
241 |
status: 200, |
|
242 |
/* $schema is not a string as it should be. */ |
|
243 |
json: {$schema: null} |
|
244 |
}; |
|
245 |
browser.tabs.sendMessage = () => Promise.resolve(response); |
|
240 |
window.mock_cacher_fetch = async function(...args) { |
|
241 |
const response = await fetch(...args); |
|
242 |
const json = await response.json(); |
|
243 |
|
|
244 |
/* $schema is no longer a string as it should be. */ |
|
245 |
json.$schema = null; |
|
246 |
|
|
247 |
return new Response(JSON.stringify(json), { |
|
248 |
status: response.status, |
|
249 |
statusText: response.statusText, |
|
250 |
headers: [...response.headers.entries()] |
|
251 |
}); |
|
252 |
} |
|
246 | 253 |
''') |
247 | 254 |
show_and_wait_for_repo_entry() |
248 | 255 |
|
... | ... | |
252 | 259 |
elif message == 'querying_repo': |
253 | 260 |
setup_view(execute_in_page, repo_urls) |
254 | 261 |
execute_in_page( |
255 |
'browser.tabs.sendMessage = () => new Promise(() => {});'
|
|
262 |
'window.mock_cacher_fetch = () => new Promise(cb => {});'
|
|
256 | 263 |
) |
257 | 264 |
show_and_wait_for_repo_entry() |
258 | 265 |
|
... | ... | |
262 | 269 |
setup_view(execute_in_page, repo_urls) |
263 | 270 |
execute_in_page( |
264 | 271 |
''' |
265 |
const response = { |
|
266 |
ok: true, |
|
267 |
status: 200, |
|
268 |
json: { |
|
269 |
$schema: "https://hydrilla.koszko.org/schemas/api_query_result-1.schema.json", |
|
270 |
mappings: [] |
|
271 |
} |
|
272 |
}; |
|
273 |
browser.tabs.sendMessage = () => Promise.resolve(response); |
|
272 |
const schema_url = |
|
273 |
"https://hydrilla.koszko.org/schemas/api_query_result-1.schema.json"; |
|
274 |
const mocked_json_data = |
|
275 |
JSON.stringify({$schema: schema_url, mappings: []}); |
|
276 |
const response = new Response(mocked_json_data, {status: 200}); |
|
277 |
window.mock_cacher_fetch = () => Promise.resolve(response); |
|
274 | 278 |
''') |
275 | 279 |
show_and_wait_for_repo_entry() |
276 | 280 |
|
Also available in: Unified diff
serialize and deserialize entire Response object when relaying fetch() calls to other contexts using sendMessage