ng-apimock / core

ng-apimock core module
MIT License
34 stars 20 forks source link

Can't set headers after they are sent. #1

Closed sentoro closed 5 years ago

sentoro commented 5 years ago

Does anyone tested it with latest Angular ? It is not working for me anymore. I see: _http_outgoing.js:491 throw new Error('Can\'t set headers after they are sent.');

server.js:


const ngApimock = require('@ng-apimock/core');
const app = express();

const mocksDir = 'e2e/mocks/endpoints';

ngApimock.processor.process({
  src: mocksDir,
  pattern: '**/*.mock.json', // optional, defaults to **/*.json
});
app.set('port', process.env.PORT || 9900);
app.use(ngApimock.middleware);

app.listen(app.get('port'), function() {
  console.log('app running on port', app.get('port'));
});```
mdasberg commented 5 years ago

@sentoro there are some demo's that do. (https://github.com/ng-apimock/demo)

The @ng-apimock/core does not have anything to do with angular. It provides middleware. So it might be something else.

Could you tell me:

And when you get the error? Do you perform an action? If so, can you provide the mock.json file?

pbazurin commented 5 years ago

Hello @mdasberg, we found the source of the problem.

It's related to this commit https://github.com/ng-apimock/core/commit/98335c324b4a138831aeaeb3482ded1b13ec2c8d.

In order to reproduce this issue, you need to use mocked file response with wrong file path (in our case it was the absolute file path instead of relative).

In this case, we sent response headers 2 times:

mdasberg commented 5 years ago

@sentoro @pbazurin Thanks for reporting and finding the root cause. A new release has been created to fix this issue. v1.0.17

GrumpyMeow commented 3 years ago

Update: it was caused by a scenario name which wasn't in the mock. Thus the scenario "Onbekend" was not defined in the selfServiceCustomerStatus mock:

      "selfServiceCustomerStatus": {
        "scenario": "Onbekend",
        "echo": true,
        "delay": 0,
        "counter": 0
      },

I seem to have the same error at this time :-S

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at ServerResponse.setHeader (_http_outgoing.js:558:11) at ServerResponse.writeHead (_http_server.js:289:21) at SelectPresetHandler.handle (/home/sander/AthoraNx/node_modules/@ng-apimock/core/src/core/middleware/handlers/api/select-preset.handler.ts:59:22) at /home/sander/AthoraNx/node_modules/@ng-apimock/core/src/core/middleware/middleware.ts:115:25

I suspect it has something to do with the combination of the cypress-plugin and the ngapimock/core versions. "@ng-apimock/core": "^3.0.1", "@ng-apimock/cypress-plugin": "^1.1.0", "@ng-apimock/dev-interface": "^2.0.0",