Skip to content

Commit

Permalink
feat!: remove sendAsJson option
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Aug 29, 2024
1 parent cc5433a commit 4b11fc4
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 38 deletions.
2 changes: 0 additions & 2 deletions packages/core/src/FetchMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import CallHistory from './CallHistory.js';
import * as requestUtils from './RequestUtils.js';

export type FetchMockGlobalConfig = {
sendAsJson?: boolean;
includeContentLength?: boolean;
matchPartialBody?: boolean;
allowRelativeUrls?: boolean;
Expand All @@ -20,7 +19,6 @@ export type FetchMockConfig = FetchMockGlobalConfig & FetchImplementations;

export const defaultFetchMockConfig: FetchMockConfig = {
includeContentLength: true,
sendAsJson: true,
matchPartialBody: false,
Request: globalThis.Request,
Response: globalThis.Response,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/Route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ class Route {
if (!bodyIsBodyInit) {
if (typeof body === 'undefined') {
body = null
} else if (typeof body === 'object' && this.config.sendAsJson) {
} else if (typeof body === 'object') {
// convert to json if we need to
body = JSON.stringify(body);
if (!responseOptions.headers.has('Content-Type')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,39 +102,6 @@ describe('response construction', () => {
const res = await fm.fetchHandler('http://a.com/');
expect(res.headers.get('content-type')).toEqual('application/json');
});

describe('sendAsJson option', () => {
it('convert object responses to json by default', async () => {
fm.route('*', { an: 'object' });
const res = await fm.fetchHandler('http://it.at.there');
expect(res.headers.get('content-type')).toEqual('application/json');
});

it("don't convert when configured false", async () => {
fm.config.sendAsJson = false;
fm.route('*', { an: 'object' });
const res = await fm.fetchHandler('http://it.at.there');
// can't check for existence as the spec says, in the browser, that
// a default value should be set
expect(res.headers.get('content-type')).not.toEqual('application/json');
});

it('local setting can override to true', async () => {
fm.config.sendAsJson = false;
fm.route('*', { an: 'object' }, { sendAsJson: true });
const res = await fm.fetchHandler('http://it.at.there');
expect(res.headers.get('content-type')).toEqual('application/json');
});

it('local setting can override to false', async () => {
fm.config.sendAsJson = true;
fm.route('*', { an: 'object' }, { sendAsJson: false });
const res = await fm.fetchHandler('http://it.at.there');
// can't check for existence as the spec says, in the browser, that
// a default value should be set
expect(res.headers.get('content-type')).not.toEqual('application/json');
});
});
});

it('respond with a complex response, including headers', async () => {
Expand All @@ -153,7 +120,7 @@ describe('response construction', () => {

if (typeof Buffer !== 'undefined') {
it('can respond with a buffer', () => {
fm.route(/a/, new Buffer('buffer'), { sendAsJson: false });
fm.route(/a/, new Buffer('buffer'));
return fm
.fetchHandler('http://a.com')
.then((res) => res.text())
Expand All @@ -165,7 +132,7 @@ describe('response construction', () => {

it('respond with blob', async () => {
const blob = new Blob();
fm.route('*', blob, { sendAsJson: false });
fm.route('*', blob);
const res = await fm.fetchHandler('http://a.com');
expect(res.status).to.equal(200);
const blobData = await res.blob();
Expand Down

0 comments on commit 4b11fc4

Please sign in to comment.