UNPKG

@pact-foundation/pact

Version:
58 lines 2.48 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const nock_1 = __importDefault(require("nock")); const request_1 = require("./request"); describe('Request', () => { let request; const port = 1024 + Math.floor(Math.random() * 5000); const url = `http://localhost:${port}`; const urlSecure = `https://localhost:${port}`; beforeEach(() => { request = new request_1.Request(); }); describe('#send', () => { afterEach(() => nock_1.default.cleanAll()); describe('Promise', () => { it('returns a promise', async () => { (0, nock_1.default)(url).get('/').reply(200); const r = request.send(request_1.HTTPMethods.GET, url); expect(r).toBeTruthy(); expect(r.then).toBeTruthy(); expect(r.then).toBeTypeOf('function'); await r; }); it('resolves when request succeeds with response body', async () => { const body = 'body'; (0, nock_1.default)(url).get('/').reply(200, body); await expect(request.send(request_1.HTTPMethods.GET, url)).resolves.toBe(body); }); it('rejects when request fails with error message', async () => { const error = 'error'; (0, nock_1.default)(url).get('/').reply(400, error); await expect(request.send(request_1.HTTPMethods.GET, url)).rejects.toThrow(error); }); }); describe('Headers', () => { it('sends Pact headers are sent with every request', async () => { (0, nock_1.default)(url) .matchHeader('X-Pact-Mock-Service', 'true') .get('/') .reply(200); await request.send(request_1.HTTPMethods.GET, url); }); }); describe('SSL', () => { it('ignores self signed certificate errors', async () => { (0, nock_1.default)(urlSecure) .matchHeader('X-Pact-Mock-Service', 'true') .get('/') .reply(200); await request.send(request_1.HTTPMethods.GET, urlSecure); }); }); }); }); //# sourceMappingURL=request.spec.js.map