-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
caa9ae9
commit da95f14
Showing
11 changed files
with
142 additions
and
8 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
File renamed without changes.
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
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...es/api-request/src/root-url-middleware.js → ...s/api-request/src/middlewares/root-url.js
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,21 @@ | ||
import httpV1Middleware from '../http-v1'; | ||
|
||
describe( 'HTTP v1 Middleware', () => { | ||
it( 'should use a POST for a PUT requests', () => { | ||
const callback = ( options ) => { | ||
expect( options.method ).toBe( 'POST' ); | ||
expect( options.headers[ 'X-HTTP-Method-Override' ] ).toBe( 'PUT' ); | ||
}; | ||
|
||
httpV1Middleware( { method: 'PUT', data: {} }, callback ); | ||
} ); | ||
|
||
it( 'shouldn\'t touch the options for GET requests', () => { | ||
const requestOptions = { method: 'GET', path: '/wp/v2/posts' }; | ||
const callback = ( options ) => { | ||
expect( options ).toEqual( requestOptions ); | ||
}; | ||
|
||
httpV1Middleware( requestOptions, callback ); | ||
} ); | ||
} ); |
18 changes: 18 additions & 0 deletions
18
packages/api-request/src/middlewares/test/namespace-endpoint.js
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 @@ | ||
import namespaceEndpointMiddleware from '../namespace-endpoint'; | ||
|
||
describe( 'Namespace & Endpoint middleware', () => { | ||
it( 'should concat the endpoint and namespace into a path property', () => { | ||
const requestOptions = { | ||
method: 'GET', | ||
namespace: '/wp/v2', | ||
endpoint: '/posts', | ||
}; | ||
const callback = ( options ) => { | ||
expect( options.path ).toBe( 'wp/v2/posts' ); | ||
expect( options.namespace ).toBeUndefined(); | ||
expect( options.endpoint ).toBeUndefined(); | ||
}; | ||
|
||
namespaceEndpointMiddleware( 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,32 @@ | ||
import createNonceMiddleware from '../nonce'; | ||
|
||
describe( 'Nonce middleware', () => { | ||
it( 'should add a nonce header to the request', () => { | ||
const nonce = 'nonce'; | ||
const nonceMiddleware = createNonceMiddleware( nonce ); | ||
const requestOptions = { | ||
method: 'GET', | ||
path: '/wp/v2/posts', | ||
}; | ||
const callback = ( options ) => { | ||
expect( options.headers[ 'X-WP-Nonce' ] ).toBe( nonce ); | ||
}; | ||
|
||
nonceMiddleware( requestOptions, callback ); | ||
} ); | ||
|
||
it( 'should not add a nonce header to requests with nonces', () => { | ||
const nonce = 'nonce'; | ||
const nonceMiddleware = createNonceMiddleware( nonce ); | ||
const requestOptions = { | ||
method: 'GET', | ||
path: '/wp/v2/posts', | ||
headers: { 'X-WP-Nonce': 'existing nonce' }, | ||
}; | ||
const callback = ( options ) => { | ||
expect( options.headers[ 'X-WP-Nonce' ] ).toBe( 'existing nonce' ); | ||
}; | ||
|
||
nonceMiddleware( 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,41 @@ | ||
import createPreloadingMiddleware from '../preloading'; | ||
|
||
describe( 'Preloading Middleware', () => { | ||
it( 'should return the preloaded data if provided', () => { | ||
const body = { | ||
status: 'this is the preloaded response', | ||
}; | ||
const preloadedData = { | ||
'wp/v2/posts': { | ||
body, | ||
}, | ||
}; | ||
const prelooadingMiddleware = createPreloadingMiddleware( preloadedData ); | ||
const requestOptions = { | ||
method: 'GET', | ||
path: 'wp/v2/posts', | ||
}; | ||
|
||
const response = prelooadingMiddleware( requestOptions ); | ||
response.then( ( value ) => { | ||
expect( value ).toEqual( body ); | ||
} ); | ||
} ); | ||
|
||
it( 'should move to the next middleware if no preloaded data', () => { | ||
const preloadedData = {}; | ||
const prelooadingMiddleware = createPreloadingMiddleware( preloadedData ); | ||
const requestOptions = { | ||
method: 'GET', | ||
path: 'wp/v2/posts', | ||
}; | ||
|
||
const callback = ( options ) => { | ||
expect( options ).toBe( requestOptions ); | ||
return true; | ||
}; | ||
|
||
const ret = prelooadingMiddleware( requestOptions, callback ); | ||
expect( ret ).toBe( true ); | ||
} ); | ||
} ); |
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,17 @@ | ||
import createRootUrlMiddleware from '../root-url'; | ||
|
||
describe( 'Root URL middleware', () => { | ||
it( 'should append the root URL', () => { | ||
const rootURL = 'http://wp.org/wp-admin/rest/'; | ||
const rootURLMiddleware = createRootUrlMiddleware( rootURL ); | ||
const requestOptions = { | ||
method: 'GET', | ||
path: '/wp/v2/posts', | ||
}; | ||
const callback = ( options ) => { | ||
expect( options.url ).toBe( 'http://wp.org/wp-admin/rest/wp/v2/posts' ); | ||
}; | ||
|
||
rootURLMiddleware( requestOptions, callback ); | ||
} ); | ||
} ); |