Multipart/form-data test

Hey guys!

I need to test an upload video function, and send multipart/form-data request to graphql. The function works, and I’m using in my application, but i can’t test because I don’t know so much about “client” request in test files.

test('can complete profile video step', async ({ client, assert }) => {
    const responseSplashBase = await client.get('http://www.splashbase.co/api/v1/images/random?videos_only=true').end();
    const slashBaseURL = responseSplashBase.body;
    const video = await new Promise((resolve, reject) => {
        const buffer = [];
        request({
            /* Here you should specify the exact link to the file you are trying to download */
            uri: slashBaseURL.large_url,
            headers: {
                Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
                'Accept-Encoding': 'gzip, deflate, br',
                'Accept-Language': 'en-US,en;q=0.9,fr;q=0.8,ro;q=0.7,ru;q=0.6,la;q=0.5,pt;q=0.4,de;q=0.3',
                'Cache-Control': 'max-age=0',
                Connection: 'keep-alive',
                'Upgrade-Insecure-Requests': '1',
                'User-Agent':
                    'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
            },
        })
            .on('data', data => {
                buffer.push(data);
            })
            .on('end', () => {
                const fileBuffered = Buffer.concat(buffer);
                resolve(fileBuffered);
            })
            .on('error', error => {
                reject(error);
            });
    }).catch(error => {
        console.log(`Something happened: ${error}`);
    });

    const responseRandomAPI = await client.get('https://cataas.com/cat').end();
    const bufferedImage = responseRandomAPI.body;

    const { mime } = fileType(bufferedImage);

    const base64Image = Buffer.from(bufferedImage, 'binary').toString('base64');
    const query = gql`
        mutation CompleteProfileVideo($user: CompleteProfileVideoInput!) {
            completeProfileVideo(user: $user) {
                _id
            }
        }
    `;
    console.log(video);

    const response = await client
        .post('/graphql')
        .header('Authorization', `Bearer ${token}`)
        .header('Content-Type', 'multipart/form-data')
        .send({
            query,
            variables: {
                user: {
                    _id: id,
                    image: `data:${mime};base64,${base64Image}`,
                    video,
                },
            },
        })
        .end();

    console.dir(response, { depth: null });

    assert.isTrue(
        !!response.body.data.completeProfileVideo && response.body.data.completeProfileVideo.actualStep === 6 && !response.body.errors,
    );
});

My video is a Buffer, and Image is a base64 string. How can I send this with form-data content-type?

1 Like