How to use node-mocks-http - 10 common examples

To help you get started, we’ve selected a few node-mocks-http examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github universal-vue / uvue / packages / @uvue / vue-cli-plugin-ssr / uvue / StaticGenerate.js View on Github external
createRequestContext(url) {
    // Fake IncomingRequest
    const req = httpMocks.createRequest({
      method: 'GET',
      url,
    });

    // Fake ServerResponse
    const res = httpMocks.createResponse();
    res.__body = '';
    res.write = chunk => {
      res.__body += chunk;
    };
    res.end = data => {
      if (data) {
        res.__body = data;
      }
    };

    return {
      url,
      req,
      res,
      redirected: false,
      statusCode: 200,
github Strider-CD / strider / test / unit / test_middleware.js View on Github external
it('should fallthrough if all params are present', function () {
      var mockReq = httpMocks.createRequest({
        body: {
          email: 'user@email.com',
          name: 'New Guy'
        }
      });
      var mockRes = httpMocks.createResponse();

      middleware.requireBody(['email', 'name'])(mockReq, mockRes, function () {
        mockRes.statusCode = 200;
      });

      mockRes.statusCode.should.eql(200);
    });
github solid / oidc-auth-manager / test / unit / login-consent-request.js View on Github external
it('should be false if params has no client id', () => {
      let params = {}
      let response = HttpMocks.createResponse()
      let opAuthRequest = {
        host: {}
      }

      let request = new LoginConsentRequest({ params, response, opAuthRequest })

      expect(request.isLocalRpClient(undefined)).to.be.false()
    })
github punchcard-cms / punchcard / tests / applications.js View on Github external
test('Create new secret', t => {
  const req = _.cloneDeep(reqObj);
  req.method = 'POST';
  req.headers.referrer = '/applications/1';
  req.session.form.applications.edit.id = 1;

  const request = httpMocks.createRequest(req);

  const response = httpMocks.createResponse({ eventEmitter: EventEmitter });
  const resp = applications.routes.secret(request, response, next);
  response.render();

  return response.on('end', () => {
    t.is(response.statusCode, 302, 'Should be a 302 response');
    t.is(response._getRedirectUrl(), '/applications/1', 'should redirect to edit url');

    return resp.then(res => {
      t.not(res, dbmocks.rows[0]['client-secret'], 'should be a new client secret');
    });
  });
});
github matheuslc / shorts / test / url / shortUrlController.spec.js View on Github external
it('Should throw an error if url does not exist', () => {
    let request = httpMocks.createRequest({
      method: 'POST',
      body: {}
    });

    let response = httpMocks.createResponse({
      eventEmitter: events.EventEmitter
    });

    expect(() => {
      Controller.createShortUrl(request, response);
    }).to.throw(Error);
  });
github nicolasdao / webfunc / test / cors.js View on Github external
it('Should retrieves required response headers defined inside the config.json file.', () => {
		/*eslint-enable */
		const req_01 = httpMocks.createRequest({
			method: 'GET',
			headers: {
				origin: 'http://localhost:8080',
				referer: 'http://localhost:8080'
			},
			_parsedUrl: {
				pathname: '/users/nicolas'
			}
		})
		const res_01 = httpMocks.createResponse()

		const req_02 = httpMocks.createRequest({
			method: 'POST',
			headers: {
				origin: 'http://localhost:8080',
				referer: 'http://localhost:8080'
			},
			_parsedUrl: {
				pathname: '/users/nicolas'
			},
			query: { lastname: 'dao' }
		})
		const res_02 = httpMocks.createResponse()

		const req_01_cors = cors({
			origins: ['http://boris.com', 'http://localhost:8080'],
			methods: ['GET', 'HEAD', 'OPTIONS'],
			allowedHeaders: ['Authorization', 'Content-Type', 'Origin'],
github alex94cp / jsonapify / test / select.js View on Github external
it('removes all resource hooks', function(done) {
			var select = new Selector;
			var object = new testModel;
			var req = httpMocks.createRequest({
				query: {
					fields: 'type,id,attributes.a',
				},
			})
			select.initialize(resource, req, response);
			select.remove();
			resource.serialize(object, null, function(err, resdata) {
				if (err) return done(err);
				expect(resdata).to.have.property('id');
				expect(resdata).to.have.property('type', 'test-models');
				expect(resdata).to.have.deep.property('attributes.a', 1234);
				expect(resdata).to.have.deep.property('attributes.b', 5678);
				done();
			});
		});
	});
github solid / node-solid-server / test / unit / create-account-request.js View on Github external
it('should create subclass depending on authMethod', () => {
      let request, aliceData, req

      aliceData = { username: 'alice' }
      req = HttpMocks.createRequest({
        app: { locals: { accountManager } }, body: aliceData, session
      })
      req.app.locals.authMethod = 'tls'

      request = CreateAccountRequest.fromParams(req, res, accountManager)
      expect(request).to.respondTo('generateTlsCertificate')

      aliceData = { username: 'alice', password: '12345' }
      req = HttpMocks.createRequest({
        app: { locals: { accountManager, oidc: {} } }, body: aliceData, session
      })
      req.app.locals.authMethod = 'oidc'
      request = CreateAccountRequest.fromParams(req, res, accountManager)
      expect(request).to.not.respondTo('generateTlsCertificate')
    })
  })
github SAP / cloud-s4-sdk-examples / test / unit-tests / helloworld.spec.js View on Github external
it("responds with \"Hello, World!\"", () => {
    const response = createResponse();
    spy(response, "send");
    spy(response, "status");

    helloWorld(createRequest(), response);

    expect(response.status).to.have.been.calledWith(200);
    expect(response.send).to.have.been.calledWith("Hello, World!");
  });
});
github SAP / cf-nodejs-logging-support / test / test-complete.js View on Github external
it("checking dummy app results", () => {
        process.hrtime = function () {
            return [12, 14];
        }
        req = httpMock.createRequest({
            headers: {
                "X-CorrelationID": "test-correlation-id",
                "remote-user": "test-user"
            }
        });
        res = httpMock.createResponse();
        prepare(res);
        log.overrideNetworkField("msg","testmessage");
        log.logNetwork(req, res, () => {});
        res.end("ok");
        assert.deepEqual(JSON.parse(store),results.getLogMessage());
    });
});

node-mocks-http

Mock 'http' objects for testing Express, Next.js and Koa routing functions

MIT
Latest version published 11 days ago

Package Health Score

91 / 100
Full package analysis