metadata_test.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. 'use strict';
  34. var Metadata = require('../src/metadata.js');
  35. var assert = require('assert');
  36. describe('Metadata', function() {
  37. var metadata;
  38. beforeEach(function() {
  39. metadata = new Metadata();
  40. });
  41. describe('#set', function() {
  42. it('Only accepts string values for non "-bin" keys', function() {
  43. assert.throws(function() {
  44. metadata.set('key', new Buffer('value'));
  45. });
  46. assert.doesNotThrow(function() {
  47. metadata.set('key', 'value');
  48. });
  49. });
  50. it('Only accepts Buffer values for "-bin" keys', function() {
  51. assert.throws(function() {
  52. metadata.set('key-bin', 'value');
  53. });
  54. assert.doesNotThrow(function() {
  55. metadata.set('key-bin', new Buffer('value'));
  56. });
  57. });
  58. it('Rejects invalid keys', function() {
  59. assert.throws(function() {
  60. metadata.set('key$', 'value');
  61. });
  62. assert.throws(function() {
  63. metadata.set('', 'value');
  64. });
  65. });
  66. it('Rejects values with non-ASCII characters', function() {
  67. assert.throws(function() {
  68. metadata.set('key', 'résumé');
  69. });
  70. });
  71. it('Saves values that can be retrieved', function() {
  72. metadata.set('key', 'value');
  73. assert.deepEqual(metadata.get('key'), ['value']);
  74. });
  75. it('Overwrites previous values', function() {
  76. metadata.set('key', 'value1');
  77. metadata.set('key', 'value2');
  78. assert.deepEqual(metadata.get('key'), ['value2']);
  79. });
  80. it('Normalizes keys', function() {
  81. metadata.set('Key', 'value1');
  82. assert.deepEqual(metadata.get('key'), ['value1']);
  83. metadata.set('KEY', 'value2');
  84. assert.deepEqual(metadata.get('key'), ['value2']);
  85. });
  86. });
  87. describe('#add', function() {
  88. it('Only accepts string values for non "-bin" keys', function() {
  89. assert.throws(function() {
  90. metadata.add('key', new Buffer('value'));
  91. });
  92. assert.doesNotThrow(function() {
  93. metadata.add('key', 'value');
  94. });
  95. });
  96. it('Only accepts Buffer values for "-bin" keys', function() {
  97. assert.throws(function() {
  98. metadata.add('key-bin', 'value');
  99. });
  100. assert.doesNotThrow(function() {
  101. metadata.add('key-bin', new Buffer('value'));
  102. });
  103. });
  104. it('Rejects invalid keys', function() {
  105. assert.throws(function() {
  106. metadata.add('key$', 'value');
  107. });
  108. assert.throws(function() {
  109. metadata.add('', 'value');
  110. });
  111. });
  112. it('Saves values that can be retrieved', function() {
  113. metadata.add('key', 'value');
  114. assert.deepEqual(metadata.get('key'), ['value']);
  115. });
  116. it('Combines with previous values', function() {
  117. metadata.add('key', 'value1');
  118. metadata.add('key', 'value2');
  119. assert.deepEqual(metadata.get('key'), ['value1', 'value2']);
  120. });
  121. it('Normalizes keys', function() {
  122. metadata.add('Key', 'value1');
  123. assert.deepEqual(metadata.get('key'), ['value1']);
  124. metadata.add('KEY', 'value2');
  125. assert.deepEqual(metadata.get('key'), ['value1', 'value2']);
  126. });
  127. });
  128. describe('#remove', function() {
  129. it('clears values from a key', function() {
  130. metadata.add('key', 'value');
  131. metadata.remove('key');
  132. assert.deepEqual(metadata.get('key'), []);
  133. });
  134. it('Normalizes keys', function() {
  135. metadata.add('key', 'value');
  136. metadata.remove('KEY');
  137. assert.deepEqual(metadata.get('key'), []);
  138. });
  139. });
  140. describe('#get', function() {
  141. beforeEach(function() {
  142. metadata.add('key', 'value1');
  143. metadata.add('key', 'value2');
  144. metadata.add('key-bin', new Buffer('value'));
  145. });
  146. it('gets all values associated with a key', function() {
  147. assert.deepEqual(metadata.get('key'), ['value1', 'value2']);
  148. });
  149. it('Normalizes keys', function() {
  150. assert.deepEqual(metadata.get('KEY'), ['value1', 'value2']);
  151. });
  152. it('returns an empty list for non-existent keys', function() {
  153. assert.deepEqual(metadata.get('non-existent-key'), []);
  154. });
  155. it('returns Buffers for "-bin" keys', function() {
  156. assert(metadata.get('key-bin')[0] instanceof Buffer);
  157. });
  158. });
  159. describe('#getMap', function() {
  160. it('gets a map of keys to values', function() {
  161. metadata.add('key1', 'value1');
  162. metadata.add('Key2', 'value2');
  163. metadata.add('KEY3', 'value3');
  164. assert.deepEqual(metadata.getMap(),
  165. {key1: 'value1',
  166. key2: 'value2',
  167. key3: 'value3'});
  168. });
  169. });
  170. describe('#clone', function() {
  171. it('retains values from the original', function() {
  172. metadata.add('key', 'value');
  173. var copy = metadata.clone();
  174. assert.deepEqual(copy.get('key'), ['value']);
  175. });
  176. it('Does not see newly added values', function() {
  177. metadata.add('key', 'value1');
  178. var copy = metadata.clone();
  179. metadata.add('key', 'value2');
  180. assert.deepEqual(copy.get('key'), ['value1']);
  181. });
  182. it('Does not add new values to the original', function() {
  183. metadata.add('key', 'value1');
  184. var copy = metadata.clone();
  185. copy.add('key', 'value2');
  186. assert.deepEqual(metadata.get('key'), ['value1']);
  187. });
  188. });
  189. });