test(middleware-body): added missing test cases
ADD
packages/core/src/+internal/observable/fromReadableStream.spec.ts
+import { fromReadableStream } from './fromReadableStream';
+import { Transform } from 'stream';
+
+class DuplexStream extends Transform {
+ _transform(chunk, _, callback) {
+ this.push(chunk);
+ callback();
+ }
+}
+
+describe('#fromReadableStream', () => {
+ test('reads data to buffer', done => {
+ const stream = new DuplexStream();
+ const data = '111';
+
+ fromReadableStream(stream).subscribe(
+ (buffer: Buffer) => {
+ expect(buffer.toString()).toEqual(data);
+ },
+ (error: Error) => {
+ fail(`Error shouldn't be thrown: ${error}`);
+ done();
+ },
+ () => {
+ done();
+ },
+ );
+
+ stream.write(data);
+ stream.end();
+ });
+
+ test('handles stream error', done => {
+ const stream = new DuplexStream();
+ const errorToThrow = new Error('test-error');
+
+ fromReadableStream(stream).subscribe(
+ (buffer: Buffer) => {
+ fail(`Error should be thrown`);
+ done();
+ },
+ (error: Error) => {
+ expect(error.message).toBe(errorToThrow.message);
+ done();
+ }
+ );
+
+ stream.emit('error', errorToThrow);
+ stream.end();
+ });
+});

MODIFY
packages/middleware-body/src/specs/body.middleware.spec.ts
packages/middleware-body/src/specs/body.middleware.spec.ts
@@ -65,29 +65,22 @@ describe('bodyParser$ middleware', () => {
request.end();
});
-
- test(`throws exception on ${ContentType.APPLICATION_JSON} parse`, done => {
+ test(`parses ${ContentType.APPLICATION_OCTET_STREAM} body`, done => {
+ const message = 'test message';
const request = new MockReq({
method: 'POST',
- headers: { 'Content-Type': ContentType.APPLICATION_JSON },
+ headers: { 'Content-Type': ContentType.APPLICATION_OCTET_STREAM },
});
const req$ = of(request as HttpRequest);
const res = {} as HttpResponse;
const http$ = bodyParser$()(req$, res, effectMeta);
- http$.subscribe(
- () => {
- fail('Exceptions should be thrown');
- done();
- },
- error => {
- expect(error.message).toBe('Request body parse error');
- expect(error.status).toBe(400);
+ http$.subscribe(data => {
+ expect(data.body).toEqual(new Buffer(message));
done();
- }
- );
+ });
- request.write('test');
+ request.write(message);
request.end();
});
@@ -109,6 +102,31 @@ describe('bodyParser$ middleware', () => {
request.end();
});
+ test(`throws exception on ${ContentType.APPLICATION_JSON} parse`, done => {
+ const request = new MockReq({
+ method: 'POST',
+ headers: { 'Content-Type': ContentType.APPLICATION_JSON },
+ });
+ const req$ = of(request as HttpRequest);
+ const res = {} as HttpResponse;
+ const http$ = bodyParser$()(req$, res, effectMeta);
+
+ http$.subscribe(
+ () => {
+ fail('Exceptions should be thrown');
+ done();
+ },
+ error => {
+ expect(error.message).toBe('Request body parse error');
+ expect(error.status).toBe(400);
+ done();
+ }
+ );
+
+ request.write('test');
+ request.end();
+ });
+
test('throws exception on EventEmitter "error" event', done => {
const request = new MockReq({
method: 'POST',
@@ -134,4 +152,22 @@ describe('bodyParser$ middleware', () => {
request.end();
});
+ test('does nothing if Content-Type is unsupported', done => {
+ const request = new MockReq({
+ method: 'POST',
+ headers: { 'Content-Type': ContentType.AUDIO_MPEG },
+ });
+ const req$ = of(request as HttpRequest);
+ const res = {} as HttpResponse;
+ const http$ = bodyParser$()(req$, res, effectMeta);
+
+ http$.subscribe(data => {
+ expect(data.body).toBeUndefined();
+ done();
+ });
+
+ request.write('test');
+ request.end();
+ });
+
});

ADD
packages/middleware-body/src/specs/body.util.spec.ts
+import { createHttpRequest } from '@marblejs/core/dist/+internal/testing/http.helper';
+import { hasBody } from '../body.util';
+
+test('#hasBody checks if request has body', () => {
+ expect(hasBody(createHttpRequest({ url: '/', body: null }))).toEqual(false);
+ expect(hasBody(createHttpRequest({ url: '/', body: undefined }))).toEqual(false);
+ expect(hasBody(createHttpRequest({ url: '/', body: 'test' }))).toEqual(true);
+ expect(hasBody(createHttpRequest({ url: '/', body: {} }))).toEqual(true);
+ expect(hasBody(createHttpRequest({ url: '/', body: 1 }))).toEqual(true);
+});

test(middleware-body): added missing test cases