Skip to content

Commit

Permalink
Handling quotes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre Rodrigues committed Oct 10, 2012
1 parent 803e19b commit 30caaf2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
18 changes: 17 additions & 1 deletion lib/services/core/connectionstringparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* limitations under the License.
*/

var util = require('../../util/util');

// Expose 'ConnectionStringParser'.
exports = module.exports = ConnectionStringParser;

Expand All @@ -32,6 +34,7 @@ function ConnectionStringParser(connectionString) {
}

ConnectionStringParser.prototype._parse = function () {
var self = this;
var parts = this._connectionString.split(';');
var parsedConnectionString = { };

Expand All @@ -41,12 +44,25 @@ ConnectionStringParser.prototype._parse = function () {
if (part) {
var currentKVP = part.split('=');
if (currentKVP.length === 2) {
parsedConnectionString[currentKVP[0].toLowerCase()] = currentKVP[1];
if (currentKVP[0].length > 0) {
parsedConnectionString[self._getValue(currentKVP[0].toLowerCase())] = self._getValue(currentKVP[1]);
} else {
throw new Error('Invalid key');
}
} else {
throw new Error('Invalid connection string');
}
}
});

return parsedConnectionString;
};

ConnectionStringParser.prototype._getValue = function (str) {
if ((util.stringStartsWith(str, '"') && util.stringEndsWith(str, '"')) ||
(util.stringStartsWith(str, '\'') && util.stringEndsWith(str, '\''))) {
return str.substring(1, str.length - 1);
}

return str;
};
30 changes: 29 additions & 1 deletion test/services/core/connectionstringparser-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ suite('connectionstringparser-tests', function () {
done();
});

test('parseInvalid', function (done) {
test('parseInvalidAssignment', function (done) {
// no assignment
assert.throws(
function() {
Expand All @@ -54,4 +54,32 @@ suite('connectionstringparser-tests', function () {

done();
});

test('parseInvalidKey', function (done) {
assert.throws(
function() {
var parsedConnectionString = ConnectionStringParser.parse('=value');
},
Error
);

assert.throws(
function() {
var parsedConnectionString = ConnectionStringParser.parse(' =value');
},
Error
);

done();
});

test('parseQuotedValues', function (done) {
var parsedConnectionString = ConnectionStringParser.parse('"test"=\'value\'');
assert.equal(parsedConnectionString['test'], 'value');

var parsedConnectionString = ConnectionStringParser.parse('\'test\'="value"');
assert.equal(parsedConnectionString['test'], 'value');

done();
});
});

0 comments on commit 30caaf2

Please sign in to comment.