@@ -6,6 +6,14 @@ const dirtyChai = require('dirty-chai')
6
6
const expect = chai . expect
7
7
chai . use ( dirtyChai )
8
8
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
+ }
9
17
10
18
describe ( 'HTTP Gateway' , ( ) => {
11
19
let http = { }
@@ -15,8 +23,35 @@ describe('HTTP Gateway', () => {
15
23
http . api = new API ( )
16
24
17
25
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
+ } )
20
55
} )
21
56
} )
22
57
@@ -27,7 +62,7 @@ describe('HTTP Gateway', () => {
27
62
} )
28
63
} )
29
64
30
- describe ( '/ipfs/* route ' , ( ) => {
65
+ describe ( '## HTTP Gateway ' , ( ) => {
31
66
it ( 'returns 400 for request without argument' , ( done ) => {
32
67
gateway . inject ( {
33
68
method : 'GET' ,
@@ -61,5 +96,96 @@ describe('HTTP Gateway', () => {
61
96
done ( )
62
97
} )
63
98
} )
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
+ } )
64
190
} )
65
191
} )
0 commit comments