-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for put and delete requests.
- Loading branch information
1 parent
e93081e
commit fa0b303
Showing
5 changed files
with
362 additions
and
1 deletion.
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,75 @@ | ||
import faker from 'faker' | ||
import nock from 'nock' | ||
import { expect } from 'chai' | ||
import Resource from '../../src/Resource' | ||
import Navigator from '../../src/Navigator' | ||
import * as api from '../support/api' | ||
|
||
const baseUrl = faker.internet.url() | ||
|
||
describe('Navigator', () => { | ||
beforeEach(() => { | ||
nock.cleanAll() | ||
}) | ||
|
||
it('deletes resources in an API', async () => { | ||
api.onDiscover(baseUrl, {}, { | ||
user: {href: '/users/thomas'} | ||
}) | ||
|
||
api.onDelete(baseUrl, '/users/thomas', { | ||
permanent: true | ||
}) | ||
|
||
const discoveryResult = await Navigator.discover(baseUrl) | ||
const result = await discoveryResult.delete('user', { | ||
permanent: true | ||
}) | ||
|
||
expect(result.status()).to.equal(204) | ||
}) | ||
|
||
it('uses template params when creating resources', async () => { | ||
api.onDiscover(baseUrl, {}, { | ||
user: {href: '/users/{id}', templated: true} | ||
}) | ||
|
||
api.onDelete(baseUrl, '/users/thomas', { | ||
permanent: true | ||
}) | ||
|
||
const discoveryResult = await Navigator.discover(baseUrl) | ||
const result = await discoveryResult.delete('user', { | ||
permanent: true | ||
}, { | ||
id: 'thomas' | ||
}) | ||
|
||
expect(result.status()).to.equal(204) | ||
}) | ||
|
||
it('adds header options for navigation', async () => { | ||
api.onDiscover(baseUrl, {}, { | ||
user: {href: '/users/thomas'} | ||
}) | ||
|
||
const headers = { | ||
authorization: 'some-token' | ||
} | ||
|
||
api.onDelete(baseUrl, '/users/thomas', | ||
{permanent: true}, | ||
{headers}) | ||
|
||
api.onGet(baseUrl, '/users/thomas', | ||
new Resource() | ||
.addProperty('name', 'Thomas')) | ||
|
||
const discoveryResult = await Navigator.discover(baseUrl) | ||
const result = await discoveryResult.delete('user', { | ||
permanent: true | ||
}, {}, {headers}) | ||
|
||
expect(result.status()).to.equal(204) | ||
}) | ||
}) |
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,202 @@ | ||
import faker from 'faker' | ||
import nock from 'nock' | ||
import { expect } from 'chai' | ||
import Resource from '../../src/Resource' | ||
import Navigator from '../../src/Navigator' | ||
import * as api from '../support/api' | ||
|
||
const baseUrl = faker.internet.url() | ||
|
||
describe('Navigator', () => { | ||
beforeEach(() => { | ||
nock.cleanAll() | ||
}) | ||
|
||
it('replaces resources in an API', async () => { | ||
api.onDiscover(baseUrl, {}, { | ||
user: {href: '/users/thomas'} | ||
}) | ||
|
||
api.onPutToReplace(baseUrl, '/users/thomas', { | ||
name: 'Thomas' | ||
}) | ||
|
||
const discoveryResult = await Navigator.discover(baseUrl) | ||
const result = await discoveryResult.put('user', { | ||
name: 'Thomas' | ||
}) | ||
|
||
expect(result.status()).to.equal(200) | ||
|
||
expect(result.resource().getProperty('name')) | ||
.to.deep.equal('Thomas') | ||
}) | ||
|
||
it('uses template params when creating resources', async () => { | ||
api.onDiscover(baseUrl, {}, { | ||
user: {href: '/users/{id}', templated: true} | ||
}) | ||
|
||
api.onPutToReplace(baseUrl, '/users/thomas', { | ||
name: 'Sponge' | ||
}) | ||
|
||
const discoveryResult = await Navigator.discover(baseUrl) | ||
const result = await discoveryResult.put('user', { | ||
name: 'Sponge' | ||
}, { | ||
id: 'thomas' | ||
}) | ||
|
||
expect(result.status()).to.equal(200) | ||
|
||
expect(result.resource().getProperty('name')) | ||
.to.deep.equal('Sponge') | ||
}) | ||
|
||
it('uses absolute url when location is relative', async () => { | ||
api.onDiscover(baseUrl, {}, { | ||
user: {href: '/users/thomas'} | ||
}) | ||
|
||
api.onPutToReplace(baseUrl, '/users/thomas', { | ||
name: 'Thomas' | ||
}) | ||
|
||
const discoveryResult = await Navigator.discover(baseUrl) | ||
const result = await discoveryResult.put('user', { | ||
name: 'Thomas' | ||
}) | ||
|
||
expect(result.status()).to.equal(200) | ||
|
||
expect(result.resource().getProperty('name')) | ||
.to.deep.equal('Thomas') | ||
}) | ||
|
||
it('uses same configuration as provided on put when following redirect', | ||
async () => { | ||
api.onDiscover(baseUrl, {}, { | ||
user: {href: '/users/thomas'} | ||
}) | ||
|
||
api.onPutToCreate(baseUrl, '/users/thomas', { | ||
name: 'Thomas' | ||
}, | ||
`${baseUrl}/users/thomas`, { | ||
headers: { authorization: 'Bearer 1a2b3c4d' } | ||
}) | ||
|
||
api.onGet(baseUrl, '/users/thomas', | ||
new Resource().addProperty('name', 'Thomas'), { | ||
headers: { authorization: 'Bearer 1a2b3c4d' } | ||
}) | ||
|
||
const discoveryResult = await Navigator.discover(baseUrl) | ||
const result = await discoveryResult.put('user', { | ||
name: 'Thomas' | ||
}, {}, { | ||
headers: { | ||
authorization: 'Bearer 1a2b3c4d' | ||
} | ||
}) | ||
|
||
expect(result.status()).to.equal(200) | ||
|
||
expect(result.resource().getProperty('name')) | ||
.to.deep.equal('Thomas') | ||
}) | ||
|
||
it('does not follow location headers when the status is not 201', async () => { | ||
api.onDiscover(baseUrl, {}, { | ||
user: {href: '/users/thomas', templated: true} | ||
}) | ||
|
||
nock(baseUrl) | ||
.put('/users/thomas', {name: 'Thomas'}) | ||
.reply(400) | ||
|
||
const discoveryResult = await Navigator.discover(baseUrl) | ||
const result = await discoveryResult.put('user', { | ||
name: 'Thomas' | ||
}) | ||
|
||
expect(result.status()).to.equal(400) | ||
}) | ||
|
||
it('does not follow location headers when the options say not to', async () => { | ||
api.onDiscover(baseUrl, {}, { | ||
user: {href: '/users/thomas'} | ||
}) | ||
|
||
api.onPutToCreate(baseUrl, '/users/thomas', { | ||
name: 'Thomas' | ||
}, `${baseUrl}/users/thomas`) | ||
|
||
const discoveryResult = | ||
await Navigator.discover(baseUrl, {followRedirects: false}) | ||
const result = await discoveryResult.put('user', { | ||
name: 'Thomas' | ||
}) | ||
|
||
expect(result.status()).to.equal(201) | ||
|
||
expect(result.getHeader('location')) | ||
.to.deep.equal(`${baseUrl}/users/thomas`) | ||
}) | ||
|
||
it('continues the conversation even if we do not follow redirects', async () => { | ||
api.onDiscover(baseUrl, {}, { | ||
user: {href: '/users/thomas'} | ||
}) | ||
|
||
api.onPutToCreate(baseUrl, '/users/thomas', { | ||
name: 'Thomas' | ||
}, `${baseUrl}/users/thomas`) | ||
|
||
api.onGet(baseUrl, '/users/thomas', | ||
new Resource() | ||
.addProperty('name', 'Thomas')) | ||
|
||
const discoveryResult = | ||
await Navigator.discover(baseUrl, {followRedirects: false}) | ||
const putResult = await discoveryResult.put('user', { | ||
name: 'Thomas' | ||
}) | ||
const result = await putResult.followRedirect() | ||
|
||
expect(result.status()).to.equal(200) | ||
|
||
expect(result.resource().getProperty('name')) | ||
.to.deep.equal('Thomas') | ||
}) | ||
|
||
it('adds header options for navigation', async () => { | ||
api.onDiscover(baseUrl, {}, { | ||
user: {href: '/users/thomas'} | ||
}) | ||
|
||
const headers = { | ||
authorization: 'some-token' | ||
} | ||
|
||
api.onPutToCreate(baseUrl, '/users/thomas', | ||
{name: 'Thomas'}, | ||
`${baseUrl}/users/thomas`, | ||
{headers}) | ||
|
||
api.onGet(baseUrl, '/users/thomas', | ||
new Resource() | ||
.addProperty('name', 'Thomas')) | ||
|
||
const discoveryResult = await Navigator.discover(baseUrl) | ||
const result = await discoveryResult.put('user', { | ||
name: 'Thomas' | ||
}, {}, {headers}) | ||
|
||
expect(result.status()).to.equal(200) | ||
|
||
expect(result.resource().getProperty('name')) | ||
.to.deep.equal('Thomas') | ||
}) | ||
}) |
Oops, something went wrong.