forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add user locale api-fetch middleware (WordPress#10862)
* Add user locale api-fetch middleware See WordPress#10811 * Don't modify existing _locale query arg * Add changelog entry * Update changelog * Use new hasQueryArg helper function See WordPress#10885. * Match on simply the route path, instead of malformed query param * Fully mock embedding test * Skip the embedding test until we can make the travis env mock the requests * Skip demo test too, for now
- Loading branch information
1 parent
1c21d42
commit 7cb7566
Showing
6 changed files
with
177 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import userLocaleMiddleware from '../user-locale'; | ||
|
||
describe( 'User locale middleware', () => { | ||
it( 'should append the _locale parameter to the path', () => { | ||
expect.hasAssertions(); | ||
|
||
const requestOptions = { | ||
method: 'GET', | ||
path: '/wp/v2/posts', | ||
}; | ||
|
||
const callback = ( options ) => { | ||
expect( options.path ).toBe( '/wp/v2/posts?_locale=user' ); | ||
}; | ||
|
||
userLocaleMiddleware( requestOptions, callback ); | ||
} ); | ||
|
||
it( 'should append the _locale parameter to path with existing query argument', () => { | ||
expect.hasAssertions(); | ||
|
||
const requestOptions = { | ||
method: 'GET', | ||
path: '/wp/v2/posts?foo=bar', | ||
}; | ||
|
||
const callback = ( options ) => { | ||
expect( options.path ).toBe( '/wp/v2/posts?foo=bar&_locale=user' ); | ||
}; | ||
|
||
userLocaleMiddleware( requestOptions, callback ); | ||
} ); | ||
|
||
it( 'does not modify existing single _locale parameter in path', () => { | ||
expect.hasAssertions(); | ||
|
||
const requestOptions = { | ||
method: 'GET', | ||
path: '/wp/v2/posts?_locale=foo', | ||
}; | ||
|
||
const callback = ( options ) => { | ||
expect( options.path ).toBe( '/wp/v2/posts?_locale=foo' ); | ||
}; | ||
|
||
userLocaleMiddleware( requestOptions, callback ); | ||
} ); | ||
|
||
it( 'does not modify existing _locale parameter in path', () => { | ||
expect.hasAssertions(); | ||
|
||
const requestOptions = { | ||
method: 'GET', | ||
path: '/wp/v2/posts?foo=bar&_locale=foo', | ||
}; | ||
|
||
const callback = ( options ) => { | ||
expect( options.path ).toBe( '/wp/v2/posts?foo=bar&_locale=foo' ); | ||
}; | ||
|
||
userLocaleMiddleware( requestOptions, callback ); | ||
} ); | ||
|
||
it( 'should append the _locale parameter to the url', () => { | ||
expect.hasAssertions(); | ||
|
||
const requestOptions = { | ||
method: 'GET', | ||
url: 'http://wp.org/wp-json/wp/v2/posts', | ||
}; | ||
|
||
const callback = ( options ) => { | ||
expect( options.url ).toBe( 'http://wp.org/wp-json/wp/v2/posts?_locale=user' ); | ||
}; | ||
|
||
userLocaleMiddleware( requestOptions, callback ); | ||
} ); | ||
|
||
it( 'should append the _locale parameter to url with existing query argument', () => { | ||
expect.hasAssertions(); | ||
|
||
const requestOptions = { | ||
method: 'GET', | ||
url: 'http://wp.org/wp-json/wp/v2/posts?foo=bar', | ||
}; | ||
|
||
const callback = ( options ) => { | ||
expect( options.url ).toBe( 'http://wp.org/wp-json/wp/v2/posts?foo=bar&_locale=user' ); | ||
}; | ||
|
||
userLocaleMiddleware( requestOptions, callback ); | ||
} ); | ||
|
||
it( 'does not modify existing single _locale parameter in url', () => { | ||
expect.hasAssertions(); | ||
|
||
const requestOptions = { | ||
method: 'GET', | ||
url: 'http://wp.org/wp-json/wp/v2/posts?_locale=foo', | ||
}; | ||
|
||
const callback = ( options ) => { | ||
expect( options.url ).toBe( 'http://wp.org/wp-json/wp/v2/posts?_locale=foo' ); | ||
}; | ||
|
||
userLocaleMiddleware( requestOptions, callback ); | ||
} ); | ||
|
||
it( 'does not modify existing _locale parameter in url', () => { | ||
expect.hasAssertions(); | ||
|
||
const requestOptions = { | ||
method: 'GET', | ||
url: 'http://wp.org/wp-json/wp/v2/posts?foo=bar&_locale=foo', | ||
}; | ||
|
||
const callback = ( options ) => { | ||
expect( options.url ).toBe( 'http://wp.org/wp-json/wp/v2/posts?foo=bar&_locale=foo' ); | ||
}; | ||
|
||
userLocaleMiddleware( requestOptions, callback ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { addQueryArgs, hasQueryArg } from '@wordpress/url'; | ||
|
||
function userLocaleMiddleware( options, next ) { | ||
if ( typeof options.url === 'string' && ! hasQueryArg( options.url, '_locale' ) ) { | ||
options.url = addQueryArgs( options.url, { _locale: 'user' } ); | ||
} | ||
|
||
if ( typeof options.path === 'string' && ! hasQueryArg( options.path, '_locale' ) ) { | ||
options.path = addQueryArgs( options.path, { _locale: 'user' } ); | ||
} | ||
|
||
return next( options, next ); | ||
} | ||
|
||
export default userLocaleMiddleware; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters