Skip to content

Ignore Got defaults when extending instances #1450

@ghermeto

Description

@ghermeto

Describe the bug

  • Node.js version: 10, 12, 14
  • OS & version: MacOS

The value passed to responseType is not being respected the plugin is extended by another plugin that doesn't set it.

Actual behavior

If PluginA sets responseType to json (or something else) and then PluginA extends a PluginB which doesn't set a responseType, the resulting responseType will be text.

Expected behavior

If only one plugin sets the responseType, then that value should be used. If more then one sets it, then it should prefer the extension one.

Code to reproduce

test('extending responseType', withServer, async (t, server) => {
	server.get('/', (req, res) => {
		res.json(req.headers);
	});

	const instance1 = got.extend({
		prefixUrl: server.url,
		responseType: 'json'
	});

	const instance2 = got.extend({
		headers: { 'x-test': 'test' }
	});

	const merged = instance1.extend(instance2);

	const {body} = await merged.get('');
	t.is((body as unknown as Record<string, unknown>)['x-test'], 'test');
});

If, instead, merged is defined as instance2.extend(instance1) the tests pass.

Quick fix

replacing L21-23 from /source/as-promise/normalize-arguments.ts by

	if (options.responseType === undefined) {
		options.responseType = defaults?.responseType ?? 'text';
	} else if (options.responseType === 'text' && defaults?.responseType) {
		options.responseType = defaults.responseType;
	}

will make the test pass.

Checklist

  • I have read the documentation.
  • I have tried my code with the latest version of Node.js and Got.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions