Skip to content

Commit

Permalink
test: test with binary data
Browse files Browse the repository at this point in the history
  • Loading branch information
maraisr committed May 26, 2023
1 parent ae42e3b commit 9a62bd2
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
13 changes: 8 additions & 5 deletions test/mocks.ts
Expand Up @@ -13,11 +13,14 @@ export const makePart = (payload: any, headers: string[] | boolean = []): Part =
if (headers === false) {
headers = [];
} else {
(headers as string[]).unshift(
`content-type: ${
typeof payload === 'string' ? 'text/plain' : 'application/json; charset=utf-8'
}`,
);
if (!headers.includes('content-type'))
(headers as string[]).unshift(
`content-type: ${
typeof payload === 'string'
? 'text/plain'
: 'application/json; charset=utf-8'
}`,
);
}

const returns = [
Expand Down
47 changes: 47 additions & 0 deletions test/suites/binary.ts
@@ -0,0 +1,47 @@
import { suite } from 'uvu';
import * as assert from 'uvu/assert';
import {
bodies,
makePart,
preamble,
tail,
wrap,
test_helper,
type Meros,
type Responder,
} from '../mocks';

export default (meros: Meros, responder: Responder) => {
const make_test = test_helper.bind(0, meros, responder);

const Binary = suite('binary');

// 1x1 transparent png
const blob = Buffer.from([
0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x01, 0x00, 0x01, 0x00, 0x80, 0x00, 0x00, 0x00,
0x00, 0x00, 0xff, 0xff, 0xff, 0x21, 0xf9, 0x04, 0x01, 0x00, 0x00, 0x00, 0x00, 0x2c,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x01, 0x44, 0x00, 0x3b,
]);

Binary('works', async () => {
const collection = await make_test((push) => {
push([
preamble,
wrap,
makePart(new TextDecoder('utf8').decode(blob), ['content-type: image/gif']),
tail,
]);
});

const values = bodies(collection);
assert.is(values.length, 1);

const img = Buffer.from(values[0]);
assert.equal(img.toString(), blob.toString());

// No clue, but bitwise they are different, but functionally equiv.
//assert.ok(img.equals(blob));
});

Binary.run();
};
6 changes: 5 additions & 1 deletion test/suites/index.ts
@@ -1,9 +1,11 @@
import { type Meros, type Responder } from '../mocks';

import { default as API } from './api';
import { default as Body } from './body';
import { default as Boundary } from './boundary';
import { default as Chunking } from './chunking';
import { default as Headers } from './headers';
import { default as Body } from './body';
import { default as Binary } from './binary';
import { default as UseCases } from './use-cases';

export default (meros: Meros, responder: Responder) => {
Expand All @@ -15,5 +17,7 @@ export default (meros: Meros, responder: Responder) => {
Headers(meros, responder);
Body(meros, responder);

Binary(meros, responder);

UseCases(meros, responder);
};

0 comments on commit 9a62bd2

Please sign in to comment.