From c900f1ee0a661d56c03334c8cbe7fb3e779cc7d8 Mon Sep 17 00:00:00 2001 From: Yahya Date: Wed, 6 Sep 2017 18:17:55 +0200 Subject: [PATCH 1/2] gateway Essential tests --- examples/traverse-ipld-graphs/git.js | 5 +- src/http/gateway/resolver.js | 2 +- test/gateway/index.js | 139 +++++++++++++++++- test/gateway/interface/block.js | 20 +++ test/gateway/interface/config.js | 20 +++ test/gateway/interface/files.js | 20 +++ test/gateway/interface/object.js | 20 +++ test/gateway/interface/pubsub.js | 23 +++ test/gateway/interface/swarm.js | 20 +++ test/gateway/test-folder/index.html | 10 ++ .../test-folder/nested-folder/hello.txt | 1 + .../test-folder/nested-folder/ipfs.txt | 1 + .../test-folder/nested-folder/nested.html | 10 ++ 13 files changed, 284 insertions(+), 7 deletions(-) create mode 100644 test/gateway/interface/block.js create mode 100644 test/gateway/interface/config.js create mode 100644 test/gateway/interface/files.js create mode 100644 test/gateway/interface/object.js create mode 100644 test/gateway/interface/pubsub.js create mode 100644 test/gateway/interface/swarm.js create mode 100644 test/gateway/test-folder/index.html create mode 100644 test/gateway/test-folder/nested-folder/hello.txt create mode 100644 test/gateway/test-folder/nested-folder/ipfs.txt create mode 100644 test/gateway/test-folder/nested-folder/nested.html diff --git a/examples/traverse-ipld-graphs/git.js b/examples/traverse-ipld-graphs/git.js index c52c7ecd46..d72bcbe67b 100644 --- a/examples/traverse-ipld-graphs/git.js +++ b/examples/traverse-ipld-graphs/git.js @@ -47,13 +47,13 @@ createNode((err, ipfs) => { const v1tag = 'z8mWaGfwSWLMPJ6Q2JdsAjGiXTf61Nbue' - function errOrLog(comment) { + function errOrLog (comment) { return (err, result) => { if (err) { throw err } - if (Buffer.isBuffer(result.value)) { //Blobs (files) are returned as buffer instance + if (Buffer.isBuffer(result.value)) { // Blobs (files) are returned as buffer instance result.value = result.value.toString() } @@ -63,7 +63,6 @@ createNode((err, ipfs) => { } } - ipfs.dag.get(v1tag + '/', errOrLog('Tag object:')) ipfs.dag.get(v1tag + '/object/message', errOrLog('Tagged commit message:')) ipfs.dag.get(v1tag + '/object/parents/0/message', errOrLog('Parent of tagged commit:')) diff --git a/src/http/gateway/resolver.js b/src/http/gateway/resolver.js index 014c8adfa5..11a0db4db5 100644 --- a/src/http/gateway/resolver.js +++ b/src/http/gateway/resolver.js @@ -38,7 +38,7 @@ const resolveDirectory = promisify((ipfs, path, multihash, callback) => { return callback(null, indexFiles) } - return callback(null, dirView.build(path, dagNode.links)) + return callback(null, dirView.render(path, dagNode.links)) }) }) diff --git a/test/gateway/index.js b/test/gateway/index.js index 9a77f9903c..13759a1205 100644 --- a/test/gateway/index.js +++ b/test/gateway/index.js @@ -1,11 +1,21 @@ /* eslint-env mocha */ 'use strict' +const fs = require('fs') +const path = require('path') const chai = require('chai') const dirtyChai = require('dirty-chai') const expect = chai.expect chai.use(dirtyChai) const API = require('../../src/http') +const loadFixture = require('aegir/fixtures') +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') +} describe('HTTP Gateway', () => { let http = {} @@ -15,8 +25,35 @@ describe('HTTP Gateway', () => { http.api = new API() http.api.start(true, () => { - gateway = http.api.server.select('Gateway') - done() + const content = (name) => ({ + path: `test-folder/${name}`, + content: directoryContent[name] + }) + + const emptyDir = (name) => ({ + path: `test-folder/${name}` + }) + + const expectedRootMultihash = 'QmbQD7EMEL1zeebwBsWEfA3ndgSS6F7S6iTuwuqasPgVRi' + + const dirs = [ + content('index.html'), + emptyDir('empty-folder'), + content('nested-folder/hello.txt'), + content('nested-folder/ipfs.txt'), + content('nested-folder/nested.html'), + emptyDir('nested-folder/empty') + ] + + http.api.node.files.add(dirs, (err, res) => { + expect(err).to.not.exist() + const root = res[res.length - 1] + + expect(root.path).to.equal('test-folder') + expect(root.hash).to.equal(expectedRootMultihash) + gateway = http.api.server.select('Gateway') + done() + }) }) }) @@ -27,7 +64,12 @@ describe('HTTP Gateway', () => { }) }) - describe('/ipfs/* route', () => { + describe('## interface tests', () => { + fs.readdirSync(path.join(__dirname, '/interface')) + .forEach((file) => require('./interface/' + file)) + }) + + describe('## HTTP Gateway', () => { it('returns 400 for request without argument', (done) => { gateway.inject({ method: 'GET', @@ -61,5 +103,96 @@ describe('HTTP Gateway', () => { done() }) }) + + it('stream a large file', (done) => { + let bigFileHash = 'Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq' + + gateway.inject({ + method: 'GET', + url: '/ipfs/' + bigFileHash + }, (res) => { + expect(res.statusCode).to.equal(200) + expect(res.rawPayload).to.deep.equal(bigFile) + done() + }) + }) + + it('load a non text file', (done) => { + let kitty = 'QmW2WQi7j6c7UgJTarActp7tDNikE4B2qXtFCfLPdsgaTQ/cat.jpg' + + gateway.inject({ + method: 'GET', + url: '/ipfs/' + kitty + }, (res) => { + expect(res.statusCode).to.equal(200) + expect(res.headers['content-type']).to.equal('image/jpeg') + done() + }) + }) + + it('load a directory', (done) => { + let dir = 'QmW2WQi7j6c7UgJTarActp7tDNikE4B2qXtFCfLPdsgaTQ/' + + gateway.inject({ + method: 'GET', + url: '/ipfs/' + dir + }, (res) => { + expect(res.statusCode).to.equal(200) + expect(res.headers['content-type']).to.equal('text/html; charset=utf-8') + done() + }) + }) + + it('load a webpage index.html', (done) => { + let dir = 'QmbQD7EMEL1zeebwBsWEfA3ndgSS6F7S6iTuwuqasPgVRi/index.html' + + gateway.inject({ + method: 'GET', + url: '/ipfs/' + dir + }, (res) => { + expect(res.statusCode).to.equal(200) + expect(res.rawPayload).to.deep.equal(directoryContent['index.html']) + done() + }) + }) + + it('load a webpage {hash}/nested-folder/nested.html', (done) => { + let dir = 'QmbQD7EMEL1zeebwBsWEfA3ndgSS6F7S6iTuwuqasPgVRi/nested-folder/nested.html' + + gateway.inject({ + method: 'GET', + url: '/ipfs/' + dir + }, (res) => { + expect(res.statusCode).to.equal(200) + expect(res.rawPayload).to.deep.equal(directoryContent['nested-folder/nested.html']) + done() + }) + }) + + it('redirect to generated index', (done) => { + let dir = 'QmW2WQi7j6c7UgJTarActp7tDNikE4B2qXtFCfLPdsgaTQ' + + gateway.inject({ + method: 'GET', + url: '/ipfs/' + dir + }, (res) => { + expect(res.statusCode).to.equal(301) + expect(res.headers['location']).to.equal('/ipfs/QmW2WQi7j6c7UgJTarActp7tDNikE4B2qXtFCfLPdsgaTQ/') + done() + }) + }) + + it('redirect to webpage index.html', (done) => { + let dir = 'QmbQD7EMEL1zeebwBsWEfA3ndgSS6F7S6iTuwuqasPgVRi/' + + gateway.inject({ + method: 'GET', + url: '/ipfs/' + dir + }, (res) => { + expect(res.statusCode).to.equal(302) + expect(res.headers['location']).to.equal('/ipfs/QmbQD7EMEL1zeebwBsWEfA3ndgSS6F7S6iTuwuqasPgVRi/index.html') + done() + }) + }) }) }) diff --git a/test/gateway/interface/block.js b/test/gateway/interface/block.js new file mode 100644 index 0000000000..db39d378c6 --- /dev/null +++ b/test/gateway/interface/block.js @@ -0,0 +1,20 @@ +/* eslint-env mocha */ + +'use strict' + +const test = require('interface-ipfs-core') +const FactoryClient = require('./../../utils/ipfs-factory-daemon') + +let fc + +const common = { + setup: function (callback) { + fc = new FactoryClient() + callback(null, fc) + }, + teardown: function (callback) { + fc.dismantle(callback) + } +} + +test.block(common) diff --git a/test/gateway/interface/config.js b/test/gateway/interface/config.js new file mode 100644 index 0000000000..30837f8b86 --- /dev/null +++ b/test/gateway/interface/config.js @@ -0,0 +1,20 @@ +/* eslint-env mocha */ + +'use strict' + +const test = require('interface-ipfs-core') +const FactoryClient = require('./../../utils/ipfs-factory-daemon') + +let fc + +const common = { + setup: function (callback) { + fc = new FactoryClient() + callback(null, fc) + }, + teardown: function (callback) { + fc.dismantle(callback) + } +} + +test.config(common) diff --git a/test/gateway/interface/files.js b/test/gateway/interface/files.js new file mode 100644 index 0000000000..84b150f79f --- /dev/null +++ b/test/gateway/interface/files.js @@ -0,0 +1,20 @@ +/* eslint-env mocha */ + +'use strict' + +const test = require('interface-ipfs-core') +const FactoryClient = require('./../../utils/ipfs-factory-daemon') + +let fc + +const common = { + setup: function (callback) { + fc = new FactoryClient() + callback(null, fc) + }, + teardown: function (callback) { + fc.dismantle(callback) + } +} + +test.files(common) diff --git a/test/gateway/interface/object.js b/test/gateway/interface/object.js new file mode 100644 index 0000000000..e00df72564 --- /dev/null +++ b/test/gateway/interface/object.js @@ -0,0 +1,20 @@ +/* eslint-env mocha */ + +'use strict' + +const test = require('interface-ipfs-core') +const FactoryClient = require('./../../utils/ipfs-factory-daemon') + +let fc + +const common = { + setup: function (callback) { + fc = new FactoryClient() + callback(null, fc) + }, + teardown: function (callback) { + fc.dismantle(callback) + } +} + +test.object(common) diff --git a/test/gateway/interface/pubsub.js b/test/gateway/interface/pubsub.js new file mode 100644 index 0000000000..968227c83b --- /dev/null +++ b/test/gateway/interface/pubsub.js @@ -0,0 +1,23 @@ +/* eslint-env mocha */ + +'use strict' + +// TODO needs: https://github.com/ipfs/js-ipfs-api/pull/493 +/* +const test = require('interface-ipfs-core') +const FactoryClient = require('./../../utils/ipfs-factory-daemon') + +let fc + +const common = { + setup: function (callback) { + fc = new FactoryClient() + callback(null, fc) + }, + teardown: function (callback) { + fc.dismantle(callback) + } +} + +test.pubsub(common) +*/ diff --git a/test/gateway/interface/swarm.js b/test/gateway/interface/swarm.js new file mode 100644 index 0000000000..eeca079124 --- /dev/null +++ b/test/gateway/interface/swarm.js @@ -0,0 +1,20 @@ +/* eslint-env mocha */ + +'use strict' + +const test = require('interface-ipfs-core') +const FactoryClient = require('./../../utils/ipfs-factory-daemon') + +let fc + +const common = { + setup: function (callback) { + fc = new FactoryClient() + callback(null, fc) + }, + teardown: function (callback) { + fc.dismantle(callback) + } +} + +test.swarm(common) diff --git a/test/gateway/test-folder/index.html b/test/gateway/test-folder/index.html new file mode 100644 index 0000000000..a83cca1dfe --- /dev/null +++ b/test/gateway/test-folder/index.html @@ -0,0 +1,10 @@ + + + + + IPFS test index.html + + + index.html + + diff --git a/test/gateway/test-folder/nested-folder/hello.txt b/test/gateway/test-folder/nested-folder/hello.txt new file mode 100644 index 0000000000..e965047ad7 --- /dev/null +++ b/test/gateway/test-folder/nested-folder/hello.txt @@ -0,0 +1 @@ +Hello diff --git a/test/gateway/test-folder/nested-folder/ipfs.txt b/test/gateway/test-folder/nested-folder/ipfs.txt new file mode 100644 index 0000000000..95a3116521 --- /dev/null +++ b/test/gateway/test-folder/nested-folder/ipfs.txt @@ -0,0 +1 @@ +IPFS diff --git a/test/gateway/test-folder/nested-folder/nested.html b/test/gateway/test-folder/nested-folder/nested.html new file mode 100644 index 0000000000..6f81153fcd --- /dev/null +++ b/test/gateway/test-folder/nested-folder/nested.html @@ -0,0 +1,10 @@ + + + + + IPFS test nested.html + + + nested.html + + From 0b8637fb2afb5bb73f8654ffacf87c5944c75120 Mon Sep 17 00:00:00 2001 From: Yahya Date: Wed, 6 Sep 2017 19:23:44 +0200 Subject: [PATCH 2/2] removing interface tests from gateway --- test/gateway/index.js | 7 ------- test/gateway/interface/block.js | 20 -------------------- test/gateway/interface/config.js | 20 -------------------- test/gateway/interface/files.js | 20 -------------------- test/gateway/interface/object.js | 20 -------------------- test/gateway/interface/pubsub.js | 23 ----------------------- test/gateway/interface/swarm.js | 20 -------------------- 7 files changed, 130 deletions(-) delete mode 100644 test/gateway/interface/block.js delete mode 100644 test/gateway/interface/config.js delete mode 100644 test/gateway/interface/files.js delete mode 100644 test/gateway/interface/object.js delete mode 100644 test/gateway/interface/pubsub.js delete mode 100644 test/gateway/interface/swarm.js diff --git a/test/gateway/index.js b/test/gateway/index.js index 13759a1205..ac10be0829 100644 --- a/test/gateway/index.js +++ b/test/gateway/index.js @@ -1,8 +1,6 @@ /* eslint-env mocha */ 'use strict' -const fs = require('fs') -const path = require('path') const chai = require('chai') const dirtyChai = require('dirty-chai') const expect = chai.expect @@ -64,11 +62,6 @@ describe('HTTP Gateway', () => { }) }) - describe('## interface tests', () => { - fs.readdirSync(path.join(__dirname, '/interface')) - .forEach((file) => require('./interface/' + file)) - }) - describe('## HTTP Gateway', () => { it('returns 400 for request without argument', (done) => { gateway.inject({ diff --git a/test/gateway/interface/block.js b/test/gateway/interface/block.js deleted file mode 100644 index db39d378c6..0000000000 --- a/test/gateway/interface/block.js +++ /dev/null @@ -1,20 +0,0 @@ -/* eslint-env mocha */ - -'use strict' - -const test = require('interface-ipfs-core') -const FactoryClient = require('./../../utils/ipfs-factory-daemon') - -let fc - -const common = { - setup: function (callback) { - fc = new FactoryClient() - callback(null, fc) - }, - teardown: function (callback) { - fc.dismantle(callback) - } -} - -test.block(common) diff --git a/test/gateway/interface/config.js b/test/gateway/interface/config.js deleted file mode 100644 index 30837f8b86..0000000000 --- a/test/gateway/interface/config.js +++ /dev/null @@ -1,20 +0,0 @@ -/* eslint-env mocha */ - -'use strict' - -const test = require('interface-ipfs-core') -const FactoryClient = require('./../../utils/ipfs-factory-daemon') - -let fc - -const common = { - setup: function (callback) { - fc = new FactoryClient() - callback(null, fc) - }, - teardown: function (callback) { - fc.dismantle(callback) - } -} - -test.config(common) diff --git a/test/gateway/interface/files.js b/test/gateway/interface/files.js deleted file mode 100644 index 84b150f79f..0000000000 --- a/test/gateway/interface/files.js +++ /dev/null @@ -1,20 +0,0 @@ -/* eslint-env mocha */ - -'use strict' - -const test = require('interface-ipfs-core') -const FactoryClient = require('./../../utils/ipfs-factory-daemon') - -let fc - -const common = { - setup: function (callback) { - fc = new FactoryClient() - callback(null, fc) - }, - teardown: function (callback) { - fc.dismantle(callback) - } -} - -test.files(common) diff --git a/test/gateway/interface/object.js b/test/gateway/interface/object.js deleted file mode 100644 index e00df72564..0000000000 --- a/test/gateway/interface/object.js +++ /dev/null @@ -1,20 +0,0 @@ -/* eslint-env mocha */ - -'use strict' - -const test = require('interface-ipfs-core') -const FactoryClient = require('./../../utils/ipfs-factory-daemon') - -let fc - -const common = { - setup: function (callback) { - fc = new FactoryClient() - callback(null, fc) - }, - teardown: function (callback) { - fc.dismantle(callback) - } -} - -test.object(common) diff --git a/test/gateway/interface/pubsub.js b/test/gateway/interface/pubsub.js deleted file mode 100644 index 968227c83b..0000000000 --- a/test/gateway/interface/pubsub.js +++ /dev/null @@ -1,23 +0,0 @@ -/* eslint-env mocha */ - -'use strict' - -// TODO needs: https://github.com/ipfs/js-ipfs-api/pull/493 -/* -const test = require('interface-ipfs-core') -const FactoryClient = require('./../../utils/ipfs-factory-daemon') - -let fc - -const common = { - setup: function (callback) { - fc = new FactoryClient() - callback(null, fc) - }, - teardown: function (callback) { - fc.dismantle(callback) - } -} - -test.pubsub(common) -*/ diff --git a/test/gateway/interface/swarm.js b/test/gateway/interface/swarm.js deleted file mode 100644 index eeca079124..0000000000 --- a/test/gateway/interface/swarm.js +++ /dev/null @@ -1,20 +0,0 @@ -/* eslint-env mocha */ - -'use strict' - -const test = require('interface-ipfs-core') -const FactoryClient = require('./../../utils/ipfs-factory-daemon') - -let fc - -const common = { - setup: function (callback) { - fc = new FactoryClient() - callback(null, fc) - }, - teardown: function (callback) { - fc.dismantle(callback) - } -} - -test.swarm(common)