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

Add CORS headers to gateway responses #2254

Merged
merged 6 commits into from
Jul 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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