http-fake

0.0.16 • Public • Published

Small Node.js http.request emulator

I use it in unit tests.

describe('HttpFake', () => {

    const HttpFake = require('../src/HttpFake'),
        options = {
            hostname: 'localhost',
            port: 80,
            path: '/api/users/login',
            body: {
                login: 'jack',
                password: 'P@ssw0rd'
            },
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            }
        },
        response = {
            statusCode: 200,
            body: {
                token: 'value is here'
            },
            headers: {
                'Content-Type': 'application/json'
            }
        };
    let http = null;

    beforeEach(() => {
       http = new HttpFake();
    });

    it('should pass', () => {    
    
        http.expect(options);
        http.returns(response);
    
        const clientRequest = http.request(options, (res) => {
    
            expect(res.headers).toEqual(response.headers);
            expect(res.statusCode).toEqual(response.statusCode);
    
            res.on('data', (chunk) => {
    
                const body = JSON.parse(chunk);
                expect(body).toEqual(response.body);
            });
        });
    
        const content = JSON.stringify(options.body);
        clientRequest.write(content);
        clientRequest.end();
    });
});

Package Sidebar

Install

npm i http-fake

Weekly Downloads

2

Version

0.0.16

License

ISC

Unpacked Size

34.8 kB

Total Files

31

Last publish

Collaborators

  • egorov