diff --git a/lib/default-config.js b/lib/default-config.js index 1b687ea93..547f34914 100644 --- a/lib/default-config.js +++ b/lib/default-config.js @@ -118,10 +118,20 @@ module.exports = { /** * @property snippetOptions - * @param {Boolean} [ignorePaths] + * @param {String|Array} [ignorePaths=undefined] + * @param {RegExp} [regex.match=/]*>/i] + * @param {Function} [regex.fn=function(snippet, match){ return match + snippet}] * @type Object + * @since 1.7.0 */ - snippetOptions: {}, + snippetOptions: { + rule: { + match: /]*>/i, + fn: function (snippet, match) { + return match + snippet; + } + } + }, /** * @property tunnel diff --git a/lib/server/index.js b/lib/server/index.js index d87ff9775..28d9b6e56 100644 --- a/lib/server/index.js +++ b/lib/server/index.js @@ -196,7 +196,7 @@ module.exports.createProxy = function (options, scripts, bs) { return foxy.init( options.proxy.target, - snippetUtils.getRegex(options.snippet), + snippetUtils.getRegex(options.snippet, options.snippetOptions), snippetUtils.getProxyMiddleware(scripts, options.scriptPaths.versioned), function () {} ); diff --git a/lib/snippet.js b/lib/snippet.js index d53fa5499..f5b51a87a 100644 --- a/lib/snippet.js +++ b/lib/snippet.js @@ -33,21 +33,16 @@ var utils = { }, /** * @param {String} snippet - * @returns {Function} - */ - appendSnippet: function (snippet) { - return function (w) { - return w + snippet; - }; - }, - /** - * @param {String} snippet + * @param {Object} options * @returns {{match: RegExp, fn: Function}} */ - getRegex: function (snippet) { + getRegex: function (snippet, options) { + + var fn = options.rule.fn.bind(null, snippet); + return { - match: /]*>/i, - fn: utils.appendSnippet(snippet) + match: options.rule.match, + fn: fn }; }, /** @@ -57,11 +52,10 @@ var utils = { */ getSnippetMiddleware: function (snippet, options) { - var rules = [utils.getRegex(snippet)]; options = options || {}; return lrSnippet({ - rules: rules, + rules: [utils.getRegex(snippet, options)], ignorePaths: options.ignorePaths }); }, diff --git a/test/protractor/tests/proxy.interactions.js b/test/protractor/tests/proxy.interactions.js index 716dacdff..d2ac81d7c 100644 --- a/test/protractor/tests/proxy.interactions.js +++ b/test/protractor/tests/proxy.interactions.js @@ -45,9 +45,6 @@ describe('Interactions on proxy Pages', function() { }); it("should know when a client scrolls", function () { - var flow = protractor.promise.controlFlow(); - var deferred = protractor.promise.defer(); - instance.io.sockets.on("connection", function (client) { client.on("scroll", function (data) { expect(data.position.raw.y).toBe(100); diff --git a/test/specs/e2e/e2e.options.snippet.js b/test/specs/e2e/e2e.options.snippet.js index a62e61a17..ed9a80f15 100644 --- a/test/specs/e2e/e2e.options.snippet.js +++ b/test/specs/e2e/e2e.options.snippet.js @@ -38,3 +38,41 @@ describe("E2E snippet ignore paths test", function () { }); }); }); +describe("E2E snippet custom regex", function () { + + var instance; + + before(function (done) { + + var config = { + server: { + baseDir: "test/fixtures" + }, + open: false, + snippetOptions: { + rule: { + match: /]*>/i, + fn: function (snippet, match) { + return match + snippet; + } + } + } + }; + instance = browserSync(config, done); + }); + + after(function () { + instance.cleanup(); + }); + + it("uses a user-provided regex", function (done) { + request(instance.server) + .get("/iframe.html") + .set("accept", "text/html") + .expect(200) + .end(function (err, res) { + assert.include(res.text, "" + instance.options.snippet); + done(); + }); + }); +}); diff --git a/test/specs/server/server.js b/test/specs/server/server.js index 4896edb5f..317ab1caf 100644 --- a/test/specs/server/server.js +++ b/test/specs/server/server.js @@ -1,6 +1,7 @@ "use strict"; var server = require("../../../lib/server/"); +var defaultConfig = require("../../../lib/default-config"); var assert = require("chai").assert; var sinon = require("sinon"); @@ -35,7 +36,8 @@ describe("Server: The createServer method", function () { server: {}, port: 3000, version: "0.0.1", - host: "localhost" + host: "localhost", + snippetOptions: defaultConfig.snippetOptions }; }); afterEach(function () { diff --git a/test/specs/server/server.middleware.js b/test/specs/server/server.middleware.js index f3711f408..c6603e7c7 100644 --- a/test/specs/server/server.middleware.js +++ b/test/specs/server/server.middleware.js @@ -1,6 +1,7 @@ "use strict"; var server = require("../../../lib/server/"); +var defaultConfig = require("../../../lib/default-config"); var sinon = require("sinon"); var request = require("supertest"); @@ -9,7 +10,8 @@ var options = { host: "localhost", port: 3000, server: {}, - version: "2.1.0" + version: "2.1.0", + snippetOptions: defaultConfig.snippetOptions }; describe("Server: Adding custom middleware", function () { diff --git a/test/specs/server/server.snippet.js b/test/specs/server/server.snippet.js index a4633a294..96a2dd380 100644 --- a/test/specs/server/server.snippet.js +++ b/test/specs/server/server.snippet.js @@ -47,7 +47,8 @@ describe("Server: Launching a server with snippets", function () { injectScripts: true }, version: "0.1.0", - port: 3000 + port: 3000, + snippetOptions: defaultConfig.snippetOptions }; reqOptions = { hostname: "0.0.0.0", diff --git a/test/specs/snippet/snippet.js b/test/specs/snippet/snippet.js deleted file mode 100644 index 2e0e0bfb7..000000000 --- a/test/specs/snippet/snippet.js +++ /dev/null @@ -1,40 +0,0 @@ -"use strict"; - -var utils = require("../../../lib/snippet").utils; - -var assert = require("chai").assert; - -describe("Snippet: returning the snippet regex", function () { - it("should return a working regex", function () { - var actual = utils.getRegex(); - assert.isTrue(actual.match instanceof RegExp); - assert.isFunction(actual.fn); - }); - it("should append a snippet", function () { - var string = ""; - var snippet = "SNIPPET"; - var expected = "SNIPPET"; - var regex = utils.getRegex(snippet); - - var actual = string.replace(regex.match, regex.fn); - assert.equal(actual, expected); - }); - it("should append a snippet", function () { - var string = "

Here's an iframe

"; - var snippet = "SNIPPET"; - var expected = "SNIPPET

Here's an iframe

"; - var regex = utils.getRegex(snippet); - - var actual = string.replace(regex.match, regex.fn); - assert.equal(actual, expected); - }); - it("should append a snippet", function () { - var string = ""; - var snippet = "SNIPPET"; - var expected = "SNIPPET"; - var regex = utils.getRegex(snippet); - - var actual = string.replace(regex.match, regex.fn); - assert.equal(actual, expected); - }); -});