Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anniel-stripe committed Feb 1, 2023
1 parent 1305a0b commit a4de0a8
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 12 deletions.
2 changes: 0 additions & 2 deletions lib/multipart.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions lib/platform/WebPlatformFunctions.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions src/multipart.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import utils = require('./utils');
import _Error = require('./Error');
import PlatformFunctions = require('./platform/PlatformFunctions');
const {StripeError} = _Error;

type MultipartCallbackReturn = any;
type MultipartCallback = (
Expand Down
7 changes: 1 addition & 6 deletions src/platform/WebPlatformFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import EventEmitter = require('events');
import StripeEmitter = require('../StripeEmitter');
import {emitWarning} from '../utils';
import PlatformFunctions = require('./PlatformFunctions');

/**
Expand Down Expand Up @@ -44,10 +42,7 @@ class WebPlatformFunctions extends PlatformFunctions {

/** @override */
tryBufferData(data: any): Promise<any> {
if (
data.file.data.pipeThrough &&
typeof data.file.data.pipeThrough === 'function'
) {
if (data.file.data instanceof ReadableStream) {
throw new Error(
'Uploading a file as a stream is not supported in non-Node environments. Please open or upvote an issue at github.com/stripe/stripe-node if you use this, detailing your use-case.'
);
Expand Down
73 changes: 73 additions & 0 deletions test/PlatformFunctions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

require('../testUtils');

import fs = require('fs');
import path = require('path');
import NodePlatformFunctions = require('../lib/platform/NodePlatformFunctions');
import PlatformFunctions = require('../lib/platform/PlatformFunctions');

Expand Down Expand Up @@ -276,5 +278,76 @@ for (const platform in platforms) {
done();
});
});

describe('tryBufferData', () => {
if (!isNodeEnvironment) {
// WebPlatformFunctions
it('should throw an error in web environments if streaming data is provided', () => {
if (process.versions.node < '18') {
console.log(
`'ReadableStream' is not available in the global scope for ${process.version}, skipping test.`
);
return;
}

const f = new ReadableStream();
const data = {
file: {
data: f,
name: 'test.pdf',
type: 'application/octet-stream',
},
};

expect(() => {
platformFunctions.tryBufferData(data);
}).to.throw();
});
} else {
// NodePlatformFunctions
it('should return data unmodified if not a file stream', () => {
const testFilename = path.join(
__dirname,
'resources/data/minimal.pdf'
);
const buf = fs.readFileSync(testFilename);

// Create Uint8Array from Buffer
const f = new Uint8Array(buf.buffer, buf.byteOffset, buf.length);

const data = {
file: {
data: f,
name: 'minimal.pdf',
type: 'application/octet-stream',
},
};

expect(
platformFunctions.tryBufferData(data)
).to.eventually.deep.equal(data);
});

it('should load file streams into buffer', () => {
const testFilename = path.join(
__dirname,
'resources/data/minimal.pdf'
);
const f = fs.createReadStream(testFilename);

const data = {
file: {
data: f,
name: 'minimal.pdf',
type: 'application/octet-stream',
},
};

return expect(
platformFunctions.tryBufferData(data).then((d) => d.file.data)
).to.eventually.have.nested.property('length', 739);
});
}
});
});
}

0 comments on commit a4de0a8

Please sign in to comment.