|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | 3 | const assert = require('assert'); |
| 4 | +const { Readable } = require('stream'); |
4 | 5 | const Parse = require('../../node'); |
5 | 6 |
|
6 | 7 | describe('Parse.File', () => { |
@@ -144,6 +145,69 @@ describe('Parse.File', () => { |
144 | 145 | } |
145 | 146 | }); |
146 | 147 |
|
| 148 | + it('can save file from Buffer', async () => { |
| 149 | + const data = Buffer.from([61, 170, 236, 120]); |
| 150 | + const file = new Parse.File('buffer-file.bin', data); |
| 151 | + await file.save(); |
| 152 | + |
| 153 | + assert(file.url()); |
| 154 | + assert(file.name()); |
| 155 | + |
| 156 | + const object = new Parse.Object('TestObject'); |
| 157 | + object.set('file', file); |
| 158 | + await object.save(); |
| 159 | + |
| 160 | + const query = new Parse.Query('TestObject'); |
| 161 | + const result = await query.get(object.id); |
| 162 | + assert.equal(result.get('file').name(), file.name()); |
| 163 | + assert.equal(result.get('file').url(), file.url()); |
| 164 | + |
| 165 | + const retrieved = await file.getData(); |
| 166 | + assert.equal(retrieved, data.toString('base64')); |
| 167 | + }); |
| 168 | + |
| 169 | + it('can save file from Buffer with content type', async () => { |
| 170 | + const data = Buffer.from('hello world'); |
| 171 | + const file = new Parse.File('hello.txt', data, 'text/plain'); |
| 172 | + await file.save(); |
| 173 | + |
| 174 | + assert(file.url()); |
| 175 | + const retrieved = await file.getData(); |
| 176 | + assert.equal(Buffer.from(retrieved, 'base64').toString(), 'hello world'); |
| 177 | + }); |
| 178 | + |
| 179 | + it('can save file from Readable stream', async () => { |
| 180 | + const data = Buffer.from([61, 170, 236, 120]); |
| 181 | + const stream = Readable.from(data); |
| 182 | + const file = new Parse.File('stream-file.bin', stream); |
| 183 | + await file.save(); |
| 184 | + |
| 185 | + assert(file.url()); |
| 186 | + assert(file.name()); |
| 187 | + |
| 188 | + const object = new Parse.Object('TestObject'); |
| 189 | + object.set('file', file); |
| 190 | + await object.save(); |
| 191 | + |
| 192 | + const query = new Parse.Query('TestObject'); |
| 193 | + const result = await query.get(object.id); |
| 194 | + assert.equal(result.get('file').name(), file.name()); |
| 195 | + assert.equal(result.get('file').url(), file.url()); |
| 196 | + |
| 197 | + const retrieved = await file.getData(); |
| 198 | + assert.equal(retrieved, data.toString('base64')); |
| 199 | + }); |
| 200 | + |
| 201 | + it('can save file from Readable stream with content type', async () => { |
| 202 | + const stream = Readable.from(Buffer.from('hello world')); |
| 203 | + const file = new Parse.File('hello.txt', stream, 'text/plain'); |
| 204 | + await file.save(); |
| 205 | + |
| 206 | + assert(file.url()); |
| 207 | + const retrieved = await file.getData(); |
| 208 | + assert.equal(Buffer.from(retrieved, 'base64').toString(), 'hello world'); |
| 209 | + }); |
| 210 | + |
147 | 211 | it('can save file to localDatastore', async () => { |
148 | 212 | Parse.enableLocalDatastore(); |
149 | 213 | const file = new Parse.File('parse-js-sdk', [61, 170, 236, 120]); |
|
0 commit comments