How to use the node-mocks-http.createRequest function in node-mocks-http

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 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());
    });
});
github nicolasdao / webfunc / test / index.js View on Github external
it(`Should support any path if no path have been defined in any route.`, () => {
			/*eslint-enable */
			const req_01 = httpMocks.createRequest({
				method: 'GET',
				headers: {
					origin: 'http://localhost:8080',
					referer: 'http://localhost:8080'
				},
				_parsedUrl: {
					pathname: '/users'
				}
			})
			const res_01 = httpMocks.createResponse()

			const req_02 = httpMocks.createRequest({
				method: 'GET',
				headers: {
					origin: 'http://localhost:8080',
					referer: 'http://localhost:8080'
				},
				_parsedUrl: {
					pathname: '/companies'
				}
			})
			const res_02 = httpMocks.createResponse()

			app.reset()
			app.get('/(.*)', (req, res) => res.status(200).send('Hello User'))
			const fn = app.handleEvent()

			const result_01 = fn(req_01, res_01).then(() => {
github jkriss / altcloud / tests / signup.js View on Github external
test("don't allow missing password", function (t) {
  t.plan(3)

  const invitationCode = `some-invitation-code:
  expires: ${new Date().getTime() + (1000 * 60 * 60 * 24 * 2)}
  `

  fs.writeFileSync(invitationsPath, invitationCode)
  passwords.remove(`${__dirname}/data/.passwords`, 'newuser')

  const req = httpMocks.createRequest({
    method: 'POST',
    url: '/signup',
    body: {
      token: 'some-invitation-code',
      username: 'newuser'
    }
  })

  const res = httpMocks.createResponse()

  const handler = signup({root: `${__dirname}/data/`})

  handler(req, res, function (err) {
    t.ok(err)
    t.notOk(req.user)
github alex94cp / jsonapify / test / middleware / modify.js View on Github external
model.create({}, function(err, object) {
				if (err) return done(err);
				common.initAccessor(accessors.field, 'prev');
				common.initAccessor(accessors.output);
				var req = httpMocks.createRequest({
					params: { id: object._id.toString() },
					body: {
						data: [{
							op: 'add',
							path: '/field',
							value: 'current',
						}],
					},
				});
				var chain = ['ModifyResource', jsonapify.param('id')];
				modify(chain)(req, res, function(err) {
					if (err) return done(err);
					var resdata = JSON.parse(res._getData());
					expect(resdata).to.have.deep.property('data.field');
					expect(resdata.data.field).to.equal('current');
					done();
github nicolasdao / webfunc / src / utils.js View on Github external
const createGCPRequestResponse = (event={}, paramsPropName) => {
	try {
		const pubsubMessage = event.data || {}
		const data = pubsubMessage.attributes || {}
		let body
		try {
			/*eslint-disable */
			body = pubsubMessage.data ? Buffer.from(pubsubMessage.data, 'base64').toString() : {}
		}
		catch(err) {}
		/*eslint-enable */

		const resource = event.resource || ''

		let req = httpMocks.createRequest({
			method: 'POST',
			headers: {
				origin: resource
			},
			_parsedUrl: {
				pathname: data.pathname && typeof(data.pathname) == 'string' ? path.posix.join('/', data.pathname) : '/'
			}
		})

		req[paramsPropName] = data
		req.body = body
		req.__event = event

		return { req, res: httpMocks.createResponse() }
	}
	catch(err) {
github DFEAGILEDEVOPS / MTC / admin / spec / controllers / service-manager.spec.js View on Github external
function getReq (params) {
    const req = httpMocks.createRequest(params)
    req.breadcrumbs = jasmine.createSpy('breadcrumbs')
    req.flash = jasmine.createSpy('flash')
    return req
  }

node-mocks-http

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

MIT
Latest version published 1 month ago

Package Health Score

89 / 100
Full package analysis