Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
fix: add CORS headers to gateway responses (#2254)
Browse files Browse the repository at this point in the history
  • Loading branch information
nick authored and alanshaw committed Jul 18, 2019
1 parent a0f424d commit 5156a47
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,13 @@ class HttpApi {
}

async _createGatewayServer (host, port, ipfs) {
const server = Hapi.server({ host, port })
const server = Hapi.server({
host,
port,
routes: {
cors: true
}
})
server.app.ipfs = ipfs

await server.register({
Expand Down
16 changes: 16 additions & 0 deletions test/gateway/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,22 @@ describe('HTTP Gateway', function () {
expect(res.headers.suborigin).to.equal('ipfs000bafybeicg2rebjoofv4kbyovkw7af3rpiitvnl6i7ckcywaq6xjcxnc2mby')
})

it('returns CORS headers', async () => {
const res = await gateway.inject({
method: 'OPTIONS',
url: '/ipfs/QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o',
headers: {
origin: 'http://example.com',
'access-control-request-method': 'GET',
'access-control-request-headers': ''
}
})

expect(res.statusCode).to.equal(200)
expect(res.headers['access-control-allow-origin']).to.equal('http://example.com')
expect(res.headers['access-control-allow-methods']).to.equal('GET')
})

/* TODO when support for CIDv1 lands
it('valid CIDv1', (done) => {
gateway.inject({
Expand Down

0 comments on commit 5156a47

Please sign in to comment.