Add double conversion check (issue #14)

This commit is contained in:
yumauri
2020-05-30 18:02:35 +03:00
parent 3d9392b920
commit cea090f373
7 changed files with 40 additions and 12 deletions
+1 -1
View File
@@ -25,7 +25,7 @@
{
"path": "pkg/dist-node/index.js",
"webpack": false,
"limit": "3916 B"
"limit": "4100 B"
}
],
"@pika/pack": {
+1 -1
View File
@@ -179,7 +179,7 @@ export type HttpHeaders = {
/// request types //////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
export const enum RequestType {
export enum RequestType {
Url,
Ping,
Html,
-6
View File
@@ -74,12 +74,6 @@ export const toTuples = (source: Source, recursive = false): TupleSource[] => {
return ret
}
// if we get there inside of recursion -> this is bad
if (recursive) {
// by tests looks like this is impossible case -> should remove it?
throw new Error('Bad source, possible recursive iterables?')
}
// if iterable source
if (isIterable(source)) {
const ret: TupleSource[] = []
+12 -4
View File
@@ -20,7 +20,15 @@ export const type: {
(type: RequestType.Merge): (request: Request) => MergeRequest
(type: RequestType.Office): (request: Request) => OfficeRequest
(type: RequestType.Markdown): (request: Request) => MarkdownRequest
} = (type: RequestType) => (request: Request): any => ({
...request,
type,
})
} = (type: RequestType) => (request: Request): any => {
if ('type' in request && request.type !== RequestType.Undefined) {
throw new Error(
`Cannot set "${RequestType[type]}" conversion, already set to "${RequestType[request.type]}"`
)
}
return {
...request,
type,
}
}
+13
View File
@@ -39,6 +39,19 @@ test('Test `toTuples` function', () => {
expect(() => toTuples(set)).toThrow('Bad source, don\'t know what to do with "[object Set]"')
})
test('Test `toTuples` function, different edge cases', () => {
// line 46
const file = createReadStream(`${__dirname}/../manual/statement.html`)
expect(toTuples(file)).toEqual([['index.html', file]])
// line 66, `hasOwnProperty`
function Src(this: any) {
this['index.html'] = 'test'
}
Src.prototype['header.html'] = ''
expect(toTuples(new Src())).toEqual([['index.html', 'test']])
})
test('Test `fromFile` function', () => {
expect(fromFile('file:' + __filename) instanceof ReadStream).toBe(true)
expect(fromFile('file://' + __filename) instanceof ReadStream).toBe(true)
+6
View File
@@ -57,3 +57,9 @@ test('Should change type to Markdown', () => {
type: RequestType.Markdown,
})
})
test('Should throw on double conversion', () => {
const request = { type: RequestType.Url } as Request
const fn = type(RequestType.Office)
expect(() => fn(request)).toThrow(`Cannot set "Office" conversion, already set to "Url"`)
})
+7
View File
@@ -0,0 +1,7 @@
import { convert, gotenberg, office, pipe, please, url } from '../../src'
// following should throw an exception about double conversion
// >>> Cannot set "Office" conversion, already set to "Url"
const toPDF = pipe(gotenberg('http://localhost:8008'), convert, url, office, please)
toPDF('http://any.url.com').then(console.log).catch(console.error)