-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.test.js
216 lines (206 loc) · 8.39 KB
/
client.test.js
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import createDebug from 'debug'
import chai, { util, expect } from 'chai'
import chailint from 'chai-lint'
import superagent from 'superagent'
import feathers from '@feathersjs/feathers'
import express from '@feathersjs/express'
import feathersClient from '@feathersjs/client'
import feathersSocketio from '@feathersjs/socketio'
import io from 'socket.io-client'
import { Service, getClientService } from '../lib/index.js'
import fs from 'fs'
import crypto from 'crypto'
import { Blob } from 'buffer'
import fetch from 'node-fetch'
const { rest } = express
feathers.setDebug(createDebug)
feathersClient.setDebug(createDebug)
const debugClient = createDebug('feathers-s3:client')
let serverApp, expressServer, socket, transport, clientApp, s3Service, s3ClientService
const options = {
s3Client: {
credentials: {
accessKeyId: process.env.S3_ACCESS_KEY_ID,
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY
},
endpoint: process.env.S3_ENDPOINT,
region: process.env.S3_REGION,
signatureVersion: 'v4'
},
bucket: process.env.S3_BUCKET,
prefix: crypto.randomUUID()
}
const textFileId = 'text.txt'
const imageFileId = 'image.png'
const archiveFileId = 'archive.zip'
const featuresFileId = 'features.geojson'
const textFileContent = fs.readFileSync('test/data/text.txt')
const imageFileContent = fs.readFileSync('test/data/image.png')
const archiveFileContent = fs.readFileSync('test/data/archive.zip')
const featuresFileContent = fs.readFileSync('test/data/features.geojson')
let useProxy = false
function runTests (message, checkEvents) {
it('create s3 service' + message, () => {
s3ClientService = getClientService(clientApp, {
servicePath: 's3',
transport,
useProxy,
fetch,
debug: debugClient
})
expect(s3ClientService).toExist()
expect(s3ClientService.createMultipartUpload).toExist()
expect(s3ClientService.completeMultipartUpload).toExist()
expect(s3ClientService.uploadPart).toExist()
expect(s3ClientService.putObject).toExist()
expect(s3ClientService.upload).toExist()
expect(s3ClientService.download).toExist()
})
it('upload text file' + message, async () => {
const blob = new Blob([textFileContent], { type: 'text/plain' })
let eventReceived = false
if (checkEvents) {
s3ClientService.once(useProxy ? 'object-put' : 'created', (data) => {
if (data.id === textFileId) eventReceived = true
})
}
const response = await s3ClientService.upload(textFileId, blob, { expiresIn: 30 })
expect(response.ETag).toExist()
if (checkEvents) expect(eventReceived).beTrue()
})
it('upload image file' + message, async () => {
const blob = new Blob([imageFileContent], { type: 'image/png' })
let eventReceived = false
if (checkEvents) {
s3ClientService.once(useProxy ? 'object-put' : 'created', (data) => {
if (data.id === imageFileId) eventReceived = true
})
}
const response = await s3ClientService.upload(imageFileId, blob, { expiresIn: 30 })
expect(response.ETag).toExist()
if (checkEvents) expect(eventReceived).beTrue()
})
it('upload zip file' + message, async () => {
const blob = new Blob([archiveFileContent], { type: 'application/zip' })
let eventReceived = false
if (checkEvents) {
s3ClientService.once(useProxy ? 'object-put' : 'created', (data) => {
if (data.id === archiveFileId) eventReceived = true
})
}
const response = await s3ClientService.upload(archiveFileId, blob, { expiresIn: 30 })
expect(response.ETag).toExist()
if (checkEvents) expect(eventReceived).beTrue()
})
it('upload features file' + message, async () => {
const blob = new Blob([featuresFileContent], { type: 'application/geo+json' })
let eventsReceived = 0
if (checkEvents) {
s3ClientService.once('multipart-upload-created', (data) => {
if (data.id === featuresFileId) eventsReceived++
})
s3ClientService.once(useProxy ? 'part-uploaded' : 'created', (data) => {
if (data.id === featuresFileId) {
eventsReceived++
s3ClientService.once(useProxy ? 'part-uploaded' : 'created', (data) => {
if (data.id === featuresFileId) eventsReceived++
})
}
})
s3ClientService.once('multipart-upload-completed', (data) => {
if (data.id === featuresFileId) eventsReceived++
})
}
const response = await s3ClientService.upload(featuresFileId, blob, { expiresIn: 30 })
expect(response.ETag).toExist()
if (checkEvents) expect(eventsReceived).to.equal(4)
})
it('list uploaded files', async () => {
const response = await s3ClientService.find()
expect(response.length).to.equal(4)
})
it('download text file' + message, async () => {
const response = await s3ClientService.download(textFileId, { expiresIn: 30 })
expect(response.type).to.equal('text/plain')
expect(response.buffer).toExist()
expect(Buffer.from(response.buffer).toString()).to.equal(textFileContent.toString())
})
it('download image file' + message, async () => {
const response = await s3ClientService.download(imageFileId, { expiresIn: 30 })
expect(response.type).to.equal('image/png')
expect(response.buffer).toExist()
expect(Buffer.from(response.buffer).toString()).to.equal(imageFileContent.toString())
})
it('download zip file' + message, async () => {
const response = await s3ClientService.download(archiveFileId, { expiresIn: 30 })
expect(response.type).to.equal('application/zip')
expect(response.buffer).toExist()
expect(Buffer.from(response.buffer).toString()).to.equal(archiveFileContent.toString())
})
it('download features file' + message, async () => {
const response = await s3ClientService.download(featuresFileId, { expiresIn: 30 })
expect(response.type).to.equal('application/geo+json')
expect(response.buffer).toExist()
expect(Buffer.from(response.buffer).toString()).to.equal(featuresFileContent.toString())
})
it('delete text file' + message, async () => {
const response = await s3ClientService.remove(textFileId)
expect(response.$metadata.httpStatusCode).to.equal(204)
})
it('delete image file' + message, async () => {
const response = await s3ClientService.remove(imageFileId)
expect(response.$metadata.httpStatusCode).to.equal(204)
})
it('delete archive file' + message, async () => {
const response = await s3ClientService.remove(archiveFileId)
expect(response.$metadata.httpStatusCode).to.equal(204)
})
it('change proxy mode', () => {
useProxy = !useProxy
})
}
describe('feathers-s3-client', () => {
before(async () => {
chailint(chai, util)
serverApp = express(feathers())
// With proxy we need to extend limits as we are transferring object/part to our server
serverApp.use(express.json({ limit: 100 * 1024 * 1024 }))
serverApp.use(express.urlencoded({ extended: true }))
serverApp.configure(rest())
serverApp.configure(feathersSocketio({ maxHttpBufferSize: 1e8 }))
expressServer = await serverApp.listen(3333)
})
it('is ES module compatible', () => {
expect(typeof Service).to.equal('function')
expect(typeof getClientService).to.equal('function')
})
it('create s3 service', () => {
serverApp.use('s3', new Service(options), {
methods: ['create', 'get', 'find', 'remove', 'createMultipartUpload', 'completeMultipartUpload', 'uploadPart', 'putObject'],
events: ['multipart-upload-created', 'multipart-upload-completed', 'part-uploaded', 'object-put']
})
// Setup a channel to publish all events
serverApp.on('connection', connection => serverApp.channel('anonymous').join(connection))
serverApp.publish((data, context) => serverApp.channel('anonymous'))
s3Service = serverApp.service('s3')
expect(s3Service).toExist()
})
it('create REST client', () => {
clientApp = feathersClient()
transport = feathersClient.rest('http://localhost:3333').superagent(superagent)
clientApp.configure(transport)
})
runTests(' with REST client and without proxy', false)
runTests(' with REST client and with proxy', false)
it('create websocket client', () => {
clientApp = feathersClient()
socket = io('http://localhost:3333')
transport = feathersClient.socketio(socket)
clientApp.configure(transport)
})
runTests(' with websocket client and without proxy', true)
runTests(' with websocket client and with proxy', true)
after(async () => {
await expressServer.close()
})
})