Skip to content

Commit

Permalink
set content-length header on badge responses (#7179)
Browse files Browse the repository at this point in the history
closes #7171

Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
  • Loading branch information
chris48s and repo-ranger[bot] authored Oct 21, 2021
1 parent 14f64d1 commit f62dbed
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
2 changes: 2 additions & 0 deletions core/base-service/legacy-result-sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ function streamFromString(str) {

function sendSVG(res, askres, end) {
askres.setHeader('Content-Type', 'image/svg+xml;charset=utf-8')
askres.setHeader('Content-Length', Buffer.byteLength(res, 'utf8'))
end(null, { template: streamFromString(res) })
}

function sendJSON(res, askres, end) {
askres.setHeader('Content-Type', 'application/json')
askres.setHeader('Access-Control-Allow-Origin', '*')
askres.setHeader('Content-Length', Buffer.byteLength(res, 'utf8'))
end(null, { template: streamFromString(res) })
}

Expand Down
20 changes: 18 additions & 2 deletions core/server/server.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,28 @@ describe('The server', function () {
)
})

it('should produce json badges', async function () {
it('should produce SVG badges with expected headers', async function () {
const { statusCode, headers } = await got(
`${baseUrl}:fruit-apple-green.svg`
)
expect(statusCode).to.equal(200)
expect(headers['content-type']).to.equal('image/svg+xml;charset=utf-8')
expect(headers['content-length']).to.equal('1130')
})

it('correctly calculates the content-length header for multi-byte unicode characters', async function () {
const { headers } = await got(`${baseUrl}:fruit-apple🍏-green.json`)
expect(headers['content-length']).to.equal('100')
})

it('should produce JSON badges with expected headers', async function () {
const { statusCode, body, headers } = await got(
`${baseUrl}twitter/follow/_Pyves.json`
`${baseUrl}:fruit-apple-green.json`
)
expect(statusCode).to.equal(200)
expect(headers['content-type']).to.equal('application/json')
expect(headers['access-control-allow-origin']).to.equal('*')
expect(headers['content-length']).to.equal('92')
expect(() => JSON.parse(body)).not.to.throw()
})

Expand Down

0 comments on commit f62dbed

Please sign in to comment.