Skip to content

Commit

Permalink
feat: Check for body size limitation when requesting the capture endp…
Browse files Browse the repository at this point in the history
…oint (close #1)
  • Loading branch information
cnouguier committed Dec 15, 2021
1 parent 5a3c7de commit a160b3c
Show file tree
Hide file tree
Showing 3 changed files with 10,422 additions and 6 deletions.
11 changes: 6 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import { capture } from './capture.js'
import geojsonhint from '@mapbox/geojsonhint'

const port = process.env.PORT || 3000
const url = process.env.KANO_URL
const jwt = process.env.KANO_JWT
const kanoUrl = process.env.KANO_URL
const kanoJwt = process.env.KANO_JWT
const bodyLimit = process.env.BODY_LIMIT || '100kb'

// features validator middleware
const geoJsonValidator = function (req, res, next) {
Expand All @@ -27,14 +28,14 @@ const geoJsonValidator = function (req, res, next) {
// Initialize express app
const app = express()
app.use(cors()) // enable cors
app.use(express.urlencoded({extended: true}))
app.use(express.json())
app.use(express.urlencoded({limit: bodyLimit, extended: true}))
app.use(express.json({limit: bodyLimit}))
app.use(geoJsonValidator)

// Capture
app.post('/capture', async (req, res) => {
let start = new Date()
const buffer = await capture(Object.assign(req.body, { url, jwt }))
const buffer = await capture(Object.assign(req.body, { url : kanoUrl, jwt: kanoJwt }))
if (Buffer.isBuffer(buffer)) {
res.contentType('image/png')
res.send(buffer)
Expand Down
8 changes: 7 additions & 1 deletion test/capture.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,19 @@ describe(`suite:${suite}`, () => {
expect(match('layers')).to.be.true
})

it('capture geojson file', async () => {
it('capture heterogenous geojson file', async () => {
const body = JSON.parse(fs.readFileSync(path.join(dataDir, 'shapes.geojson')))
const res = await capture(body, 'shapes')
expect(res.status).to.equal(200)
expect(match('shapes')).to.be.true
})

it('handle too large geojson file', async () => {
const body = JSON.parse(fs.readFileSync(path.join(dataDir, 'adsb.geojson')))
const res = await capture(body, 'adsb')
expect(res.status).to.equal(413)
})

it('capture gradient geoson file', async () => {
let body = JSON.parse(fs.readFileSync(path.join(dataDir, 'flight.geojson')))
body.layers = ['layers-osm-dark']
Expand Down
Loading

0 comments on commit a160b3c

Please sign in to comment.