Skip to content

is it possible to ignore mongodb requests? #30

@joelmukuthu

Description

@joelmukuthu

i have a case where my handler is making some mongodb calls and a request to a remote server. in my tests i'd like to mock out only the upstream server requests but it seems that unexpected-mitm also considers the mongodb requests as upstream traffic. i'm not really sure why this is happening and perhaps i'm just missing something obvious but here's a minimal example reproducing the problem:

// test.js
const express = require('express');
const request = require('request-promise');
const httpception = require('httpception');
const { MongoClient, Server } = require('mongodb');

const sendUpstreamRequest = async () =>
  request({
    method: 'POST',
    url: 'http://foo.bar/'
  });

const connectToMongoDb = async () => {
  const uri = 'mongodb://127.0.0.1:27017';
  const client = new MongoClient(new Server(uri));
  await client.connect(uri);
};

const expect = require('unexpected')
  .clone()
  .use(require('unexpected-express'))
  .addAssertion(
    '<object|string> to respond with <object|number>',
    (expect, req, res) => {
      const app = async (req, res, next) => {
        try {
          await connectToMongoDb();
          await sendUpstreamRequest();
          res.send('OK');
        } catch (e) {
          next(e);
        }
      };

      return expect(app, 'to yield exchange', {
        request: req,
        response: res
      });
    }
  );

it('foos', async () => {
  httpception({
    request: 'POST http://foo.bar/',
    response: 200
  });
  await expect('/', 'to respond with', 200);
});

i also have mocha installed, and when i run the test with ./node_modules/.bin/mocha --exit test.js, it times out with this output:

  1) foos
  2) "after each" hook for "foos"

  0 passing (2s)
  2 failing

  1) foos:
     Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
  

  2) "after each" hook for "foos":
     
expected
function () {
  const innerPromise = expect.promise(function (resolve, reject) {
    // This promise will be resolved once afterEach calls us.
    resolveFromAfterEach = resolve;
    // ... lines removed ...
  });

  return innerPromise;
}
with http mocked out allowing modification [ { request: 'POST http://foo.bar/', response: 200 } ] not to error

// missing:
// POST /
// Host: foo.bar
//
// HTTP/1.1 200 OK

     at Array.forEach (native)
  From previous event:
      at node_modules/httpception/lib/httpception.js:100:35
      at httpception (node_modules/httpception/lib/httpception.js:86:37)
      at Context.it (test.js:42:3)
      set UNEXPECTED_FULL_TRACE=true to see the full stack trace

i'm using httpception but it's the same problem if using unexpected-mitm. also, the test passes if i comment out the await connectToMongoDb(); call. is there a way to get unexpected-mitm to ignore the mongodb requests?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions