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

Commit

Permalink
feat(http): Refactor inject tests, made them all pass again
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Aug 20, 2016
1 parent df02596 commit 31f673d
Show file tree
Hide file tree
Showing 22 changed files with 1,289 additions and 10 deletions.
7 changes: 7 additions & 0 deletions src/http-api/resources/swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const debug = require('debug')
const log = debug('http-api:block')
log.error = debug('http-api:block:error')
const multiaddr = require('multiaddr')

exports = module.exports

Expand All @@ -12,6 +13,12 @@ exports.parseAddrs = (request, reply) => {
return reply("Argument 'addr' is required").code(400).takeover()
}

try {
multiaddr(request.query.arg)
} catch (err) {
return reply("Argument 'addr' is invalid").code(500).takeover()
}

return reply({
addr: request.query.arg
})
Expand Down
29 changes: 21 additions & 8 deletions test/http-api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,50 @@ const ncp = require('ncp').ncp
const path = require('path')
const clean = require('../utils/clean')

describe('http api', () => {
describe('HTTP API', () => {
const repoExample = path.join(__dirname, '../go-ipfs-repo')
const repoTests = exports.repoPath = path.join(__dirname, '../repo-tests-run-http')
const api = new Api(repoTests)
const repoTests = path.join(__dirname, '../repo-tests-run-http')

let http = {}

before((done) => {
http.api = new Api(repoTests)

clean(repoTests)
ncp(repoExample, repoTests, (err) => {
expect(err).to.not.exist

api.start((err) => {
http.api.start((err) => {
expect(err).to.not.exist
done()
})
})
})

after((done) => {
api.stop((err) => {
http.api.stop((err) => {
expect(err).to.not.exist
clean(repoTests)
done()
})
})

describe('--all', () => {
var tests = fs.readdirSync(__dirname)
describe('## inject', () => {
const tests = fs.readdirSync(path.join(__dirname, '/inject'))

tests.filter((file) => {
return file.match(/test-.*\.js/)
}).forEach((file) => {
require('./' + file)(api)
require('./inject/' + file)(http)
})
})

// it.skip('## ipfs-api + interface-ipfs-core', () => {
// const tests = fs.readdirSync(path.join(__dirname, '/ipfs-api'))
// tests.filter((file) => {
// return file.match(/test-.*\.js/)
// }).forEach((file) => {
// require('./ipfs-api/' + file)(http)
// })
// })
})
48 changes: 48 additions & 0 deletions test/http-api/inject/test-bitswap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* eslint-env mocha */
'use strict'

const expect = require('chai').expect

module.exports = (http) => {
describe('/bitswap', () => {
let api

before(() => {
api = http.api.server.select('API')
})

it('/wantlist', (done) => {
api.inject({
method: 'GET',
url: '/api/v0/bitswap/wantlist'
}, (res) => {
expect(res.statusCode).to.equal(200)
expect(res.result).to.have.property('Keys')
// TODO test that there actual values in there
done()
})
})

it('/stat', (done) => {
api.inject({
method: 'GET',
url: '/api/v0/bitswap/stat'
}, (res) => {
expect(res.statusCode).to.equal(200)

expect(res.result).to.have.keys([
'BlocksReceived',
'Wantlist',
'Peers',
'DupBlksReceived',
'DupDataReceived'
])
// TODO test that there actual values in there
done()
})
})

it.skip('/unwant', () => {
})
})
}
170 changes: 170 additions & 0 deletions test/http-api/inject/test-block.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/* eslint-env mocha */
'use strict'

const expect = require('chai').expect
const fs = require('fs')
const FormData = require('form-data')
const streamToPromise = require('stream-to-promise')

module.exports = (http) => {
describe('/block', () => {
let api

before(() => {
api = http.api.server.select('API')
})

describe('/block/put', () => {
it('returns 400 if no node is provided', (done) => {
const form = new FormData()
const headers = form.getHeaders()

streamToPromise(form).then((payload) => {
api.inject({
method: 'POST',
url: '/api/v0/block/put',
headers: headers,
payload: payload
}, (res) => {
expect(res.statusCode).to.equal(400)
done()
})
})
})

it('updates value', (done) => {
const form = new FormData()
const filePath = 'test/test-data/hello'
form.append('data', fs.createReadStream(filePath))
const headers = form.getHeaders()
const expectedResult = {
Key: 'QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp',
Size: 12
}

streamToPromise(form).then((payload) => {
api.inject({
method: 'POST',
url: '/api/v0/block/put',
headers: headers,
payload: payload
}, (res) => {
expect(res.statusCode).to.equal(200)
expect(res.result).to.deep.equal(expectedResult)
done()
})
})
})
})

describe('/block/get', () => {
it('returns 400 for request without argument', (done) => {
api.inject({
method: 'GET',
url: '/api/v0/block/get'
}, (res) => {
expect(res.statusCode).to.equal(400)
expect(res.result).to.be.a('string')
done()
})
})

it('returns 500 for request with invalid argument', (done) => {
api.inject({
method: 'GET',
url: '/api/v0/block/get?arg=invalid'
}, (res) => {
expect(res.statusCode).to.equal(500)
expect(res.result.Code).to.equal(0)
expect(res.result.Message).to.be.a('string')
done()
})
})

it('returns value', (done) => {
api.inject({
method: 'GET',
url: '/api/v0/block/get?arg=QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp'
}, (res) => {
expect(res.statusCode).to.equal(200)
expect(res.result)
.to.equal('hello world\n')
done()
})
})
})

describe('/block/stat', () => {
it('returns 400 for request without argument', (done) => {
api.inject({
method: 'GET',
url: '/api/v0/block/stat'
}, (res) => {
expect(res.statusCode).to.equal(400)
expect(res.result).to.be.a('string')
done()
})
})

it('returns 500 for request with invalid argument', (done) => {
api.inject({
method: 'GET',
url: '/api/v0/block/stat?arg=invalid'
}, (res) => {
expect(res.statusCode).to.equal(500)
expect(res.result.Code).to.equal(0)
expect(res.result.Message).to.be.a('string')
done()
})
})

it('returns value', (done) => {
api.inject({
method: 'GET',
url: '/api/v0/block/stat?arg=QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp'
}, (res) => {
expect(res.statusCode).to.equal(200)
expect(res.result.Key)
.to.equal('QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp')
expect(res.result.Size).to.equal(12)
done()
})
})
})

describe('/block/del', () => {
it('returns 400 for request without argument', (done) => {
api.inject({
method: 'GET',
url: '/api/v0/block/del'
}, (res) => {
expect(res.statusCode).to.equal(400)
expect(res.result).to.be.a('string')
done()
})
})

it('returns 500 for request with invalid argument', (done) => {
api.inject({
method: 'GET',
url: '/api/v0/block/del?arg=invalid'
}, (res) => {
expect(res.statusCode).to.equal(500)
expect(res.result.Code).to.equal(0)
expect(res.result.Message).to.be.a('string')
done()
})
})

it('returns 200', (done) => {
api.inject({
method: 'GET',
url: '/api/v0/block/del?arg=QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp'
}, (res) => {
expect(res.statusCode).to.equal(200)
done()
})
})
})
})
}
79 changes: 79 additions & 0 deletions test/http-api/inject/test-bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* eslint-env mocha */
'use strict'

const expect = require('chai').expect

module.exports = (http) => {
describe('/bootstrap', () => {
let api

before(() => {
api = http.api.server.select('API')
})

it('/list', (done) => {
api.inject({
method: 'GET',
url: '/api/v0/bootstrap/list'
}, (res) => {
expect(res.result).to.deep.equal(defaultList)
done()
})
})

it('/list alias', (done) => {
api.inject({
method: 'GET',
url: '/api/v0/bootstrap'
}, (res) => {
expect(res.result).to.deep.equal(defaultList)
done()
})
})

it.skip('/add', (done) => { // TODO
api.inject({
method: 'GET',
url: '/api/v0/bootstrap/add',
payload: {
arg: '/ip4/111.111.111.111/tcp/1001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLUVIT'
}
}, (res) => {
// TODO assess
})
})

it.skip('/rm', (done) => { // TODO
api.inject({
method: 'GET',
url: '/api/v0/bootstrap/rm',
payload: {
arg: '/ip4/111.111.111.111/tcp/1001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLUVIT'
}
}, (res) => {
// TODO assess
})
})

it.skip('/list confirm it changed', (done) => { // TODO
api.inject({
method: 'GET',
url: '/api/v0/bootstrap/list'
}, (res) => {
// TODO assess
})
})
})
}

const defaultList = [
'/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ',
'/ip4/104.236.176.52/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z',
'/ip4/104.236.179.241/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
'/ip4/162.243.248.213/tcp/4001/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
'/ip4/128.199.219.111/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
'/ip4/104.236.76.40/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
'/ip4/178.62.158.247/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
'/ip4/178.62.61.185/tcp/4001/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
'/ip4/104.236.151.122/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx'
]
Loading

0 comments on commit 31f673d

Please sign in to comment.