|
| 1 | +import "reflect-metadata"; |
| 2 | + |
| 3 | +import {createReadStream} from "fs"; |
| 4 | +import * as path from "path"; |
| 5 | +import {createExpressServer, createKoaServer, getMetadataArgsStorage} from "../../src/index"; |
| 6 | +import {assertRequest} from "./test-utils"; |
| 7 | +import {InterceptorInterface} from "../../src/InterceptorInterface"; |
| 8 | +import {Interceptor} from "../../src/decorator/Interceptor"; |
| 9 | +import {UseInterceptor} from "../../src/decorator/UseInterceptor"; |
| 10 | +import {Controller} from "../../src/decorator/Controller"; |
| 11 | +import {Get} from "../../src/decorator/Get"; |
| 12 | +import {Action} from "../../src/Action"; |
| 13 | +import {ContentType} from "../../src/decorator/ContentType"; |
| 14 | +const chakram = require("chakram"); |
| 15 | +const expect = chakram.expect; |
| 16 | + |
| 17 | +describe("special result value treatment", () => { |
| 18 | + |
| 19 | + const rawData = [0xFF, 0x66, 0xAA, 0xCC]; |
| 20 | + |
| 21 | + before(() => { |
| 22 | + |
| 23 | + // reset metadata args storage |
| 24 | + getMetadataArgsStorage().reset(); |
| 25 | + |
| 26 | + @Controller() |
| 27 | + class HandledController { |
| 28 | + |
| 29 | + @Get("/stream") |
| 30 | + @ContentType("text/plain") |
| 31 | + getStream() { |
| 32 | + return createReadStream(path.resolve(__dirname, "../../../../test/resources/sample-text-file.txt")); |
| 33 | + } |
| 34 | + |
| 35 | + @Get("/buffer") |
| 36 | + getBuffer() { |
| 37 | + return new Buffer(rawData); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + }); |
| 42 | + |
| 43 | + let expressApp: any, koaApp: any; |
| 44 | + before(done => expressApp = createExpressServer().listen(3001, done)); |
| 45 | + after(done => expressApp.close(done)); |
| 46 | + before(done => koaApp = createKoaServer().listen(3002, done)); |
| 47 | + after(done => koaApp.close(done)); |
| 48 | + |
| 49 | + describe("should pipe stream to response", () => { |
| 50 | + assertRequest([3001, 3002], "get", "stream", response => { |
| 51 | + expect(response).to.be.status(200); |
| 52 | + expect(response).to.have.header("content-type", (contentType: string) => { |
| 53 | + expect(contentType).to.match(/text\/plain/); |
| 54 | + }); |
| 55 | + expect(response.body).to.be.equal("Hello World!"); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe("should send raw binary data", () => { |
| 60 | + assertRequest([3001, 3002], "get", "buffer", response => { |
| 61 | + expect(response).to.be.status(200); |
| 62 | + expect(response).to.have.header("content-type", "application/octet-stream"); |
| 63 | + expect(response.body).to.be.equal(new Buffer(rawData).toString()); |
| 64 | + }); |
| 65 | + }); |
| 66 | +}); |
0 commit comments