Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Fetch: Make preloaded OPTIONS requests use parse setting #28862

Merged
merged 4 commits into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/api-fetch/src/middlewares/preloading.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ function createPreloadingMiddleware( preloadedData ) {
cache[ method ] &&
cache[ method ][ path ]
) {
return Promise.resolve( cache[ method ][ path ] );
return Promise.resolve(
parse
? cache[ method ][ path ].body
: cache[ method ][ path ]
);
}
}

Expand Down
66 changes: 66 additions & 0 deletions packages/api-fetch/src/middlewares/test/preloading.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,72 @@ describe( 'Preloading Middleware', () => {
expect( nextSpy ).toHaveBeenCalled();
} );
} );

describe( 'and the OPTIONS request has a parse flag', () => {
it( 'should return the full response if parse: false', () => {
const data = {
body: {
status: 'this is the preloaded response',
},
headers: {
Allow: 'GET, POST',
},
};

const preloadedData = {
OPTIONS: {
'wp/v2/posts': data,
},
};

const preloadingMiddleware = createPreloadingMiddleware(
preloadedData
);

const requestOptions = {
method: 'OPTIONS',
path: 'wp/v2/posts',
parse: false,
};

const response = preloadingMiddleware( requestOptions );
return response.then( ( value ) => {
expect( value ).toEqual( data );
} );
} );

it( 'should return only the response body if parse: true', () => {
const body = {
status: 'this is the preloaded response',
};

const preloadedData = {
OPTIONS: {
'wp/v2/posts': {
body,
headers: {
Allow: 'GET, POST',
},
},
},
};

const preloadingMiddleware = createPreloadingMiddleware(
preloadedData
);

const requestOptions = {
method: 'OPTIONS',
path: 'wp/v2/posts',
parse: true,
};

const response = preloadingMiddleware( requestOptions );
return response.then( ( value ) => {
expect( value ).toEqual( body );
} );
} );
} );
} );

describe( 'when the requested data is not from a preloaded endpoint', () => {
Expand Down