Skip to content

Commit

Permalink
Refactored path variable substitution - fixes #78
Browse files Browse the repository at this point in the history
  • Loading branch information
prakhar1989 committed May 14, 2014
1 parent 4c79f53 commit ea1709c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
12 changes: 7 additions & 5 deletions src/utilities/VariableProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ var VariableProcessor = jsface.Class({
$statics: {
ENV_REGEX: /\{\{([a-z0-9\-._]+)\}\}/ig,

// negative match for 4 digit numbers to weed out port number matches
PATH_REGEX: /\:(?![0-9]{4})+(([a-z0-9\-._]+))/,

FUNCTION_REGEX: /\{\{\$([a-z0-9\-._]+)\}\}/ig
},

Expand All @@ -30,8 +27,13 @@ var VariableProcessor = jsface.Class({
// updates request url by the replacing it with pathVariables
_processPathVariable: function(request) {
if (typeof request.pathVariables !== undefined) {
request.url = this._findReplace(request.url, request.pathVariables, this.PATH_REGEX);
}
var sourceObject = request.pathVariables;
// for each path variable - do a simple find replace
_und.each(_und.keys(sourceObject), function(key) {
var s = request.url;
request.url = s.replace(":" + key, sourceObject[key]);
}, this);
}
},

// updates request properties by the replacing them with function variables
Expand Down
15 changes: 12 additions & 3 deletions tests/utilities/VariableProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,24 @@ describe("Variable Processor", function() {

it("should replace available path variables correctly", function() {
var sampleReq = this.collectionJson.requests[0];
var anotherSample = this.collectionJson.requests[1];

sampleReq.url = "http://localhost:3000/blog/:user/posts/:post_id";
sampleReq.pathVariables = {user: "foo", post_id: 10};
sampleReq.url = "http://localhost:3000/blog/:user/posts/";
sampleReq.pathVariables = {user: "foo"};

anotherSample.url = "http://localhost:26069/values/one:two@three:four/resource/pathone/pathtwo/pathlast";
anotherSample.pathVariables = {};

VariableProcessor.processRequestVariables(sampleReq, {
envJson: this.environmentJson
});

assert.equal(sampleReq.transformed.url, "http://localhost:3000/blog/foo/posts/10");
VariableProcessor.processRequestVariables(anotherSample, {
envJson: this.environmentJson
});

assert.equal(sampleReq.transformed.url, "http://localhost:3000/blog/foo/posts/");
assert.equal(anotherSample.transformed.url, "http://localhost:26069/values/one:two@three:four/resource/pathone/pathtwo/pathlast");
});

it("should replace function variables correctly", function() {
Expand Down

0 comments on commit ea1709c

Please sign in to comment.