Skip to content

Commit

Permalink
feat(snippet): Allow user-provided rule for writing the snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Nov 16, 2014
1 parent dd9b284 commit 33c4586
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 63 deletions.
14 changes: 12 additions & 2 deletions lib/default-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,20 @@ module.exports = {

/**
* @property snippetOptions
* @param {Boolean} [ignorePaths]
* @param {String|Array} [ignorePaths=undefined]
* @param {RegExp} [regex.match=/<body[^>]*>/i]
* @param {Function} [regex.fn=function(snippet, match){ return match + snippet}]
* @type Object
* @since 1.7.0
*/
snippetOptions: {},
snippetOptions: {
rule: {
match: /<body[^>]*>/i,
fn: function (snippet, match) {
return match + snippet;
}
}
},

/**
* @property tunnel
Expand Down
2 changes: 1 addition & 1 deletion lib/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {}
);
Expand Down
22 changes: 8 additions & 14 deletions lib/snippet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: /<body[^>]*>/i,
fn: utils.appendSnippet(snippet)
match: options.rule.match,
fn: fn
};
},
/**
Expand All @@ -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
});
},
Expand Down
3 changes: 0 additions & 3 deletions test/protractor/tests/proxy.interactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
38 changes: 38 additions & 0 deletions test/specs/e2e/e2e.options.snippet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: /<head[^>]*>/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, "<head>" + instance.options.snippet);
done();
});
});
});
4 changes: 3 additions & 1 deletion test/specs/server/server.js
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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 () {
Expand Down
4 changes: 3 additions & 1 deletion test/specs/server/server.middleware.js
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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 () {
Expand Down
3 changes: 2 additions & 1 deletion test/specs/server/server.snippet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
40 changes: 0 additions & 40 deletions test/specs/snippet/snippet.js

This file was deleted.

0 comments on commit 33c4586

Please sign in to comment.