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

Commit 513c4ba

Browse files
ya7yadaviddias
authored andcommitted
test: Essential Tests for Feat/gateway (#999)
* gateway Essential tests * removing interface tests from gateway
1 parent 5ed444c commit 513c4ba

File tree

7 files changed

+154
-7
lines changed

7 files changed

+154
-7
lines changed

examples/traverse-ipld-graphs/git.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ createNode((err, ipfs) => {
4747

4848
const v1tag = 'z8mWaGfwSWLMPJ6Q2JdsAjGiXTf61Nbue'
4949

50-
function errOrLog(comment) {
50+
function errOrLog (comment) {
5151
return (err, result) => {
5252
if (err) {
5353
throw err
5454
}
5555

56-
if (Buffer.isBuffer(result.value)) { //Blobs (files) are returned as buffer instance
56+
if (Buffer.isBuffer(result.value)) { // Blobs (files) are returned as buffer instance
5757
result.value = result.value.toString()
5858
}
5959

@@ -63,7 +63,6 @@ createNode((err, ipfs) => {
6363
}
6464
}
6565

66-
6766
ipfs.dag.get(v1tag + '/', errOrLog('Tag object:'))
6867
ipfs.dag.get(v1tag + '/object/message', errOrLog('Tagged commit message:'))
6968
ipfs.dag.get(v1tag + '/object/parents/0/message', errOrLog('Parent of tagged commit:'))

src/http/gateway/resolver.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const resolveDirectory = promisify((ipfs, path, multihash, callback) => {
3838
return callback(null, indexFiles)
3939
}
4040

41-
return callback(null, dirView.build(path, dagNode.links))
41+
return callback(null, dirView.render(path, dagNode.links))
4242
})
4343
})
4444

test/gateway/index.js

+129-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ const dirtyChai = require('dirty-chai')
66
const expect = chai.expect
77
chai.use(dirtyChai)
88
const API = require('../../src/http')
9+
const loadFixture = require('aegir/fixtures')
10+
const bigFile = loadFixture(__dirname, '../../node_modules/interface-ipfs-core/test/fixtures/15mb.random', 'ipfs')
11+
const directoryContent = {
12+
'index.html': loadFixture(__dirname, './test-folder/index.html', 'ipfs'),
13+
'nested-folder/hello.txt': loadFixture(__dirname, './test-folder/nested-folder/hello.txt', 'ipfs'),
14+
'nested-folder/ipfs.txt': loadFixture(__dirname, './test-folder/nested-folder/ipfs.txt', 'ipfs'),
15+
'nested-folder/nested.html': loadFixture(__dirname, './test-folder/nested-folder/nested.html', 'ipfs')
16+
}
917

1018
describe('HTTP Gateway', () => {
1119
let http = {}
@@ -15,8 +23,35 @@ describe('HTTP Gateway', () => {
1523
http.api = new API()
1624

1725
http.api.start(true, () => {
18-
gateway = http.api.server.select('Gateway')
19-
done()
26+
const content = (name) => ({
27+
path: `test-folder/${name}`,
28+
content: directoryContent[name]
29+
})
30+
31+
const emptyDir = (name) => ({
32+
path: `test-folder/${name}`
33+
})
34+
35+
const expectedRootMultihash = 'QmbQD7EMEL1zeebwBsWEfA3ndgSS6F7S6iTuwuqasPgVRi'
36+
37+
const dirs = [
38+
content('index.html'),
39+
emptyDir('empty-folder'),
40+
content('nested-folder/hello.txt'),
41+
content('nested-folder/ipfs.txt'),
42+
content('nested-folder/nested.html'),
43+
emptyDir('nested-folder/empty')
44+
]
45+
46+
http.api.node.files.add(dirs, (err, res) => {
47+
expect(err).to.not.exist()
48+
const root = res[res.length - 1]
49+
50+
expect(root.path).to.equal('test-folder')
51+
expect(root.hash).to.equal(expectedRootMultihash)
52+
gateway = http.api.server.select('Gateway')
53+
done()
54+
})
2055
})
2156
})
2257

@@ -27,7 +62,7 @@ describe('HTTP Gateway', () => {
2762
})
2863
})
2964

30-
describe('/ipfs/* route', () => {
65+
describe('## HTTP Gateway', () => {
3166
it('returns 400 for request without argument', (done) => {
3267
gateway.inject({
3368
method: 'GET',
@@ -61,5 +96,96 @@ describe('HTTP Gateway', () => {
6196
done()
6297
})
6398
})
99+
100+
it('stream a large file', (done) => {
101+
let bigFileHash = 'Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq'
102+
103+
gateway.inject({
104+
method: 'GET',
105+
url: '/ipfs/' + bigFileHash
106+
}, (res) => {
107+
expect(res.statusCode).to.equal(200)
108+
expect(res.rawPayload).to.deep.equal(bigFile)
109+
done()
110+
})
111+
})
112+
113+
it('load a non text file', (done) => {
114+
let kitty = 'QmW2WQi7j6c7UgJTarActp7tDNikE4B2qXtFCfLPdsgaTQ/cat.jpg'
115+
116+
gateway.inject({
117+
method: 'GET',
118+
url: '/ipfs/' + kitty
119+
}, (res) => {
120+
expect(res.statusCode).to.equal(200)
121+
expect(res.headers['content-type']).to.equal('image/jpeg')
122+
done()
123+
})
124+
})
125+
126+
it('load a directory', (done) => {
127+
let dir = 'QmW2WQi7j6c7UgJTarActp7tDNikE4B2qXtFCfLPdsgaTQ/'
128+
129+
gateway.inject({
130+
method: 'GET',
131+
url: '/ipfs/' + dir
132+
}, (res) => {
133+
expect(res.statusCode).to.equal(200)
134+
expect(res.headers['content-type']).to.equal('text/html; charset=utf-8')
135+
done()
136+
})
137+
})
138+
139+
it('load a webpage index.html', (done) => {
140+
let dir = 'QmbQD7EMEL1zeebwBsWEfA3ndgSS6F7S6iTuwuqasPgVRi/index.html'
141+
142+
gateway.inject({
143+
method: 'GET',
144+
url: '/ipfs/' + dir
145+
}, (res) => {
146+
expect(res.statusCode).to.equal(200)
147+
expect(res.rawPayload).to.deep.equal(directoryContent['index.html'])
148+
done()
149+
})
150+
})
151+
152+
it('load a webpage {hash}/nested-folder/nested.html', (done) => {
153+
let dir = 'QmbQD7EMEL1zeebwBsWEfA3ndgSS6F7S6iTuwuqasPgVRi/nested-folder/nested.html'
154+
155+
gateway.inject({
156+
method: 'GET',
157+
url: '/ipfs/' + dir
158+
}, (res) => {
159+
expect(res.statusCode).to.equal(200)
160+
expect(res.rawPayload).to.deep.equal(directoryContent['nested-folder/nested.html'])
161+
done()
162+
})
163+
})
164+
165+
it('redirect to generated index', (done) => {
166+
let dir = 'QmW2WQi7j6c7UgJTarActp7tDNikE4B2qXtFCfLPdsgaTQ'
167+
168+
gateway.inject({
169+
method: 'GET',
170+
url: '/ipfs/' + dir
171+
}, (res) => {
172+
expect(res.statusCode).to.equal(301)
173+
expect(res.headers['location']).to.equal('/ipfs/QmW2WQi7j6c7UgJTarActp7tDNikE4B2qXtFCfLPdsgaTQ/')
174+
done()
175+
})
176+
})
177+
178+
it('redirect to webpage index.html', (done) => {
179+
let dir = 'QmbQD7EMEL1zeebwBsWEfA3ndgSS6F7S6iTuwuqasPgVRi/'
180+
181+
gateway.inject({
182+
method: 'GET',
183+
url: '/ipfs/' + dir
184+
}, (res) => {
185+
expect(res.statusCode).to.equal(302)
186+
expect(res.headers['location']).to.equal('/ipfs/QmbQD7EMEL1zeebwBsWEfA3ndgSS6F7S6iTuwuqasPgVRi/index.html')
187+
done()
188+
})
189+
})
64190
})
65191
})

test/gateway/test-folder/index.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>IPFS test index.html</title>
6+
</head>
7+
<body>
8+
index.html
9+
</body>
10+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
IPFS
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>IPFS test nested.html</title>
6+
</head>
7+
<body>
8+
nested.html
9+
</body>
10+
</html>

0 commit comments

Comments
 (0)