Permalink
Browse files

Added uri tests

  • Loading branch information...
1 parent c9d51ba commit fc310a0520c78d039c3e3510f8bac95530bcebc9 mde committed Aug 4, 2012
Showing with 145 additions and 5 deletions.
  1. +7 −4 lib/uri.js
  2. +1 −1 test/string.js
  3. +137 −0 test/uri.js
View
@@ -15,7 +15,10 @@
* limitations under the License.
t
*/
-var uri = new (function () {
+var uri
+ , string = require('./string');
+
+uri = new (function () {
var _isArray = function (obj) {
return obj &&
typeof obj === 'object' &&
@@ -76,7 +79,7 @@ var uri = new (function () {
isValid = !( val === null || val === undefined ||
(typeof val === 'number' && isNaN(val)) );
- key = opts.snakeize ? geddy.string.snakeize(p) : p;
+ key = opts.snakeize ? string.snakeize(p) : p;
if (isValid) {
// Multiple vals -- array
if (_isArray(val) && val.length) {
@@ -89,7 +92,7 @@ var uri = new (function () {
itemArray[i] = isValid ? encodeURIComponent(arrVal) : '';
if (opts.escapeVals) {
- itemArray[i] = geddy.string.escapeXML(itemArray[i]);
+ itemArray[i] = string.escapeXML(itemArray[i]);
}
}
// Consolidation mode -- single value joined on comma
@@ -107,7 +110,7 @@ var uri = new (function () {
// Single val -- string
else {
if (opts.escapeVals) {
- val = geddy.string.escapeXML(val);
+ val = string.escapeXML(val);
}
arr.push(key + '=' + encodeURIComponent(val));
}
View
@@ -253,7 +253,7 @@ tests = {
},
};
- checkObjects(expected, actual);
+ assert.deepEqual(expected, actual);
}
, 'test uuid length for string': function() {
View
@@ -0,0 +1,137 @@
+/*
+ * Utilities: A classic collection of JavaScript utilities
+ * Copyright 2112 Matthew Eernisse (mde@fleegix.org)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+*/
+var uri = require('../lib/uri')
+ , array = require('../lib/array')
+ , assert = require('assert')
+ , tests = {}
+ , checkObjects;
+
+// Check if two objects are similar
+checkObjects = function(first, second) {
+ var finished = [];
+
+ for(var i in first) {
+ if((typeof first[i] === 'object') && (typeof second[i] === 'object')) {
+ finished.push(checkObjects(first[i], second[i]));
+ } else {
+ if(first[i] instanceof Array) {
+ var arr = [];
+
+ for(var p in first[i]) {
+ if(first[i][p] === second[i][p]) {
+ arr.push(true)
+ } else {
+ arr.push(false)
+ }
+ }
+
+ if(array.included(false, finished)) {
+ return false;
+ }
+ return true;
+ } else if(first[i] === second[i]) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+ }
+
+ if(array.included(false, finished)) {
+ return false;
+ }
+ return true;
+};
+
+tests = {
+
+ 'test getFileExtension for uri': function() {
+ var data = uri.getFileExtension('users.json')
+ , actual = 'json';
+ assert.equal(data, actual);
+ }
+
+, 'test paramify for uri': function() {
+ var data = uri.paramify({username: 'user', token: 'token', secret: 'secret'})
+ , actual = 'username=user&token=token&secret=secret';
+ assert.equal(data, actual);
+ }
+
+, 'test paramify with conslidate option for uri': function() {
+ var data = uri.paramify({username: 'user', auth: ['token', 'secret']}, {conslidate: true})
+ , actual = 'username=user&auth=token&auth=secret';
+ assert.equal(data, actual);
+ }
+
+, 'test paramify with includeEmpty option for uri': function() {
+ var data = uri.paramify({username: 'user', token: ''}, {includeEmpty: true})
+ , actual = 'username=user&token=';
+ assert.equal(data, actual);
+ }
+
+, 'test paramify with includeEmpty as 0 option for uri': function() {
+ var data = uri.paramify({username: 'user', token: 0}, {includeEmpty: true})
+ , actual = 'username=user&token=0';
+ assert.equal(data, actual);
+ }
+
+, 'test paramify with includeEmpty as null option for uri': function() {
+ var data = uri.paramify({username: 'user', token: null}, {includeEmpty: true})
+ , actual = 'username=user&token=';
+ assert.equal(data, actual);
+ }
+
+, 'test paramify with includeEmpty as undefined option for uri': function() {
+ var data = uri.paramify({username: 'user', token: undefined}, {includeEmpty: true})
+ , actual = 'username=user&token=';
+ assert.equal(data, actual);
+ }
+
+, 'test paramify with snakeize option for uri': function() {
+ var data = uri.paramify({username: 'user', authToken: 'token'}, {snakeize: true})
+ , actual = 'username=user&auth_token=token';
+ assert.equal(data, actual);
+ }
+
+, 'test paramify with escapeVals option for uri': function() {
+ var data = uri.paramify({username: 'user', token: '<token'}, {escapeVals: true})
+ , actual = 'username=user&token=%26lt%3Btoken';
+ assert.equal(data, actual);
+ }
+
+, 'test objectify for uri': function() {
+ var data = uri.objectify('name=user')
+ , actual = {name: 'user'};
+ assert.equal(checkObjects(data, actual), true);
+ }
+
+, 'test objectify with multiple matching keys for uri': function() {
+ var data = uri.objectify('name=user&name=user2')
+ , actual = {name: ['user', 'user2']};
+ assert.equal(checkObjects(data, actual), true);
+ }
+
+, 'test objectify with no conslidation for uri': function() {
+ var data = uri.objectify('name=user&name=user2', {conslidate: false})
+ , actual = {name: 'user2'};
+ assert.equal(checkObjects(data, actual), true);
+ }
+
+};
+
+module.exports = tests;

0 comments on commit fc310a0

Please sign in to comment.