sindresorhus / got

🌐 Human-friendly and powerful HTTP request library for Node.js
MIT License
14.27k stars 935 forks source link

allowGetBody payload is dropped by .paginate() #1186

Closed PopGoesTheWza closed 4 years ago

PopGoesTheWza commented 4 years ago

this is a spinoff of #1171

since got@11.0.0, .paginate() no longer send the body (json payload) of an allowGetBody query

Actual behavior

the json payload (a filter expression) is not sent to the server which returns the whole set of records

Expected behavior

Like in got@10.x the server should only return the records matching the filter (i.e. the json payload)

Code to reproduce

I have two simple allowGetBody test cases. In order to run, test/helpers/with-server.ts needs to be changed in lines 16-20:

    const server = await createTestServer({
        bodyParser: {
            // type: () => false
        }
    }) as ExtendedTestServer;

This enable the bodyParser middleware to properly populate request.body. Because I do not fathom the possible impact of above change, I won't issue a PR.

First a simple and successful test

test('allowGetBody sends json payload', withServer, async (t, server, got) => {
    server.get('/', (request, response) => {
        if (request.body.hello !== 'world') {
            response.statusCode = 400;
        }

        response.end();
    });

    const {statusCode} = await got({
        allowGetBody: true,
        json: {hello: 'world'},
        retry: 0,
        throwHttpErrors: false
    });
    t.is(statusCode, 200);
});

Now with .paginate() (it fails)

test('allowGetBody sends json payload with .paginate()', withServer, async (t, server, got) => {
    server.get('/', (request, response) => {
        if (request.body.hello !== 'world') {
            response.statusCode = 400;
        }

        response.end(JSON.stringify([1, 2, 3]));
    });

    const iterator = got.paginate({
        allowGetBody: true,
        json: {hello: 'world'},
        retry: 0
    });

    const result = await iterator.next();

    t.is(result.value, 1);
});

Checklist

szmarczak commented 4 years ago

Good news: I have located the source of the bug

PopGoesTheWza commented 4 years ago

About time! ;)

Thanks a lot.

szmarczak commented 4 years ago

It was a typo in the code :P