Project

General

Profile

« Previous | Next » 

Revision 13a707c6

Added by koszko over 1 year ago

serialize and deserialize entire Response object when relaying fetch() calls to other contexts using sendMessage

View differences:

test/haketilo_test/unit/test_install.py
26 26
from .utils import *
27 27

  
28 28
def setup_view(driver, execute_in_page):
29
    mock_cacher(execute_in_page)
29
    execute_in_page(mock_cacher_code)
30 30

  
31 31
    execute_in_page(load_script('html/install.js'))
32 32
    container_ids, containers_objects = execute_in_page(
......
203 203
    'indexeddb_error_file_uses',
204 204
    'failure_to_communicate_fetch',
205 205
    'HTTP_code_file',
206
    'not_valid_text',
207 206
    'sha256_mismatch',
208 207
    'indexeddb_error_write'
209 208
])
......
243 242
    if message == 'fetching_data':
244 243
        execute_in_page(
245 244
            '''
246
            browser.tabs.sendMessage = () => new Promise(cb => {});
245
            window.mock_cacher_fetch = () => new Promise(cb => {});
247 246
            install_view.show(...arguments);
248 247
            ''',
249 248
            'https://hydril.la/', 'mapping', 'mapping-a')
......
253 252
    elif message == 'failure_to_communicate_sendmessage':
254 253
        execute_in_page(
255 254
            '''
256
            browser.tabs.sendMessage = () => Promise.resolve({error: "sth"});
255
            window.mock_cacher_fetch =
256
                () => {throw new Error("Something happened :o")};
257 257
            install_view.show(...arguments);
258 258
            ''',
259 259
            'https://hydril.la/', 'mapping', 'mapping-a')
......
262 262
    elif message == 'HTTP_code_item':
263 263
        execute_in_page(
264 264
            '''
265
            const response = {ok: false, status: 404};
266
            browser.tabs.sendMessage = () => Promise.resolve(response);
265
            const response = new Response("", {status: 404});
266
            window.mock_cacher_fetch = () => Promise.resolve(response);
267 267
            install_view.show(...arguments);
268 268
            ''',
269 269
            'https://hydril.la/', 'mapping', 'mapping-a')
......
272 272
    elif message == 'invalid_JSON':
273 273
        execute_in_page(
274 274
            '''
275
            const response = {ok: true, status: 200, error_json: "sth"};
276
            browser.tabs.sendMessage = () => Promise.resolve(response);
275
            const response = new Response("sth", {status: 200});
276
            window.mock_cacher_fetch = () => Promise.resolve(response);
277 277
            install_view.show(...arguments);
278 278
            ''',
279 279
            'https://hydril.la/', 'mapping', 'mapping-a')
......
282 282
    elif message == 'newer_API_version':
283 283
        execute_in_page(
284 284
            '''
285
            const old_sendMessage = browser.tabs.sendMessage;
286
            browser.tabs.sendMessage = async function(...args) {
287
                const response = await old_sendMessage(...args);
288
                response.json.$schema = "https://hydrilla.koszko.org/schemas/api_mapping_description-255.1.schema.json";
289
                return response;
290
            }
285
            const newer_schema_url =
286
                "https://hydrilla.koszko.org/schemas/api_mapping_description-255.1.schema.json";
287
            const mocked_json_data = JSON.stringify({$schema: newer_schema_url});
288
            const response = new Response(mocked_json_data, {status: 200});
289
            window.mock_cacher_fetch = () => Promise.resolve(response);
291 290
            install_view.show(...arguments);
292 291
            ''',
293 292
            'https://hydril.la/', 'mapping', 'mapping-a', [2022, 5, 10])
......
297 296
    elif message == 'invalid_response_format':
298 297
        execute_in_page(
299 298
            '''
300
            const old_sendMessage = browser.tabs.sendMessage;
301
            browser.tabs.sendMessage = async function(...args) {
302
                const response = await old_sendMessage(...args);
303
                /* identifier is not a string as it should be. */
304
                response.json.identifier = 1234567;
305
                return response;
299
            window.mock_cacher_fetch = async function(...args) {
300
                const response = await fetch(...args);
301
                const json = await response.json();
302

  
303
                /* identifier is no longer a string as it should be. */
304
                json.identifier = 1234567;
305

  
306
                return new Response(JSON.stringify(json), {
307
                    status:     response.status,
308
                    statusText: response.statusText,
309
                    headers:    [...response.headers.entries()]
310
                });
306 311
            }
307 312
            install_view.show(...arguments);
308 313
            ''',
......
352 357
    elif message == 'failure_to_communicate_fetch':
353 358
        execute_in_page(
354 359
            '''
355
            fetch = () => {throw "some error";};
360
            fetch = () => {throw new Error("some error");};
356 361
            returnval(install_view.show(...arguments));
357 362
            ''',
358 363
            'https://hydril.la/', 'mapping', 'mapping-b')
......
372 377
        execute_in_page('returnval(install_view.install_but);').click()
373 378

  
374 379
        assert_dlg(['conf_buts'], 'Repository sent HTTP code 400 :(')
375
    elif message == 'not_valid_text':
376
        execute_in_page(
377
            '''
378
            const err = () => {throw "some error";};
379
            fetch = () => Promise.resolve({ok: true, status: 200, text: err});
380
            returnval(install_view.show(...arguments));
381
            ''',
382
            'https://hydril.la/', 'mapping', 'mapping-b')
383

  
384
        execute_in_page('returnval(install_view.install_but);').click()
385

  
386
        assert_dlg(['conf_buts'], "Repository's response is not valid text :(")
387 380
    elif message == 'sha256_mismatch':
388 381
        execute_in_page(
389 382
            '''

Also available in: Unified diff