From 706372fc5e9cbf8b7deb444240f9a9edaa243457 Mon Sep 17 00:00:00 2001 From: Andreas Lind <andreas@one.com> Date: Thu, 25 Aug 2016 12:05:57 +0200 Subject: [PATCH 1/2] with http recorded: Preserve the fulfilment value of the assertion being delegated to, add support for 'with extra info' to be used in the test suite. --- lib/unexpectedMitm.js | 14 +++++++++----- test/unexpectedMitm.js | 11 +++++++++-- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/lib/unexpectedMitm.js b/lib/unexpectedMitm.js index a244be9..4e9eb47 100644 --- a/lib/unexpectedMitm.js +++ b/lib/unexpectedMitm.js @@ -696,13 +696,13 @@ module.exports = { expect.promise(function () { return expect.shift(); - }).caught(reject).then(function () { + }).caught(reject).then(function (value) { recordedExchanges = recordedExchanges.map(trimRecordedExchange); if (recordedExchanges.length === 1) { recordedExchanges = recordedExchanges[0]; } - resolve(recordedExchanges); + resolve([value, recordedExchanges]); }); }).finally(function () { mitm.disable(); @@ -712,7 +712,7 @@ module.exports = { var afterBlockRegistered = false; expect - .addAssertion('<any> with http recorded [and injected] <assertion>', function (expect, subject) { + .addAssertion('<any> with http recorded [and injected] [with extra info] <assertion>', function (expect, subject) { var stack = callsite(), injectIntoTest = this.flags['and injected']; @@ -721,14 +721,18 @@ module.exports = { afterBlockRegistered = true; } - return executeMitm(expect, subject).then(function (recordedExchanges) { + return executeMitm(expect, subject).spread(function (value, recordedExchanges) { if (injectIntoTest) { var injectionCallsite = determineInjectionCallsite(stack); if (injectionCallsite) { recordPendingInjection(injectionCallsite, recordedExchanges); } } - return recordedExchanges; + if (expect.flags['with extra info']) { + return [value, recordedExchanges]; + } else { + return value; + } }); }) .addAssertion('<any> with http mocked out [and verified] [with extra info] <array|object> <assertion>', function (expect, subject, requestDescriptions) { // ... diff --git a/test/unexpectedMitm.js b/test/unexpectedMitm.js index 13fcc98..22434fd 100644 --- a/test/unexpectedMitm.js +++ b/test/unexpectedMitm.js @@ -35,11 +35,12 @@ describe('unexpectedMitm', function () { .use(require('unexpected-sinon')) .addAssertion('<any> with expected http recording <object> <assertion>', function (expect, subject, expectedRecordedExchanges) { // ... expect.errorMode = 'nested'; - expect.args.splice(1, 0, 'with http recorded'); + expect.args.splice(1, 0, 'with http recorded with extra info'); return expect.promise(function () { return expect.shift(); - }).then(function (recordedExchanges) { + }).spread(function (value, recordedExchanges) { expect(recordedExchanges, 'to equal', expectedRecordedExchanges); + return value; }); }) .addAssertion('<string> when injected becomes <string>', function (expect, subject, expectedFileName) { @@ -1335,6 +1336,12 @@ describe('unexpectedMitm', function () { }, 'to yield response', 405); }); + it('should preserve the fulfilment value', function () { + return expect('foo', 'with http recorded', 'to match', /^(f)o/).then(function (matches) { + expect(matches, 'to satisfy', {0: 'fo', 1: 'f', index: 0}); + }); + }); + it('should record an error', function () { var expectedError; // I do not know the exact version where this change was introduced. Hopefully this is enough to get From e1b2954f248000caf6ee965d830db7cc82e84ea4 Mon Sep 17 00:00:00 2001 From: Andreas Lind <andreas@one.com> Date: Thu, 25 Aug 2016 13:29:19 +0200 Subject: [PATCH 2/2] Correct documentation that relied on the fulfilment value of 'with http recorded' being the recorded exchanges. --- documentation/assertions/any/with-http-recorded.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/documentation/assertions/any/with-http-recorded.md b/documentation/assertions/any/with-http-recorded.md index 2da5a46..6d6510e 100644 --- a/documentation/assertions/any/with-http-recorded.md +++ b/documentation/assertions/any/with-http-recorded.md @@ -10,8 +10,9 @@ describe('requests to a popular web search service', function () { it('should have a Content-Type', function () { return expect({ url: 'GET https://www.google.co.uk', - }, 'with http recorded', 'to yield response', 200).then(function (exchange) { - expect(exchange.response.headers, 'to have property', 'Content-Type'); + }, 'with http recorded', 'to yield response', 200).then(function (context) { + // context is provided by unexpected-http: + expect(context.httpResponse.headers, 'to satisfy', {'Content-Type': /html/}); }); }); });