eugef / node-mocks-http

Mock 'http' objects for testing Express routing functions
Other
753 stars 133 forks source link

request.pipe is not a function #166

Closed amitguptagwl closed 6 years ago

amitguptagwl commented 6 years ago

I was assuming that the mocked request is actually a fake stream so pipe function should be available.

amitguptagwl commented 6 years ago

I can raise a PR for this if you please merge my previous PR. However I strongly feel that original API should be built on top of stream not the event

eugef commented 6 years ago

Please raise a PR

amitguptagwl commented 6 years ago

Thanks @eugef for your support. However I've been switched to some other library due to stream handling. Here is the code change for other users who faces this issue.

mockRequest.send = function(data) {

        if(Buffer.isBuffer(data)){
        }else if(typeof data === "object" || typeof data === "number"){
            data = Buffer.from(JSON.stringify(data));
        }else if(typeof data === "string"){
            data =  Buffer.from(data);
        }
        this.emit('data', data);
        this.emit('end');
        if(this.stream){
            this.stream.end(data);
        }
    };

    mockRequest.pipe = function(dest) {
        this.stream = dest;
    };

Feel free to close this issue.

tzkmx commented 1 year ago

Thanks @eugef for your support. However I've been switched to some other library due to stream handling. Here is the code change for other users who faces this issue.

It'd be nice to know which library you found that support this @amitguptagwl

Nevertheless this is what we are using in the job and we'd like to share something that provides without modifying the library (thus, not breaking any preexisting tests).

This is tested at this moment to send CSV files as part of a multipart form request, the contents of the file are inlined in the body as strings, and the boundaries are inserted by hand, but it is accepted without any trouble by busboy, for example, with codes that calls req.pipe

We tried the patch suggested, but it didn't worked for us:

mockRequest.send = function(data) {
// ...
        if(this.stream){
            this.stream.end(data);
        }
    };

// fails, it's not accepted as valid stream
    mockRequest.pipe = function(dest) {
        this.stream = dest;
    };

Feel free to close this issue.

Rather I'd like to reopen the conversation and if it is valuable, provide it in a PR, in the meantime, this is what we are using now to "decorate" the mockRequest:

class CustomBodyStream extends Readable {
  constructor(private body: any) {
    super()
  }

  _read() {
    this.push(this.body)
    this.push(null)
  }
}

function decorateMockRequest(mockRequest) {
  // Create a Readable stream that simulates the request body
  const stream = new CustomBodyStream(mockRequest.body)

  // Add the pipe method to the mockRequest
  mockRequest.pipe = function (dest) {
    stream.pipe(dest)
  }
  return mockRequest
}

httpMock.createRequest({
    method: 'POST',
    url: '/csv-read',
    headers: {
    'content-type': 'multipart/form-data; boundary=YourCustomBoundary'
    },
    body: csvString as any // required because types carachterize this as Record<string, string>
})

Left here in case anyone founds it useful.