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

Commit 31f673d

Browse files
committed
feat(http): Refactor inject tests, made them all pass again
1 parent df02596 commit 31f673d

22 files changed

+1289
-10
lines changed

src/http-api/resources/swarm.js

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const debug = require('debug')
44
const log = debug('http-api:block')
55
log.error = debug('http-api:block:error')
6+
const multiaddr = require('multiaddr')
67

78
exports = module.exports
89

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

16+
try {
17+
multiaddr(request.query.arg)
18+
} catch (err) {
19+
return reply("Argument 'addr' is invalid").code(500).takeover()
20+
}
21+
1522
return reply({
1623
addr: request.query.arg
1724
})

test/http-api/index.js

+21-8
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,50 @@ const ncp = require('ncp').ncp
88
const path = require('path')
99
const clean = require('../utils/clean')
1010

11-
describe('http api', () => {
11+
describe('HTTP API', () => {
1212
const repoExample = path.join(__dirname, '../go-ipfs-repo')
13-
const repoTests = exports.repoPath = path.join(__dirname, '../repo-tests-run-http')
14-
const api = new Api(repoTests)
13+
const repoTests = path.join(__dirname, '../repo-tests-run-http')
14+
15+
let http = {}
1516

1617
before((done) => {
18+
http.api = new Api(repoTests)
19+
1720
clean(repoTests)
1821
ncp(repoExample, repoTests, (err) => {
1922
expect(err).to.not.exist
2023

21-
api.start((err) => {
24+
http.api.start((err) => {
2225
expect(err).to.not.exist
2326
done()
2427
})
2528
})
2629
})
2730

2831
after((done) => {
29-
api.stop((err) => {
32+
http.api.stop((err) => {
3033
expect(err).to.not.exist
3134
clean(repoTests)
3235
done()
3336
})
3437
})
3538

36-
describe('--all', () => {
37-
var tests = fs.readdirSync(__dirname)
39+
describe('## inject', () => {
40+
const tests = fs.readdirSync(path.join(__dirname, '/inject'))
41+
3842
tests.filter((file) => {
3943
return file.match(/test-.*\.js/)
4044
}).forEach((file) => {
41-
require('./' + file)(api)
45+
require('./inject/' + file)(http)
4246
})
4347
})
48+
49+
// it.skip('## ipfs-api + interface-ipfs-core', () => {
50+
// const tests = fs.readdirSync(path.join(__dirname, '/ipfs-api'))
51+
// tests.filter((file) => {
52+
// return file.match(/test-.*\.js/)
53+
// }).forEach((file) => {
54+
// require('./ipfs-api/' + file)(http)
55+
// })
56+
// })
4457
})

test/http-api/inject/test-bitswap.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const expect = require('chai').expect
5+
6+
module.exports = (http) => {
7+
describe('/bitswap', () => {
8+
let api
9+
10+
before(() => {
11+
api = http.api.server.select('API')
12+
})
13+
14+
it('/wantlist', (done) => {
15+
api.inject({
16+
method: 'GET',
17+
url: '/api/v0/bitswap/wantlist'
18+
}, (res) => {
19+
expect(res.statusCode).to.equal(200)
20+
expect(res.result).to.have.property('Keys')
21+
// TODO test that there actual values in there
22+
done()
23+
})
24+
})
25+
26+
it('/stat', (done) => {
27+
api.inject({
28+
method: 'GET',
29+
url: '/api/v0/bitswap/stat'
30+
}, (res) => {
31+
expect(res.statusCode).to.equal(200)
32+
33+
expect(res.result).to.have.keys([
34+
'BlocksReceived',
35+
'Wantlist',
36+
'Peers',
37+
'DupBlksReceived',
38+
'DupDataReceived'
39+
])
40+
// TODO test that there actual values in there
41+
done()
42+
})
43+
})
44+
45+
it.skip('/unwant', () => {
46+
})
47+
})
48+
}

test/http-api/inject/test-block.js

+170
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const expect = require('chai').expect
5+
const fs = require('fs')
6+
const FormData = require('form-data')
7+
const streamToPromise = require('stream-to-promise')
8+
9+
module.exports = (http) => {
10+
describe('/block', () => {
11+
let api
12+
13+
before(() => {
14+
api = http.api.server.select('API')
15+
})
16+
17+
describe('/block/put', () => {
18+
it('returns 400 if no node is provided', (done) => {
19+
const form = new FormData()
20+
const headers = form.getHeaders()
21+
22+
streamToPromise(form).then((payload) => {
23+
api.inject({
24+
method: 'POST',
25+
url: '/api/v0/block/put',
26+
headers: headers,
27+
payload: payload
28+
}, (res) => {
29+
expect(res.statusCode).to.equal(400)
30+
done()
31+
})
32+
})
33+
})
34+
35+
it('updates value', (done) => {
36+
const form = new FormData()
37+
const filePath = 'test/test-data/hello'
38+
form.append('data', fs.createReadStream(filePath))
39+
const headers = form.getHeaders()
40+
const expectedResult = {
41+
Key: 'QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp',
42+
Size: 12
43+
}
44+
45+
streamToPromise(form).then((payload) => {
46+
api.inject({
47+
method: 'POST',
48+
url: '/api/v0/block/put',
49+
headers: headers,
50+
payload: payload
51+
}, (res) => {
52+
expect(res.statusCode).to.equal(200)
53+
expect(res.result).to.deep.equal(expectedResult)
54+
done()
55+
})
56+
})
57+
})
58+
})
59+
60+
describe('/block/get', () => {
61+
it('returns 400 for request without argument', (done) => {
62+
api.inject({
63+
method: 'GET',
64+
url: '/api/v0/block/get'
65+
}, (res) => {
66+
expect(res.statusCode).to.equal(400)
67+
expect(res.result).to.be.a('string')
68+
done()
69+
})
70+
})
71+
72+
it('returns 500 for request with invalid argument', (done) => {
73+
api.inject({
74+
method: 'GET',
75+
url: '/api/v0/block/get?arg=invalid'
76+
}, (res) => {
77+
expect(res.statusCode).to.equal(500)
78+
expect(res.result.Code).to.equal(0)
79+
expect(res.result.Message).to.be.a('string')
80+
done()
81+
})
82+
})
83+
84+
it('returns value', (done) => {
85+
api.inject({
86+
method: 'GET',
87+
url: '/api/v0/block/get?arg=QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp'
88+
}, (res) => {
89+
expect(res.statusCode).to.equal(200)
90+
expect(res.result)
91+
.to.equal('hello world\n')
92+
done()
93+
})
94+
})
95+
})
96+
97+
describe('/block/stat', () => {
98+
it('returns 400 for request without argument', (done) => {
99+
api.inject({
100+
method: 'GET',
101+
url: '/api/v0/block/stat'
102+
}, (res) => {
103+
expect(res.statusCode).to.equal(400)
104+
expect(res.result).to.be.a('string')
105+
done()
106+
})
107+
})
108+
109+
it('returns 500 for request with invalid argument', (done) => {
110+
api.inject({
111+
method: 'GET',
112+
url: '/api/v0/block/stat?arg=invalid'
113+
}, (res) => {
114+
expect(res.statusCode).to.equal(500)
115+
expect(res.result.Code).to.equal(0)
116+
expect(res.result.Message).to.be.a('string')
117+
done()
118+
})
119+
})
120+
121+
it('returns value', (done) => {
122+
api.inject({
123+
method: 'GET',
124+
url: '/api/v0/block/stat?arg=QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp'
125+
}, (res) => {
126+
expect(res.statusCode).to.equal(200)
127+
expect(res.result.Key)
128+
.to.equal('QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp')
129+
expect(res.result.Size).to.equal(12)
130+
done()
131+
})
132+
})
133+
})
134+
135+
describe('/block/del', () => {
136+
it('returns 400 for request without argument', (done) => {
137+
api.inject({
138+
method: 'GET',
139+
url: '/api/v0/block/del'
140+
}, (res) => {
141+
expect(res.statusCode).to.equal(400)
142+
expect(res.result).to.be.a('string')
143+
done()
144+
})
145+
})
146+
147+
it('returns 500 for request with invalid argument', (done) => {
148+
api.inject({
149+
method: 'GET',
150+
url: '/api/v0/block/del?arg=invalid'
151+
}, (res) => {
152+
expect(res.statusCode).to.equal(500)
153+
expect(res.result.Code).to.equal(0)
154+
expect(res.result.Message).to.be.a('string')
155+
done()
156+
})
157+
})
158+
159+
it('returns 200', (done) => {
160+
api.inject({
161+
method: 'GET',
162+
url: '/api/v0/block/del?arg=QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp'
163+
}, (res) => {
164+
expect(res.statusCode).to.equal(200)
165+
done()
166+
})
167+
})
168+
})
169+
})
170+
}
+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const expect = require('chai').expect
5+
6+
module.exports = (http) => {
7+
describe('/bootstrap', () => {
8+
let api
9+
10+
before(() => {
11+
api = http.api.server.select('API')
12+
})
13+
14+
it('/list', (done) => {
15+
api.inject({
16+
method: 'GET',
17+
url: '/api/v0/bootstrap/list'
18+
}, (res) => {
19+
expect(res.result).to.deep.equal(defaultList)
20+
done()
21+
})
22+
})
23+
24+
it('/list alias', (done) => {
25+
api.inject({
26+
method: 'GET',
27+
url: '/api/v0/bootstrap'
28+
}, (res) => {
29+
expect(res.result).to.deep.equal(defaultList)
30+
done()
31+
})
32+
})
33+
34+
it.skip('/add', (done) => { // TODO
35+
api.inject({
36+
method: 'GET',
37+
url: '/api/v0/bootstrap/add',
38+
payload: {
39+
arg: '/ip4/111.111.111.111/tcp/1001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLUVIT'
40+
}
41+
}, (res) => {
42+
// TODO assess
43+
})
44+
})
45+
46+
it.skip('/rm', (done) => { // TODO
47+
api.inject({
48+
method: 'GET',
49+
url: '/api/v0/bootstrap/rm',
50+
payload: {
51+
arg: '/ip4/111.111.111.111/tcp/1001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLUVIT'
52+
}
53+
}, (res) => {
54+
// TODO assess
55+
})
56+
})
57+
58+
it.skip('/list confirm it changed', (done) => { // TODO
59+
api.inject({
60+
method: 'GET',
61+
url: '/api/v0/bootstrap/list'
62+
}, (res) => {
63+
// TODO assess
64+
})
65+
})
66+
})
67+
}
68+
69+
const defaultList = [
70+
'/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ',
71+
'/ip4/104.236.176.52/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z',
72+
'/ip4/104.236.179.241/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
73+
'/ip4/162.243.248.213/tcp/4001/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
74+
'/ip4/128.199.219.111/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
75+
'/ip4/104.236.76.40/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
76+
'/ip4/178.62.158.247/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
77+
'/ip4/178.62.61.185/tcp/4001/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
78+
'/ip4/104.236.151.122/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx'
79+
]

0 commit comments

Comments
 (0)