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

Commit cd0fae9

Browse files
ya7yadaviddias
authored andcommitted
clean up, remove commented out lines. (#971)
* clean up, remove commented out lines. * cleaning code, removing commented out blocks. * gateway initial tests.
1 parent c0303fa commit cd0fae9

File tree

3 files changed

+39
-198
lines changed

3 files changed

+39
-198
lines changed

src/http/api/resources/files.js

-130
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ const toPull = require('stream-to-pull-stream')
1111
const pushable = require('pull-pushable')
1212
const EOL = require('os').EOL
1313
const toStream = require('pull-stream-to-stream')
14-
// const fileType = require('file-type')
15-
// const mime = require('mime-types')
16-
// const GatewayResolver = require('../gateway/resolver')
17-
// const PathUtils = require('../gateway/utils/path')
18-
// const Stream = require('stream')
1914

2015
exports = module.exports
2116

@@ -218,128 +213,3 @@ exports.add = {
218213
)
219214
}
220215
}
221-
222-
// exports.gateway = {
223-
// checkHash: (request, reply) => {
224-
// if (!request.params.hash) {
225-
// return reply('Path Resolve error: path must contain at least one component').code(400).takeover()
226-
// }
227-
//
228-
// return reply({
229-
// ref: `/ipfs/${request.params.hash}`
230-
// })
231-
// },
232-
// handler: (request, reply) => {
233-
// const ref = request.pre.args.ref
234-
// const ipfs = request.server.app.ipfs
235-
//
236-
// return GatewayResolver
237-
// .resolveMultihash(ipfs, ref)
238-
// .then((data) => {
239-
// ipfs
240-
// .files
241-
// .cat(data.multihash)
242-
// .then((stream) => {
243-
// if (ref.endsWith('/')) {
244-
// // remove trailing slash for files
245-
// return reply
246-
// .redirect(PathUtils.removeTrailingSlash(ref))
247-
// .permanent(true)
248-
// } else {
249-
// if (!stream._read) {
250-
// stream._read = () => {}
251-
// stream._readableState = {}
252-
// }
253-
// // response.continue()
254-
// let filetypeChecked = false
255-
// let stream2 = new Stream.PassThrough({highWaterMark: 1})
256-
// let response = reply(stream2).hold()
257-
//
258-
// pull(
259-
// toPull.source(stream),
260-
// pull.drain((chunk) => {
261-
// // Check file type. do this once.
262-
// if (chunk.length > 0 && !filetypeChecked) {
263-
// console.log('got first chunk')
264-
// let fileSignature = fileType(chunk)
265-
// console.log('file type: ', fileSignature)
266-
//
267-
// filetypeChecked = true
268-
// const mimeType = mime.lookup((fileSignature) ? fileSignature.ext : null)
269-
// console.log('ref ', ref)
270-
// console.log('mime-type ', mimeType)
271-
//
272-
// if (mimeType) {
273-
// console.log('writing mimeType')
274-
//
275-
// response
276-
// .header('Content-Type', mime.contentType(mimeType))
277-
// .header('Access-Control-Allow-Headers', 'X-Stream-Output, X-Chunked-Ouput')
278-
// .header('Access-Control-Allow-Methods', 'GET')
279-
// .header('Access-Control-Allow-Origin', '*')
280-
// .header('Access-Control-Expose-Headers', 'X-Stream-Output, X-Chunked-Ouput')
281-
// .send()
282-
// } else {
283-
// response
284-
// .header('Access-Control-Allow-Headers', 'X-Stream-Output, X-Chunked-Ouput')
285-
// .header('Access-Control-Allow-Methods', 'GET')
286-
// .header('Access-Control-Allow-Origin', '*')
287-
// .header('Access-Control-Expose-Headers', 'X-Stream-Output, X-Chunked-Ouput')
288-
// .send()
289-
// }
290-
// }
291-
//
292-
// stream2.write(chunk)
293-
// }, (err) => {
294-
// if (err) throw err
295-
// console.log('stream ended.')
296-
// stream2.end()
297-
// })
298-
// )
299-
// }
300-
// })
301-
// .catch((err) => {
302-
// if (err) {
303-
// log.error(err)
304-
// return reply(err.toString()).code(500)
305-
// }
306-
// })
307-
// }).catch((err) => {
308-
// console.log('err: ', err.toString(), ' fileName: ', err.fileName)
309-
//
310-
// const errorToString = err.toString()
311-
// if (errorToString === 'Error: This dag node is a directory') {
312-
// return GatewayResolver
313-
// .resolveDirectory(ipfs, ref, err.fileName)
314-
// .then((data) => {
315-
// if (typeof data === 'string') {
316-
// // no index file found
317-
// if (!ref.endsWith('/')) {
318-
// // for a directory, if URL doesn't end with a /
319-
// // append / and redirect permanent to that URL
320-
// return reply.redirect(`${ref}/`).permanent(true)
321-
// } else {
322-
// // send directory listing
323-
// return reply(data)
324-
// }
325-
// } else {
326-
// // found index file
327-
// // redirect to URL/<found-index-file>
328-
// return reply.redirect(PathUtils.joinURLParts(ref, data[0].name))
329-
// }
330-
// }).catch((err) => {
331-
// log.error(err)
332-
// return reply(err.toString()).code(500)
333-
// })
334-
// } else if (errorToString.startsWith('Error: no link named')) {
335-
// return reply(errorToString).code(404)
336-
// } else if (errorToString.startsWith('Error: multihash length inconsistent') ||
337-
// errorToString.startsWith('Error: Non-base58 character')) {
338-
// return reply(errorToString).code(400)
339-
// } else {
340-
// log.error(err)
341-
// return reply(errorToString).code(500)
342-
// }
343-
// })
344-
// }
345-
// }

test/gateway/index.js

+39-18
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const fs = require('fs')
54
const chai = require('chai')
65
const dirtyChai = require('dirty-chai')
76
const expect = chai.expect
87
chai.use(dirtyChai)
98
const API = require('../../src/http')
10-
// const APIctl = require('ipfs-api')
119
const ncp = require('ncp').ncp
1210
const path = require('path')
1311
const clean = require('../utils/clean')
@@ -17,14 +15,18 @@ describe('HTTP GATEWAY', () => {
1715
const repoTests = path.join(__dirname, '../repo-tests-run')
1816

1917
let http = {}
18+
let gateway
2019

2120
before((done) => {
2221
http.api = new API(repoTests)
2322

2423
ncp(repoExample, repoTests, (err) => {
2524
expect(err).to.not.exist()
2625

27-
http.api.start(false, done)
26+
http.api.start(false, () => {
27+
gateway = http.api.server.select('Gateway')
28+
done()
29+
})
2830
})
2931
})
3032

@@ -36,20 +38,39 @@ describe('HTTP GATEWAY', () => {
3638
})
3739
})
3840

39-
describe('## http-gateway spec tests', () => {
40-
fs.readdirSync(path.join(__dirname, '/spec'))
41-
.forEach((file) => require('./spec/' + file)(http))
42-
})
41+
describe('/ipfs/* route', () => {
42+
it('returns 400 for request without argument', (done) => {
43+
gateway.inject({
44+
method: 'GET',
45+
url: '/ipfs'
46+
}, (res) => {
47+
expect(res.statusCode).to.equal(400)
48+
expect(res.result.Message).to.be.a('string')
49+
done()
50+
})
51+
})
52+
53+
it('400 for request with invalid argument', (done) => {
54+
gateway.inject({
55+
method: 'GET',
56+
url: '/ipfs/invalid'
57+
}, (res) => {
58+
expect(res.statusCode).to.equal(400)
59+
expect(res.result.Message).to.be.a('string')
60+
done()
61+
})
62+
})
4363

44-
// describe('## interface tests', () => {
45-
// fs.readdirSync(path.join(__dirname, '/interface'))
46-
// .forEach((file) => require('./interface/' + file))
47-
// })
48-
//
49-
// describe('## custom ipfs-api tests', () => {
50-
// const ctl = APIctl('/ip4/127.0.0.1/tcp/6001')
51-
//
52-
// fs.readdirSync(path.join(__dirname, '/over-ipfs-api'))
53-
// .forEach((file) => require('./over-ipfs-api/' + file)(ctl))
54-
// })
64+
it('valid hash', (done) => {
65+
gateway.inject({
66+
method: 'GET',
67+
url: '/ipfs/QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o'
68+
}, (res) => {
69+
expect(res.statusCode).to.equal(200)
70+
expect(res.rawPayload).to.deep.equal(new Buffer('hello world' + '\n'))
71+
expect(res.payload).to.equal('hello world' + '\n')
72+
done()
73+
})
74+
})
75+
})
5576
})

test/gateway/spec/gateway.js

-50
This file was deleted.

0 commit comments

Comments
 (0)