This repository was archived by the owner on Mar 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuploads.test.js
More file actions
161 lines (146 loc) · 3.98 KB
/
Copy pathuploads.test.js
File metadata and controls
161 lines (146 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import test from 'blue-tape'
import express from 'express'
import axios from 'axios'
import { encode } from 'tus-metadata'
import { createHash } from 'crypto'
import { memstore } from 'abstract-tus-store'
import tusboy from '../src'
import { file, counter } from './common'
const md5 = (buf) => createHash('md5').update(buf).digest()
let server
let baseURL
let client
const store = memstore()
const nextId = counter()
test('start server', (t) => {
const app = express()
app.use('/uploads', tusboy(store, {
getKey: () => `${nextId()}`,
}))
app.get('/files/:key', (req, res, next) => {
const rs = store
.createReadStream(req.params.key, ({ contentLength, metadata }) => {
res.set('Content-Type', metadata.contentType)
res.set('Content-Length', contentLength)
rs.pipe(res)
})
.on('error', next)
})
server = app.listen(() => {
baseURL = `http://localhost:${server.address().port}`
client = axios.create({
baseURL,
headers: {
'Tus-Resumable': '1.0.0',
},
})
t.end()
})
})
let uploadUrl0
test('create', (t) => (
client
.post('/uploads', null, {
headers: {
'Upload-Length': file('crow.jpg').size,
'Upload-Metadata': encode({
contentType: file('crow.jpg').contentType,
}),
},
})
.then((response) => {
t.equal(typeof response.headers.location, 'string', 'Location header')
uploadUrl0 = response.headers.location
})
))
let uploadUrl1
test('create', (t) => (
client
.post('/uploads', null, {
headers: {
'Upload-Length': file('crow.jpg').size,
'Upload-Metadata': encode({
contentType: file('crow.jpg').contentType,
}),
},
})
.then((response) => {
uploadUrl1 = response.headers.location
t.equal(typeof uploadUrl1, 'string', 'Location header')
t.notEqual(uploadUrl0, uploadUrl1)
})
))
test('head', (t) => (
client
.head(uploadUrl1)
.then((response) => {
const { headers } = response
t.equal(headers['cache-control'], 'no-store', 'cache-control')
t.equal(headers['tus-resumable'], '1.0.0', 'tus-resumable')
t.equal(
headers['upload-length'],
`${file('crow.jpg').size}`,
'Upload-Length',
)
})
))
// TODO: test options
test('patch (first half)', (t) => {
const halfway = Math.floor(file('crow.jpg').size / 2)
const rs = file('crow.jpg').rs({ start: 0, end: halfway })
return client
.patch(uploadUrl1, rs, {
headers: {
'Content-Type': 'application/offset+octet-stream',
'Upload-Offset': 0,
},
})
.then((response) => {
const { headers } = response
t.equal(headers['upload-offset'], `${halfway + 1}`, 'Upload-Offset')
})
})
test('patch (wrong offset)', (t) => {
const halfway = Math.floor(file('crow.jpg').size / 2)
const rs = file('crow.jpg').rs()
client
.patch(uploadUrl1, rs, {
headers: {
'Content-Type': 'application/offset+octet-stream',
'Upload-Offset': halfway - 10,
},
})
.catch(({ response }) => {
t.equal(response.status, 409)
t.end()
})
})
test('patch (second half)', (t) => {
const halfway = Math.floor(file('crow.jpg').size / 2)
const rs = file('crow.jpg').rs({ start: halfway + 1 })
return client
.patch(uploadUrl1, rs, {
headers: {
'Content-Type': 'application/offset+octet-stream',
'Upload-Offset': halfway + 1,
},
})
.then((response) => {
const { headers } = response
t.equal(headers['upload-offset'], `${file('crow.jpg').size}`, 'Upload-Offset')
})
})
test('read file', (t) => (
client
.get('/files/1', {
responseType: 'arraybuffer',
})
.then((response) => {
t.equal(response.headers['content-type'], 'image/jpeg')
t.equal(response.headers['content-length'], `${file('crow.jpg').size}`)
t.deepEqual(md5(response.data), md5(file('crow.jpg').buf))
})
))
test((t) => {
server.close(() => t.end())
})