Skip to content

Commit

Permalink
Add more tests for options.allowGetBody (#1190)
Browse files Browse the repository at this point in the history
Issue #1186
  • Loading branch information
PopGoesTheWza committed Apr 26, 2020
1 parent 429db40 commit d087215
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
20 changes: 19 additions & 1 deletion test/arguments.ts
Expand Up @@ -4,7 +4,7 @@ import test from 'ava';
import {Handler} from 'express';
import pEvent = require('p-event');
import got, {StrictOptions} from '../source';
import withServer from './helpers/with-server';
import withServer, {withBodyParsingServer} from './helpers/with-server';

const echoUrl: Handler = (request, response) => {
response.end(request.url);
Expand Down Expand Up @@ -495,3 +495,21 @@ test('reuse options while using init hook', withServer, async (t, server, got) =
await got('', options);
await got('', options);
});

test('allowGetBody sends json payload', withBodyParsingServer, 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);
});
7 changes: 5 additions & 2 deletions test/helpers/with-server.ts
@@ -1,5 +1,6 @@
import {promisify} from 'util';
import * as test from 'ava';
import is from '@sindresorhus/is';
import http = require('http');
import tempy = require('tempy');
import createTestServer = require('create-test-server');
Expand All @@ -10,10 +11,11 @@ import {ExtendedGot, ExtendedHttpServer, ExtendedTestServer, GlobalClock, Instal
export type RunTestWithServer = (t: test.ExecutionContext, server: ExtendedTestServer, got: ExtendedGot, clock: GlobalClock) => Promise<void> | void;
export type RunTestWithSocket = (t: test.ExecutionContext, server: ExtendedHttpServer) => Promise<void> | void;

const generateHook = ({install}: {install?: boolean}): test.Macro<[RunTestWithServer]> => async (t, run) => {
const generateHook = ({install, options: testServerOptions}: {install?: boolean; options?: unknown}): test.Macro<[RunTestWithServer]> => async (t, run) => {
const clock: GlobalClock = install ? lolex.install() : lolex.createClock();

const server = await createTestServer({
// Re-enable body parsing to investigate https://github.com/sindresorhus/got/issues/1186
const server = await createTestServer(is.plainObject(testServerOptions) ? testServerOptions : {
bodyParser: {
type: () => false
}
Expand Down Expand Up @@ -55,6 +57,7 @@ const generateHook = ({install}: {install?: boolean}): test.Macro<[RunTestWithSe
}
};

export const withBodyParsingServer = generateHook({install: false, options: {}});
export default generateHook({install: false});

export const withServerAndLolex = generateHook({install: true});
Expand Down
22 changes: 21 additions & 1 deletion test/pagination.ts
@@ -1,7 +1,7 @@
import {URL} from 'url';
import test from 'ava';
import got, {Response} from '../source';
import withServer from './helpers/with-server';
import withServer, {withBodyParsingServer} from './helpers/with-server';
import {ExtendedTestServer} from './helpers/types';

const thrower = (): any => {
Expand Down Expand Up @@ -308,3 +308,23 @@ test('ignores the `resolveBodyOnly` option', withServer, async (t, server, got)

t.deepEqual(items, [1, 2]);
});

test.failing('allowGetBody sends json payload with .paginate()', withBodyParsingServer, 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.deepEqual(result.value, [1, 2, 3]);
});

0 comments on commit d087215

Please sign in to comment.