Does testcafe support testing of rest api

后端 未结 1 1517
囚心锁ツ
囚心锁ツ 2021-01-07 03:50

Tests hang in the testcafe browser when you try to test a rest api url directly.

I am trying to run a test against my rest API endpoint using request hooks, but when

相关标签:
1条回答
  • 2021-01-07 04:20

    You are right, TestCafe is intended to work with html pages, but it will use the "about:blank" page if you don't define any url. You can use the regular node.js HTTP API without request hooks for this case. Look at the following example:

    import http from 'http';
    
    const getResponseData = (url) => new Promise((resolve, reject) => {
        http.get(url, res => {
            const { statusCode } = res;
            const contentType = res.headers['content-type'];
    
            res.setEncoding('utf8');
            let rawData = '';
            res.on('data', (chunk) => { rawData += chunk; });
            res.on('end', () => resolve({ statusCode, contentType, rawData }));
        }).on('error', e => reject(e));
    });
    
    fixture `Test REST API`;
    
    test('Simple Test', async t => {
        const response = await getResponseData('http://example.com/api/1');
        await t
            .expect(response.statusCode).eql(200);
    });
    
    0 讨论(0)
提交回复
热议问题