Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

Commit

Permalink
Simple testing
Browse files Browse the repository at this point in the history
  • Loading branch information
cavis committed Apr 22, 2019
1 parent ba93b25 commit b2c85ab
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 1 deletion.
22 changes: 22 additions & 0 deletions index.js
@@ -0,0 +1,22 @@
const log = require('lambda-log')
const {base64Pixel, pixelSize} = require('./lib/constants')

/**
* Serve a 1x1 pixel and log metrics
*/
exports.handler = async (event) => {
if (event.path === '/i.gif') {
return {
statusCode: 200,
body: base64Pixel,
isBase64Encoded: true,
headers: {'content-type': 'image/gif', 'content-length': pixelSize},
}
} else {
return {
statusCode: 404,
body: 'Pixel not found',
headers: {'content-type': 'text/plain'},
}
}
}
21 changes: 21 additions & 0 deletions index.test.js
@@ -0,0 +1,21 @@
const {handler} = require('./index')

describe('index', () => {

it('returns 404s for unknown paths', async () => {
expect(await handler({})).toMatchObject({statusCode: 404})
expect(await handler({path: '/any/path'})).toMatchObject({statusCode: 404})
expect(await handler({path: '/any/i.gif'})).toMatchObject({statusCode: 404})
expect(await handler({path: '/i.png'})).toMatchObject({statusCode: 404})
})

it('returns a pixel', async () => {
const resp = await handler({path: '/i.gif'})
expect(resp.statusCode).toEqual(200)
expect(resp.headers['content-type']).toEqual('image/gif')
expect(resp.headers['content-length']).toEqual(35)
expect(resp.isBase64Encoded).toEqual(true)
expect(new Buffer(resp.body, 'base64').byteLength).toEqual(35)
})

})
8 changes: 8 additions & 0 deletions lib/constants.js
@@ -0,0 +1,8 @@
exports.pixel = Buffer.from([
0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x01, 0x00, 0x01, 0x00,
0x80, 0x00, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x2c,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x02,
0x02, 0x44, 0x01, 0x00, 0x3b,
])
exports.pixelSize = Buffer.byteLength(exports.pixel)
exports.base64Pixel = exports.pixel.toString('base64')
3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -26,6 +26,9 @@
"url": "https://github.com/PRX/pixel.prx.org/issues"
},
"homepage": "https://github.com/PRX/pixel.prx.org",
"dependencies": {
"lambda-log": "^2.2.0"
},
"devDependencies": {
"express": "^4.16.4",
"jest": "^24.7.1",
Expand Down
9 changes: 8 additions & 1 deletion yarn.lock
Expand Up @@ -2175,7 +2175,7 @@ json-schema@0.2.3:
version "0.2.3"
resolved "https://registry.yarnpkg.com/json-schema/-/json-schema-0.2.3.tgz#b480c892e59a2f05954ce727bd3f2a4e882f9e13"

json-stringify-safe@~5.0.1:
json-stringify-safe@^5.0.1, json-stringify-safe@~5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb"

Expand Down Expand Up @@ -2218,6 +2218,13 @@ kleur@^3.0.2:
version "3.0.3"
resolved "https://registry.yarnpkg.com/kleur/-/kleur-3.0.3.tgz#a79c9ecc86ee1ce3fa6206d1216c501f147fc07e"

lambda-log@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/lambda-log/-/lambda-log-2.2.0.tgz#39981bbded4fc2ed4f71f8b0c0c4682971a8d9c1"
integrity sha512-RM2NiHkz7ahL1TLBuDBnPy4G1BtA1rn25/YVvCidC3agjvnCPU6g1awYiHAEEnTt6ah93Ay/7oMULQrCuPeUdA==
dependencies:
json-stringify-safe "^5.0.1"

latest-version@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/latest-version/-/latest-version-3.1.0.tgz#a205383fea322b33b5ae3b18abee0dc2f356ee15"
Expand Down

0 comments on commit b2c85ab

Please sign in to comment.