0.0.16 • Published 4 years ago

http-fake v0.0.16

Weekly downloads
1
License
ISC
Repository
github
Last release
4 years ago

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();
    });
});
0.0.12

4 years ago

0.0.13

4 years ago

0.0.14

4 years ago

0.0.15

4 years ago

0.0.16

4 years ago

0.0.11

5 years ago

0.0.10

6 years ago

0.0.9

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago