uri.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Utilities: A classic collection of JavaScript utilities
  3. * Copyright 2112 Matthew Eernisse ([email protected])
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. var uri = require('../lib/uri')
  19. , array = require('../lib/array')
  20. , assert = require('assert')
  21. , tests = {};
  22. tests = {
  23. 'test getFileExtension for uri': function () {
  24. var data = uri.getFileExtension('users.json')
  25. , actual = 'json';
  26. assert.equal(actual, data);
  27. }
  28. , 'test paramify for uri': function () {
  29. var data = uri.paramify({username: 'user', token: 'token', secret: 'secret'})
  30. , actual = 'username=user&token=token&secret=secret';
  31. assert.equal(actual, data);
  32. }
  33. , 'test paramify with conslidate option for uri': function () {
  34. var data = uri.paramify({username: 'user', auth: ['token', 'secret']}, {conslidate: true})
  35. , actual = 'username=user&auth=token&auth=secret';
  36. assert.equal(actual, data);
  37. }
  38. , 'test paramify with includeEmpty option for uri': function () {
  39. var data = uri.paramify({username: 'user', token: ''}, {includeEmpty: true})
  40. , actual = 'username=user&token=';
  41. assert.equal(actual, data);
  42. }
  43. , 'test paramify with includeEmpty as 0 option for uri': function () {
  44. var data = uri.paramify({username: 'user', token: 0}, {includeEmpty: true})
  45. , actual = 'username=user&token=0';
  46. assert.equal(actual, data);
  47. }
  48. , 'test paramify with includeEmpty as null option for uri': function () {
  49. var data = uri.paramify({username: 'user', token: null}, {includeEmpty: true})
  50. , actual = 'username=user&token=';
  51. assert.equal(actual, data);
  52. }
  53. , 'test paramify with includeEmpty as undefined option for uri': function () {
  54. var data = uri.paramify({username: 'user', token: undefined}, {includeEmpty: true})
  55. , actual = 'username=user&token=';
  56. assert.equal(actual, data);
  57. }
  58. , 'test paramify with snakeize option for uri': function () {
  59. var data = uri.paramify({username: 'user', authToken: 'token'}, {snakeize: true})
  60. , actual = 'username=user&auth_token=token';
  61. assert.equal(actual, data);
  62. }
  63. , 'test paramify with escapeVals option for uri': function () {
  64. var data = uri.paramify({username: 'user', token: '<token'}, {escapeVals: true})
  65. , actual = 'username=user&token=%26lt%3Btoken';
  66. assert.equal(actual, data);
  67. }
  68. , 'test paramify with a nested object': function() {
  69. var data = uri.paramify({name: {foo: 'bar', list: [2, 3, 4], obj: {a: 2, b: {c: 2}}}})
  70. , actual = 'name[foo]=bar&name[list]=2&name[list]=3&name[list]=4&name[obj][a]=2&name[obj][b][c]=2';
  71. assert.equal(actual, data);
  72. }
  73. , 'test paramify with a nested object and index option': function() {
  74. var data = uri.paramify({name: {foo: 'bar', list: [2, 3, 4], obj: {a: 2, b: {c: 2}}}}, { index: true })
  75. , actual = 'name[foo]=bar&name[list][0]=2&name[list][1]=3&name[list][2]=4&name[obj][a]=2&name[obj][b][c]=2';
  76. assert.equal(actual, data);
  77. }
  78. , 'test paramify with a nested array and index option': function() {
  79. var data = uri.paramify({foo: [['bar'], {a: 2, b: {c: 2}}]}, { index: true })
  80. , actual = 'foo[0][0]=bar&foo[1][a]=2&foo[1][b][c]=2';
  81. assert.equal(actual, data);
  82. }
  83. , 'test objectify for uri': function () {
  84. var expected = {name: 'user'}
  85. , actual = uri.objectify('name=user');
  86. assert.deepEqual(actual, expected);
  87. }
  88. , 'test objectify with multiple matching keys for uri': function () {
  89. var expected = {name: ['user', 'user2']}
  90. , actual = uri.objectify('name=user&name=user2');
  91. assert.deepEqual(actual, expected);
  92. }
  93. , 'test objectify with no conslidation for uri': function () {
  94. var expected= {name: 'user2'}
  95. , actual = uri.objectify('name=user&name=user2', {consolidate: false});
  96. assert.deepEqual(actual, expected);
  97. }
  98. };
  99. module.exports = tests;