|
| 1 | +/* eslint-env mocha */ |
| 2 | +/* eslint max-nested-callbacks: ["error", 8] */ |
| 3 | +'use strict' |
| 4 | + |
| 5 | +const expect = require('chai').expect |
| 6 | +const FormData = require('form-data') |
| 7 | +const streamToPromise = require('stream-to-promise') |
| 8 | +const multibase = require('multibase') |
| 9 | + |
| 10 | +module.exports = (http) => { |
| 11 | + describe('resolve', () => { |
| 12 | + let api |
| 13 | + |
| 14 | + before(() => { |
| 15 | + api = http.api.server.select('API') |
| 16 | + }) |
| 17 | + |
| 18 | + it('should resolve a path and return a base2 encoded CID', done => { |
| 19 | + const form = new FormData() |
| 20 | + form.append('data', Buffer.from('TEST' + Date.now())) |
| 21 | + const headers = form.getHeaders() |
| 22 | + |
| 23 | + streamToPromise(form).then((payload) => { |
| 24 | + api.inject({ |
| 25 | + method: 'POST', |
| 26 | + url: '/api/v0/add', |
| 27 | + headers: headers, |
| 28 | + payload: payload |
| 29 | + }, (res) => { |
| 30 | + expect(res.statusCode).to.equal(200) |
| 31 | + const hash = JSON.parse(res.result).Hash |
| 32 | + |
| 33 | + api.inject({ |
| 34 | + method: 'POST', |
| 35 | + url: `/api/v0/resolve?arg=/ipfs/${hash}&cid-base=base2` |
| 36 | + }, (res) => { |
| 37 | + expect(res.statusCode).to.equal(200) |
| 38 | + expect(multibase.isEncoded(res.result.Path.replace('/ipfs/', ''))).to.deep.equal('base2') |
| 39 | + done() |
| 40 | + }) |
| 41 | + }) |
| 42 | + }) |
| 43 | + }) |
| 44 | + |
| 45 | + it('should not resolve a path for invalid cid-base option', done => { |
| 46 | + const form = new FormData() |
| 47 | + form.append('data', Buffer.from('TEST' + Date.now())) |
| 48 | + const headers = form.getHeaders() |
| 49 | + |
| 50 | + streamToPromise(form).then((payload) => { |
| 51 | + api.inject({ |
| 52 | + method: 'POST', |
| 53 | + url: '/api/v0/add', |
| 54 | + headers: headers, |
| 55 | + payload: payload |
| 56 | + }, (res) => { |
| 57 | + expect(res.statusCode).to.equal(200) |
| 58 | + const hash = JSON.parse(res.result).Hash |
| 59 | + |
| 60 | + api.inject({ |
| 61 | + method: 'POST', |
| 62 | + url: `/api/v0/resolve?arg=/ipfs/${hash}&cid-base=invalid` |
| 63 | + }, (res) => { |
| 64 | + expect(res.statusCode).to.equal(400) |
| 65 | + expect(res.result.Message).to.include('child "cid-base" fails') |
| 66 | + done() |
| 67 | + }) |
| 68 | + }) |
| 69 | + }) |
| 70 | + }) |
| 71 | + }) |
| 72 | +} |
0 commit comments