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

Commit 9a43cc8

Browse files
author
Alan Shaw
committed
test: add HTTP API tests for block.stat with cid-base option
License: MIT Signed-off-by: Alan Shaw <[email protected]>
1 parent 1763d0e commit 9a43cc8

File tree

2 files changed

+48
-16
lines changed

2 files changed

+48
-16
lines changed

src/http/api/routes/block.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ module.exports = (server) => {
1212
pre: [
1313
{ method: resources.block.get.parseArgs, assign: 'args' }
1414
],
15-
handler: resources.block.get.handler,
16-
validate: resources.block.get.validate
15+
handler: resources.block.get.handler
1716
}
1817
})
1918

test/http-api/inject/block.js

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ const expect = require('chai').expect
66
const fs = require('fs')
77
const FormData = require('form-data')
88
const streamToPromise = require('stream-to-promise')
9+
const multibase = require('multibase')
910

1011
module.exports = (http) => {
11-
describe('/block', () => {
12+
describe.only('/block', () => {
1213
let api
1314

1415
before(() => {
@@ -59,13 +60,8 @@ module.exports = (http) => {
5960

6061
it('should put a value and return a base64 encoded CID', (done) => {
6162
const form = new FormData()
62-
const filePath = 'test/fixtures/test-data/hello'
63-
form.append('data', fs.createReadStream(filePath))
63+
form.append('data', Buffer.from('TEST' + Date.now()))
6464
const headers = form.getHeaders()
65-
const expectedResult = {
66-
Key: 'mAXASIKlIkE8vD0ebj4GXaUswGEsNLtHBzSoewPuF0pmhkqRH',
67-
Size: 12
68-
}
6965

7066
streamToPromise(form).then((payload) => {
7167
api.inject({
@@ -75,16 +71,15 @@ module.exports = (http) => {
7571
payload: payload
7672
}, (res) => {
7773
expect(res.statusCode).to.equal(200)
78-
expect(res.result).to.deep.equal(expectedResult)
74+
expect(multibase.isEncoded(res.result.Key)).to.deep.equal('base64')
7975
done()
8076
})
8177
})
8278
})
8379

8480
it('should not put a value for invalid cid-base option', (done) => {
8581
const form = new FormData()
86-
const filePath = 'test/fixtures/test-data/hello'
87-
form.append('data', fs.createReadStream(filePath))
82+
form.append('data', Buffer.from('TEST' + Date.now()))
8883
const headers = form.getHeaders()
8984

9085
streamToPromise(form).then((payload) => {
@@ -175,13 +170,51 @@ module.exports = (http) => {
175170
done()
176171
})
177172
})
173+
174+
it('should stat a block and return a base64 encoded CID', (done) => {
175+
const form = new FormData()
176+
form.append('data', Buffer.from('TEST' + Date.now()))
177+
const headers = form.getHeaders()
178+
179+
streamToPromise(form).then((payload) => {
180+
api.inject({
181+
method: 'POST',
182+
url: '/api/v0/block/put?cid-base=base64',
183+
headers: headers,
184+
payload: payload
185+
}, (res) => {
186+
expect(res.statusCode).to.equal(200)
187+
expect(multibase.isEncoded(res.result.Key)).to.deep.equal('base64')
188+
done()
189+
})
190+
})
191+
})
192+
193+
it('should not stat a block for invalid cid-base option', (done) => {
194+
const form = new FormData()
195+
form.append('data', Buffer.from('TEST' + Date.now()))
196+
const headers = form.getHeaders()
197+
198+
streamToPromise(form).then((payload) => {
199+
api.inject({
200+
method: 'POST',
201+
url: '/api/v0/block/put?cid-base=invalid',
202+
headers: headers,
203+
payload: payload
204+
}, (res) => {
205+
expect(res.statusCode).to.equal(400)
206+
expect(res.result.Message).to.include('child "cid-base" fails')
207+
done()
208+
})
209+
})
210+
})
178211
})
179212

180-
describe('/block/del', () => {
213+
describe('/block/rm', () => {
181214
it('returns 400 for request without argument', (done) => {
182215
api.inject({
183216
method: 'GET',
184-
url: '/api/v0/block/del'
217+
url: '/api/v0/block/rm'
185218
}, (res) => {
186219
expect(res.statusCode).to.equal(400)
187220
expect(res.result).to.be.a('string')
@@ -192,7 +225,7 @@ module.exports = (http) => {
192225
it('returns 500 for request with invalid argument', (done) => {
193226
api.inject({
194227
method: 'GET',
195-
url: '/api/v0/block/del?arg=invalid'
228+
url: '/api/v0/block/rm?arg=invalid'
196229
}, (res) => {
197230
expect(res.statusCode).to.equal(500)
198231
expect(res.result.Code).to.equal(0)
@@ -204,7 +237,7 @@ module.exports = (http) => {
204237
it('returns 200', (done) => {
205238
api.inject({
206239
method: 'GET',
207-
url: '/api/v0/block/del?arg=QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp'
240+
url: '/api/v0/block/rm?arg=QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp'
208241
}, (res) => {
209242
expect(res.statusCode).to.equal(200)
210243
done()

0 commit comments

Comments
 (0)