Skip to content
This repository has been archived by the owner on Oct 6, 2020. It is now read-only.

Commit

Permalink
feat: handle API key
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Jun 8, 2020
1 parent 47c065f commit af68449
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
35 changes: 30 additions & 5 deletions src/server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,25 @@ import fetch from 'node-fetch'

const port = 3000
const hostname = `http://0.0.0.0:${port}`
const apiKey = 'some-key'
const headers = { Authorization: `Bearer ${apiKey}` }

describe('Flexport API Sandbox', () => {
let server: http.Server
beforeAll(() => {
server = http.createServer(requestHandler(hostname))
server = http.createServer(
requestHandler({
hostname,
apiKey,
}),
)
server.listen(port)
})
afterAll(() => {
server.close()
})
it('returns a response when an URL is found', async () => {
const res = await fetch(`${hostname}/shipments`)
const res = await fetch(`${hostname}/shipments`, { headers })
expect(res.status).toEqual(200)
expect(res.headers.get('content-type')).toEqual(
'application/json; charset=utf-8',
Expand All @@ -24,20 +31,38 @@ describe('Flexport API Sandbox', () => {
expect(response.data.data).toHaveLength(10)
})
it('returns a response for a subfolder', async () => {
const res = await fetch(`${hostname}/shipments/253590`)
const res = await fetch(`${hostname}/shipments/253590`, { headers })
expect(res.status).toEqual(200)
const response = await res.json()
expect(response.data.name).toEqual('LCL Test Shipment')
})
it('returns a 302 in case an URL is not found', async () => {
const res = await fetch(`${hostname}/foo`, { redirect: 'manual' })
const res = await fetch(`${hostname}/foo`, { redirect: 'manual', headers })
expect(res.status).toEqual(302)
expect(res.headers.get('Content-Type')).toEqual(`text/html; charset=utf-8`)
expect(res.headers.get('location')).toEqual(`${hostname}/`)
})
it('returns a 404 for the start page', async () => {
const res = await fetch(`${hostname}/`)
const res = await fetch(`${hostname}/`, { headers })
expect(res.status).toEqual(404)
expect(res.headers.get('Content-Type')).toEqual(`application/json`)
})
it('returns a 401 if the API token does not match', async () => {
const res = await fetch(`${hostname}/`, {
headers: { Authorization: `Bearer foo` },
})
expect(res.status).toEqual(401)
expect(res.headers.get('Content-Type')).toEqual(
`application/json; charset=utf-8`,
)
const response = await res.json()
expect(response).toEqual({
errors: [
{
code: 'bad_token',
message: 'Bad Token',
},
],
})
})
})
23 changes: 22 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
import * as http from 'http'
import { responseForPath } from './responseForPath'

export const requestHandler = (hostname: string) => {
export const requestHandler = ({
hostname,
apiKey,
}: {
hostname: string
apiKey: string
}) => {
const p = handlePath(hostname)
return async (
request: http.IncomingMessage,
response: http.ServerResponse,
) => {
if (request.headers.authorization !== `Bearer ${apiKey}`) {
response.writeHead(401, {
'Content-Type': 'application/json; charset=utf-8',
})
response.end(
JSON.stringify({
errors: [
{
code: 'bad_token',
message: 'Bad Token',
},
],
}),
)
}
const res = await p(request.url)
response.writeHead(res.statusCode, res.headers)
response.end(res.body)
Expand Down
4 changes: 3 additions & 1 deletion src/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { requestHandler } from './server'

const main = () => {
const port = process.env.SANDBOX_API_PORT ?? 3000
const apiKey = process.env.SANDBOX_API_KEY ?? 'secret'
const hostname = `http://0.0.0.0:${port}`

const server = http.createServer(requestHandler(hostname))
const server = http.createServer(requestHandler({ hostname, apiKey }))

server.listen(port, () => {
console.debug('[Flexport API Sandbox]', `is listening on ${hostname}`)
console.debug('[Flexport API Sandbox]', `API key: ${apiKey}`)
})
}

Expand Down

0 comments on commit af68449

Please sign in to comment.