Skip to content

Commit

Permalink
Merge 549a4a2 into 95e01da
Browse files Browse the repository at this point in the history
  • Loading branch information
NeoyeElf committed Jan 2, 2020
2 parents 95e01da + 549a4a2 commit 248a333
Show file tree
Hide file tree
Showing 5 changed files with 289 additions and 36 deletions.
32 changes: 32 additions & 0 deletions __tests__/mock/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export default class Storage {
private map: Map<string, Buffer>

constructor() {
this.map = new Map<string, Buffer>();
}

public async getAsBuffer(key: string): Promise<{content: Buffer} | undefined> {
const data = this.map.get(key);
if (!data) {
return undefined;
}

return {
content: data
}
}

public async del(key: string) {
this.map.delete(key)
}

public async put(key: string, data: Buffer | string) {
if (data instanceof Buffer) {
this.map.set(key, data);
return
}

const buffer = Buffer.from(data);
this.map.set(key, buffer);
}
}
110 changes: 110 additions & 0 deletions __tests__/receiver-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
const sinon = require('sinon');
const uuid = require('uuid');

import * as fs from 'fs';
import * as path from 'path';

import * as Receiver from '../src/bufferSupport/receiver';
import * as storage from '../src/storage';
import FakeStorage from './mock/storage';

const sandbox = sinon.createSandbox();
const fakeStorage = new FakeStorage();

describe('receiver test cases', () => {
beforeAll(() => {
const cwd = process.cwd();
fs.writeFileSync(path.join(cwd, './.fc-config.json'), '{}')
sandbox.stub(storage, 'getClientByType').callsFake(() => fakeStorage)
})

describe('should receive works normally', () => {
describe('when storeType is direct', () => {
it('when the type of event is string', async () => {
const { receive } = Receiver.initReceiver()
const resp = await receive(JSON.stringify({
body: 'test message',
ossType: 'oss',
storeType: 'direct'
}))
expect(resp.body).toBe('test message')
})

it('when the type of event is object', async () => {
const { receive } = Receiver.initReceiver()
const resp = await receive({
body: 'test message',
ossType: 'oss',
storeType: 'direct'
})
expect(resp.body).toBe('test message')
})

it('when the type of event is Buffer', async () => {
const { receive } = Receiver.initReceiver()
const bufferEvent = Buffer.from(JSON.stringify({
body: 'test message',
ossType: 'oss',
storeType: 'direct'
}))
const resp = await receive(bufferEvent)
expect(resp.body.toString()).toBe('test message')
})

it('should the type of resp.body be Buffer, when event.isBuffer is true', async () => {
const { receive } = Receiver.initReceiver()
const resp = await receive({
body: Buffer.from('test message').toString('base64'),
ossType: 'oss',
storeType: 'direct',
isBuffer: true,
})
expect(Buffer.isBuffer(resp.body)).toBeTruthy()
expect(resp.body.toString()).toBe('test message')
})
})

describe('when storeType is oss', () => {
beforeEach(async () => {
this.key = uuid.v4();
this.content = 'aaa';
await fakeStorage.put(this.key, this.content)
})

afterEach(async () => {
const data = await fakeStorage.getAsBuffer(this.key)
expect(data).toBeFalsy()
})

it('should get content from oss works normally', async () => {
const { receive } = Receiver.initReceiver()
const resp = await receive({
body: '',
ossType: 'oss',
storeType: 'oss',
ossKey: this.key,
})
expect(resp.body).toBe(this.content)
})

it('should the type of resp.body be Buffer, when event.isBuffer is true', async () => {
const { receive } = Receiver.initReceiver()
const resp = await receive({
body: '',
ossType: 'oss',
storeType: 'oss',
ossKey: this.key,
isBuffer: true,
})
expect(Buffer.isBuffer(resp.body)).toBeTruthy()
expect(resp.body.toString()).toBe(this.content)
})
})
})

afterAll(() => {
sandbox.reset();
const cwd = process.cwd();
fs.unlinkSync(path.join(cwd, './.fc-config.json'))
})
})

0 comments on commit 248a333

Please sign in to comment.