Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Experimental API support (#7296)
* First basic API support * Change require functionality * Change 501 to 404 * Change wording * Fix test
- Loading branch information
1 parent
9f09299
commit fedfbd9
Showing
10 changed files
with
143 additions
and
2 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
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,6 @@ | ||
export default (req, res) => { | ||
res.writeHead(200, { 'Content-Type': 'application/json' }) | ||
|
||
const json = JSON.stringify([{ title: 'Cool Post!' }]) | ||
res.end(json) | ||
} |
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,15 @@ | ||
import url from 'url' | ||
|
||
export default (req, res) => { | ||
res.writeHead(200, { 'Content-Type': 'application/json' }) | ||
|
||
const { query } = url.parse(req.url, true) | ||
|
||
const users = [{ name: 'Tim' }, { name: 'Jon' }] | ||
|
||
const response = | ||
query && query.name ? users.filter(user => user.name === query.name) : users | ||
|
||
const json = JSON.stringify(response) | ||
res.end(json) | ||
} |
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 @@ | ||
export default () => <div>API - support</div> |
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 @@ | ||
export default () => <div>API - support</div> |
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,54 @@ | ||
/* eslint-env jest */ | ||
/* global jasmine */ | ||
import { join } from 'path' | ||
import { | ||
killApp, | ||
findPort, | ||
launchApp, | ||
fetchViaHTTP | ||
// renderViaHTTP, | ||
} from 'next-test-utils' | ||
|
||
const appDir = join(__dirname, '../') | ||
let appPort | ||
let server | ||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2 | ||
|
||
describe('API support', () => { | ||
beforeAll(async () => { | ||
appPort = await findPort() | ||
server = await launchApp(appDir, appPort) | ||
}) | ||
afterAll(() => killApp(server)) | ||
|
||
it('API request to undefined path', async () => { | ||
const { status } = await fetchViaHTTP(appPort, '/api/unexisting', null, { | ||
Accept: 'application/json' | ||
}) | ||
expect(status).toEqual(404) | ||
}) | ||
|
||
it('API request to list of users', async () => { | ||
const data = await fetchViaHTTP(appPort, '/api/users', null, { | ||
Accept: 'application/json' | ||
}).then(res => res.ok && res.json()) | ||
|
||
expect(data).toEqual([{ name: 'Tim' }, { name: 'Jon' }]) | ||
}) | ||
|
||
it('API request to list of users with query parameter', async () => { | ||
const data = await fetchViaHTTP(appPort, '/api/users?name=Tim', null, { | ||
Accept: 'application/json' | ||
}).then(res => res.ok && res.json()) | ||
|
||
expect(data).toEqual([{ name: 'Tim' }]) | ||
}) | ||
|
||
it('API request to nested posts', async () => { | ||
const data = await fetchViaHTTP(appPort, '/api/posts', null, { | ||
Accept: 'application/json' | ||
}).then(res => res.ok && res.json()) | ||
|
||
expect(data).toEqual([{ title: 'Cool Post!' }]) | ||
}) | ||
}) |
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