mrak / stubby4node

A configurable server for mocking/stubbing external systems during development.
266 stars 62 forks source link

Request new feature to work for HTTP OPTIONS request #63

Closed DDDDDanica closed 4 years ago

DDDDDanica commented 7 years ago

Something like this would be so handy :)

-  request:
      url: ^/give/me/options$
      method: OPTIONS
   response:
      headers:
         content-type: application/json
      body: >
         [1, 2, 3]
mrak commented 7 years ago

Sorry for the late reply. Your example should already work with stubby. Have you tried it?

mrak commented 7 years ago

Also, welcome to Github and submitting issues!

castro150 commented 7 years ago

Hi! I'm with a problem related to OPTIONS method. Trying to respond to a similar request, I got the error:

events.js:163 throw er; // Unhandled 'error' event ^

Error: write after end at ServerResponse.write (_http_outgoing.js:485:15) at callback (/home/rafael/.nvm/versions/node/v7.9.0/lib/node_modules/stubby/src/portals/stubs.js:46:18) at Endpoints.found (/home/rafael/.nvm/versions/node/v7.9.0/lib/node_modules/stubby/src/models/endpoints.js:128:5) at Endpoints.find (/home/rafael/.nvm/versions/node/v7.9.0/lib/node_modules/stubby/src/models/endpoints.js:102:17) at IncomingMessage. (/home/rafael/.nvm/versions/node/v7.9.0/lib/node_modules/stubby/src/portals/stubs.js:54:22) at emitNone (events.js:86:13) at IncomingMessage.emit (events.js:188:7) at endReadableNT (_stream_readable.js:975:12) at _combinedTickCallback (internal/process/next_tick.js:80:11) at process._tickCallback (internal/process/next_tick.js:104:9)

Then the stubby simply crashes. Apparently the response is ending early, before the body is written. How can I solve this?

mrak commented 7 years ago

It appears you are using a development version of node.js. Does the same thing occur on the latest stable 6.x version of node?

xlevel commented 4 years ago

I am having the same issue.

I'm using 4.1.1 of Stubby with the LTS 10.x version of node and the following config:

- request:
    method: [OPTIONS]
    url: ^/(.*)$

  response: 
    status: 200

The error is:

events.js:174 throw er; // Unhandled 'error' event ^

Error [ERR_STREAM_WRITE_AFTEREND]: write after end at write (_http_outgoing.js:572:17) at ServerResponse.write (_http_outgoing.js:567:10) at callback (/usr/local/lib/node_modules/stubby/src/portals/stubs.js:46:18) at Endpoints.found (/usr/local/lib/node_modules/stubby/src/models/endpoints.js:128:5) at Endpoints.find (/usr/local/lib/node_modules/stubby/src/models/endpoints.js:102:17) at IncomingMessage. (/usr/local/lib/node_modules/stubby/src/portals/stubs.js:54:22) at IncomingMessage.emit (events.js:203:15) at endReadableNT (_stream_readable.js:1145:12) at process._tickCallback (internal/process/next_tick.js:63:19) Emitted 'error' event at: at writeAfterEndNT (_http_outgoing.js:634:7) at process._tickCallback (internal/process/next_tick.js:63:19)

LVCarnevalli commented 4 years ago

I am having the same issue.

I'm using 4.1.1 of Stubby with the LTS 10.x version of node and the following config:

- request:
    method: [OPTIONS]
    url: ^/(.*)$

  response: 
    status: 200

The error is:

events.js:174 throw er; // Unhandled 'error' event ^ Error [ERR_STREAM_WRITE_AFTEREND]: write after end at write (_http_outgoing.js:572:17) at ServerResponse.write (_http_outgoing.js:567:10) at callback (/usr/local/lib/node_modules/stubby/src/portals/stubs.js:46:18) at Endpoints.found (/usr/local/lib/node_modules/stubby/src/models/endpoints.js:128:5) at Endpoints.find (/usr/local/lib/node_modules/stubby/src/models/endpoints.js:102:17) at IncomingMessage. (/usr/local/lib/node_modules/stubby/src/portals/stubs.js:54:22) at IncomingMessage.emit (events.js:203:15) at endReadableNT (_stream_readable.js:1145:12) at process._tickCallback (internal/process/next_tick.js:63:19) Emitted 'error' event at: at writeAfterEndNT (_http_outgoing.js:634:7) at process._tickCallback (internal/process/next_tick.js:63:19)

I'm having the same problem...

alexxonline commented 4 years ago

I had the same problem and I was reading the source code and I realized that we shouldn't create an endpoint for the Options method. If we look at this file:

https://github.com/mrak/stubby4node/blob/master/src/portals/portal.js

We can see that option method is handle automatically (even if we see a 404 in the console).

mrak commented 4 years ago

I am unable to reproduce this (node v12.18.0)

mrak commented 4 years ago

Closing as the original issue was being able to support OPTIONS requests which stubby should already support.