diff --git a/test/gateway/index.js b/test/gateway/index.js index 4a3f2dead9..b45a350dbc 100644 --- a/test/gateway/index.js +++ b/test/gateway/index.js @@ -10,13 +10,15 @@ const API = require('../../src/http') const loadFixture = require('aegir/fixtures') const os = require('os') const path = require('path') +const fileType = require('file-type') const bigFile = loadFixture(__dirname, '../../node_modules/interface-ipfs-core/test/fixtures/15mb.random', 'ipfs') const directoryContent = { 'index.html': loadFixture(__dirname, './test-folder/index.html', 'ipfs'), 'nested-folder/hello.txt': loadFixture(__dirname, './test-folder/nested-folder/hello.txt', 'ipfs'), 'nested-folder/ipfs.txt': loadFixture(__dirname, './test-folder/nested-folder/ipfs.txt', 'ipfs'), - 'nested-folder/nested.html': loadFixture(__dirname, './test-folder/nested-folder/nested.html', 'ipfs') + 'nested-folder/nested.html': loadFixture(__dirname, './test-folder/nested-folder/nested.html', 'ipfs'), + 'cat-folder/cat.jpg': loadFixture(__dirname, './test-folder/cat-folder/cat.jpg', 'ipfs') } describe('HTTP Gateway', () => { @@ -38,17 +40,17 @@ describe('HTTP Gateway', () => { } }) + const content = (name) => ({ + path: `test-folder/${name}`, + content: directoryContent[name] + }) + + const emptyDir = (name) => ({ path: `test-folder/${name}` }) + series([ (cb) => http.api.start(true, cb), (cb) => { gateway = http.api.server.select('Gateway') - const content = (name) => ({ - path: `test-folder/${name}`, - content: directoryContent[name] - }) - - const emptyDir = (name) => ({ path: `test-folder/${name}` }) - const expectedRootMultihash = 'QmbQD7EMEL1zeebwBsWEfA3ndgSS6F7S6iTuwuqasPgVRi' const dirs = [ @@ -79,6 +81,32 @@ describe('HTTP Gateway', () => { expect(file.hash).to.equal(expectedMultihash) cb() }) + }, + (cb) => { + const expectedMultihash = 'QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o' + + http.api.node.files.add(Buffer.from('hello world' + '\n'), (err, res) => { + expect(err).to.not.exist() + const file = res[0] + expect(file.path).to.equal(expectedMultihash) + expect(file.hash).to.equal(expectedMultihash) + cb() + }) + }, + (cb) => { + const expectedMultihash = 'QmW2WQi7j6c7UgJTarActp7tDNikE4B2qXtFCfLPdsgaTQ' + + let dir = [ + content('cat-folder/cat.jpg') + ] + + http.api.node.files.add(dir, (err, res) => { + expect(err).to.not.exist() + const file = res[1] + expect(file.path).to.equal('test-folder/cat-folder') + expect(file.hash).to.equal(expectedMultihash) + cb() + }) } ], done) }) @@ -142,6 +170,11 @@ describe('HTTP Gateway', () => { }, (res) => { expect(res.statusCode).to.equal(200) expect(res.headers['content-type']).to.equal('image/jpeg') + + let fileSignature = fileType(res.rawPayload) + expect(fileSignature.mime).to.equal('image/jpeg') + expect(fileSignature.ext).to.equal('jpg') + done() }) }) @@ -155,6 +188,11 @@ describe('HTTP Gateway', () => { }, (res) => { expect(res.statusCode).to.equal(200) expect(res.headers['content-type']).to.equal('text/html; charset=utf-8') + + // check if the cat picture is in the payload as a way to check + // if this is an index of this directory + let listedFile = res.payload.match(/\/ipfs\/QmW2WQi7j6c7UgJTarActp7tDNikE4B2qXtFCfLPdsgaTQ\/cat\.jpg/g) + expect(listedFile).to.have.lengthOf(1) done() }) }) diff --git a/test/gateway/test-folder/cat-folder/cat.jpg b/test/gateway/test-folder/cat-folder/cat.jpg new file mode 100644 index 0000000000..8d8cd22cf1 Binary files /dev/null and b/test/gateway/test-folder/cat-folder/cat.jpg differ