internationalizedemailaddress_test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // Copyright 2014 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.format.InternationalizedEmailAddressTest');
  15. goog.setTestOnly('goog.format.InternationalizedEmailAddressTest');
  16. goog.require('goog.array');
  17. goog.require('goog.format.InternationalizedEmailAddress');
  18. goog.require('goog.testing.jsunit');
  19. /**
  20. * Asserts that the given validation function generates the expected outcome for
  21. * a set of expected valid and a second set of expected invalid addresses.
  22. * containing the specified address strings, irrespective of their order.
  23. * @param {function(string):boolean} testFunc Validation function to be tested.
  24. * @param {!Array<string>} valid List of addresses that should be valid.
  25. * @param {!Array<string>} invalid List of addresses that should be invalid.
  26. * @private
  27. */
  28. function doIsValidTest(testFunc, valid, invalid) {
  29. goog.array.forEach(valid, function(str) {
  30. assertTrue('"' + str + '" should be valid.', testFunc(str));
  31. });
  32. goog.array.forEach(invalid, function(str) {
  33. assertFalse('"' + str + '" should be invalid.', testFunc(str));
  34. });
  35. }
  36. /**
  37. * Asserts that parsing the inputString produces a list of email addresses
  38. * containing the specified address strings, irrespective of their order.
  39. * @param {string} inputString A raw address list.
  40. * @param {!Array<string>} expectedList The expected results.
  41. * @param {string=} opt_message An assertion message.
  42. * @return {string} the resulting email address objects.
  43. */
  44. function assertParsedList(inputString, expectedList, opt_message) {
  45. var message = opt_message || 'Should parse address correctly';
  46. var result = goog.format.InternationalizedEmailAddress.parseList(inputString);
  47. assertEquals(
  48. 'Should have correct # of addresses', expectedList.length, result.length);
  49. for (var i = 0; i < expectedList.length; ++i) {
  50. assertEquals(message, expectedList[i], result[i].getAddress());
  51. }
  52. return result;
  53. }
  54. function testParseList() {
  55. // Test only the new cases added by EAI (other cases covered in parent
  56. // class test)
  57. assertParsedList('<me.みんあ@me.xn--l8jtg9b>', ['me.みんあ@me.xn--l8jtg9b']);
  58. }
  59. function testIsEaiValid() {
  60. var valid = [
  61. 'e@b.eu', '<a.b+foo@c.com>', 'eric <e@b.com>', '"e" <e@b.com>',
  62. 'a@FOO.MUSEUM', 'bla@b.co.ac.uk', 'bla@a.b.com', 'o\'hara@gm.com',
  63. 'plus+is+allowed@gmail.com', '!/#$%&\'*+-=~|`{}?^_@expample.com',
  64. 'confirm-bhk=modulo.org@yahoogroups.com', 'み.ん-あ@みんあ.みんあ',
  65. 'みんあ@test.com', 'test@test.みんあ', 'test@みんあ.com',
  66. 'me.みんあ@me.xn--l8jtg9b', 'みんあ@me.xn--l8jtg9b',
  67. 'fullwidthfullstop@sld' +
  68. '\uff0e' +
  69. 'tld',
  70. 'ideographicfullstop@sld' +
  71. '\u3002' +
  72. 'tld',
  73. 'halfwidthideographicfullstop@sld' +
  74. '\uff61' +
  75. 'tld'
  76. ];
  77. var invalid = [
  78. null, undefined, 'e', '', 'e @c.com', 'a@b', 'foo.com', 'foo@c..com',
  79. 'test@gma=il.com', 'aaa@gmail', 'has some spaces@gmail.com',
  80. 'has@three@at@signs.com', '@no-local-part.com'
  81. ];
  82. doIsValidTest(
  83. goog.format.InternationalizedEmailAddress.isValidAddress, valid, invalid);
  84. }
  85. function testIsValidLocalPart() {
  86. var valid = [
  87. 'e', 'a.b+foo', 'o\'hara', 'user+someone', '!/#$%&\'*+-=~|`{}?^_',
  88. 'confirm-bhk=modulo.org', 'me.みんあ', 'みんあ'
  89. ];
  90. var invalid = [
  91. null, undefined, 'A@b@c', 'a"b(c)d,e:f;g<h>i[j\\k]l', 'just"not"right',
  92. 'this is"not\\allowed', 'this\\ still\"not\\\\allowed', 'has some spaces'
  93. ];
  94. doIsValidTest(
  95. goog.format.InternationalizedEmailAddress.isValidLocalPartSpec, valid,
  96. invalid);
  97. }
  98. function testIsValidDomainPart() {
  99. var valid = [
  100. 'example.com', 'dept.example.org', 'long.domain.with.lots.of.dots',
  101. 'me.xn--l8jtg9b', 'me.みんあ', 'sld.looooooongtld', 'sld' +
  102. '\uff0e' +
  103. 'tld',
  104. 'sld' +
  105. '\u3002' +
  106. 'tld',
  107. 'sld' +
  108. '\uff61' +
  109. 'tld'
  110. ];
  111. var invalid = [
  112. null, undefined, '', '@has.an.at.sign', '..has.leading.dots', 'gma=il.com',
  113. 'DoesNotHaveADot',
  114. 'aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffffgggggggggg'
  115. ];
  116. doIsValidTest(
  117. goog.format.InternationalizedEmailAddress.isValidDomainPartSpec, valid,
  118. invalid);
  119. }
  120. function testparseListWithAdditionalSeparators() {
  121. assertParsedList(
  122. '<foo@gmail.com>\u055D <bar@gmail.com>',
  123. ['foo@gmail.com', 'bar@gmail.com'],
  124. 'Failed to parse 2 email addresses with U+055D');
  125. assertParsedList(
  126. '<foo@gmail.com>\u055D <bar@gmail.com>\u055D',
  127. ['foo@gmail.com', 'bar@gmail.com'],
  128. 'Failed to parse 2 email addresses with trailing U+055D');
  129. assertParsedList(
  130. '<foo@gmail.com>\u060C <bar@gmail.com>',
  131. ['foo@gmail.com', 'bar@gmail.com'],
  132. 'Failed to parse 2 email addresses with U+060C');
  133. assertParsedList(
  134. '<foo@gmail.com>\u060C <bar@gmail.com>\u060C',
  135. ['foo@gmail.com', 'bar@gmail.com'],
  136. 'Failed to parse 2 email addresses with trailing U+060C');
  137. assertParsedList(
  138. '<foo@gmail.com>\u1363 <bar@gmail.com>',
  139. ['foo@gmail.com', 'bar@gmail.com'],
  140. 'Failed to parse 2 email addresses with U+1363');
  141. assertParsedList(
  142. '<foo@gmail.com>\u1363 <bar@gmail.com>\u1363',
  143. ['foo@gmail.com', 'bar@gmail.com'],
  144. 'Failed to parse 2 email addresses with trailing U+1363');
  145. assertParsedList(
  146. '<foo@gmail.com>\u1802 <bar@gmail.com>',
  147. ['foo@gmail.com', 'bar@gmail.com'],
  148. 'Failed to parse 2 email addresses with U+1802');
  149. assertParsedList(
  150. '<foo@gmail.com>\u1802 <bar@gmail.com>\u1802',
  151. ['foo@gmail.com', 'bar@gmail.com'],
  152. 'Failed to parse 2 email addresses with trailing U+1802');
  153. assertParsedList(
  154. '<foo@gmail.com>\u1808 <bar@gmail.com>',
  155. ['foo@gmail.com', 'bar@gmail.com'],
  156. 'Failed to parse 2 email addresses with U+1808');
  157. assertParsedList(
  158. '<foo@gmail.com>\u1808 <bar@gmail.com>\u1808',
  159. ['foo@gmail.com', 'bar@gmail.com'],
  160. 'Failed to parse 2 email addresses with trailing U+1808');
  161. assertParsedList(
  162. '<foo@gmail.com>\u2E41 <bar@gmail.com>',
  163. ['foo@gmail.com', 'bar@gmail.com'],
  164. 'Failed to parse 2 email addresses with U+2E41');
  165. assertParsedList(
  166. '<foo@gmail.com>\u2E41 <bar@gmail.com>\u2E41',
  167. ['foo@gmail.com', 'bar@gmail.com'],
  168. 'Failed to parse 2 email addresses with trailing U+2E41');
  169. assertParsedList(
  170. '<foo@gmail.com>\u3001 <bar@gmail.com>',
  171. ['foo@gmail.com', 'bar@gmail.com'],
  172. 'Failed to parse 2 email addresses with U+3001');
  173. assertParsedList(
  174. '<foo@gmail.com>\u3001 <bar@gmail.com>\u3001',
  175. ['foo@gmail.com', 'bar@gmail.com'],
  176. 'Failed to parse 2 email addresses with trailing U+3001');
  177. assertParsedList(
  178. '<foo@gmail.com>\uFF0C <bar@gmail.com>',
  179. ['foo@gmail.com', 'bar@gmail.com'],
  180. 'Failed to parse 2 email addresses with U+FF0C');
  181. assertParsedList(
  182. '<foo@gmail.com>\uFF0C <bar@gmail.com>\uFF0C',
  183. ['foo@gmail.com', 'bar@gmail.com'],
  184. 'Failed to parse 2 email addresses with trailing U+FF0C');
  185. assertParsedList(
  186. '<foo@gmail.com>\u0613 <bar@gmail.com>',
  187. ['foo@gmail.com', 'bar@gmail.com'],
  188. 'Failed to parse 2 email addresses with U+0613');
  189. assertParsedList(
  190. '<foo@gmail.com>\u0613 <bar@gmail.com>\u0613',
  191. ['foo@gmail.com', 'bar@gmail.com'],
  192. 'Failed to parse 2 email addresses with trailing U+0613');
  193. assertParsedList(
  194. '<foo@gmail.com>\u1364 <bar@gmail.com>',
  195. ['foo@gmail.com', 'bar@gmail.com'],
  196. 'Failed to parse 2 email addresses with U+1364');
  197. assertParsedList(
  198. '<foo@gmail.com>\u1364 <bar@gmail.com>\u1364',
  199. ['foo@gmail.com', 'bar@gmail.com'],
  200. 'Failed to parse 2 email addresses with trailing U+1364');
  201. assertParsedList(
  202. '<foo@gmail.com>\uFF1B <bar@gmail.com>',
  203. ['foo@gmail.com', 'bar@gmail.com'],
  204. 'Failed to parse 2 email addresses with U+FF1B');
  205. assertParsedList(
  206. '<foo@gmail.com>\uFF1B <bar@gmail.com>\uFF1B',
  207. ['foo@gmail.com', 'bar@gmail.com'],
  208. 'Failed to parse 2 email addresses with trailing U+FF1B');
  209. assertParsedList(
  210. '<foo@gmail.com>\uFF64 <bar@gmail.com>',
  211. ['foo@gmail.com', 'bar@gmail.com'],
  212. 'Failed to parse 2 email addresses with U+FF64');
  213. assertParsedList(
  214. '<foo@gmail.com>\uFF64 <bar@gmail.com>\uFF64',
  215. ['foo@gmail.com', 'bar@gmail.com'],
  216. 'Failed to parse 2 email addresses with trailing U+FF64');
  217. assertParsedList(
  218. '<foo@gmail.com>\u104A <bar@gmail.com>',
  219. ['foo@gmail.com', 'bar@gmail.com'],
  220. 'Failed to parse 2 email addresses with U+104A');
  221. assertParsedList(
  222. '<foo@gmail.com>\u104A <bar@gmail.com>\u104A',
  223. ['foo@gmail.com', 'bar@gmail.com'],
  224. 'Failed to parse 2 email addresses with trailing U+104A');
  225. }
  226. function testToString() {
  227. var f = function(str) {
  228. return goog.format.InternationalizedEmailAddress.parse(str).toString();
  229. };
  230. // No modification.
  231. assertEquals('JOHN Doe <john@gmail.com>', f('JOHN Doe <john@gmail.com>'));
  232. // Extra spaces.
  233. assertEquals('JOHN Doe <john@gmail.com>', f(' JOHN Doe <john@gmail.com> '));
  234. // No name.
  235. assertEquals('john@gmail.com', f('<john@gmail.com>'));
  236. assertEquals('john@gmail.com', f('john@gmail.com'));
  237. // No address.
  238. assertEquals('JOHN Doe', f('JOHN Doe <>'));
  239. // Already quoted.
  240. assertEquals(
  241. '"JOHN, Doe" <john@gmail.com>', f('"JOHN, Doe" <john@gmail.com>'));
  242. // Needless quotes.
  243. assertEquals('JOHN Doe <john@gmail.com>', f('"JOHN Doe" <john@gmail.com>'));
  244. // Not quoted-string, but has double quotes.
  245. assertEquals(
  246. '"JOHN, Doe" <john@gmail.com>', f('JOHN, "Doe" <john@gmail.com>'));
  247. // No special characters other than quotes.
  248. assertEquals('JOHN Doe <john@gmail.com>', f('JOHN "Doe" <john@gmail.com>'));
  249. // Escaped quotes are also removed.
  250. assertEquals(
  251. '"JOHN, Doe" <john@gmail.com>', f('JOHN, \\"Doe\\" <john@gmail.com>'));
  252. // Characters that require quoting for the display name.
  253. assertEquals('"JOHN, Doe" <john@gmail.com>', f('JOHN, Doe <john@gmail.com>'));
  254. assertEquals('"JOHN; Doe" <john@gmail.com>', f('JOHN; Doe <john@gmail.com>'));
  255. assertEquals(
  256. '"JOHN\u055D Doe" <john@gmail.com>',
  257. f('JOHN\u055D Doe <john@gmail.com>'));
  258. assertEquals(
  259. '"JOHN\u060C Doe" <john@gmail.com>',
  260. f('JOHN\u060C Doe <john@gmail.com>'));
  261. assertEquals(
  262. '"JOHN\u1363 Doe" <john@gmail.com>',
  263. f('JOHN\u1363 Doe <john@gmail.com>'));
  264. assertEquals(
  265. '"JOHN\u1802 Doe" <john@gmail.com>',
  266. f('JOHN\u1802 Doe <john@gmail.com>'));
  267. assertEquals(
  268. '"JOHN\u1808 Doe" <john@gmail.com>',
  269. f('JOHN\u1808 Doe <john@gmail.com>'));
  270. assertEquals(
  271. '"JOHN\u2E41 Doe" <john@gmail.com>',
  272. f('JOHN\u2E41 Doe <john@gmail.com>'));
  273. assertEquals(
  274. '"JOHN\u3001 Doe" <john@gmail.com>',
  275. f('JOHN\u3001 Doe <john@gmail.com>'));
  276. assertEquals(
  277. '"JOHN\uFF0C Doe" <john@gmail.com>',
  278. f('JOHN\uFF0C Doe <john@gmail.com>'));
  279. assertEquals(
  280. '"JOHN\u061B Doe" <john@gmail.com>',
  281. f('JOHN\u061B Doe <john@gmail.com>'));
  282. assertEquals(
  283. '"JOHN\u1364 Doe" <john@gmail.com>',
  284. f('JOHN\u1364 Doe <john@gmail.com>'));
  285. assertEquals(
  286. '"JOHN\uFF1B Doe" <john@gmail.com>',
  287. f('JOHN\uFF1B Doe <john@gmail.com>'));
  288. assertEquals(
  289. '"JOHN\uFF64 Doe" <john@gmail.com>',
  290. f('JOHN\uFF64 Doe <john@gmail.com>'));
  291. assertEquals(
  292. '"JOHN(Johnny) Doe" <john@gmail.com>',
  293. f('JOHN(Johnny) Doe <john@gmail.com>'));
  294. assertEquals(
  295. '"JOHN[Johnny] Doe" <john@gmail.com>',
  296. f('JOHN[Johnny] Doe <john@gmail.com>'));
  297. assertEquals(
  298. '"JOHN@work Doe" <john@gmail.com>', f('JOHN@work Doe <john@gmail.com>'));
  299. assertEquals(
  300. '"JOHN:theking Doe" <john@gmail.com>',
  301. f('JOHN:theking Doe <john@gmail.com>'));
  302. assertEquals(
  303. '"JOHN\\\\ Doe" <john@gmail.com>', f('JOHN\\ Doe <john@gmail.com>'));
  304. assertEquals(
  305. '"JOHN.com Doe" <john@gmail.com>', f('JOHN.com Doe <john@gmail.com>'));
  306. }