emailaddress_test.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // Copyright 2010 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.EmailAddressTest');
  15. goog.setTestOnly('goog.format.EmailAddressTest');
  16. goog.require('goog.array');
  17. goog.require('goog.format.EmailAddress');
  18. goog.require('goog.testing.jsunit');
  19. function testparseList() {
  20. assertParsedList('', [], 'Failed to parse empty stringy');
  21. assertParsedList(',,', [], 'Failed to parse string with commas only');
  22. assertParsedList('<foo@gmail.com>', ['foo@gmail.com']);
  23. assertParsedList(
  24. '<foo@gmail.com>,', ['foo@gmail.com'],
  25. 'Failed to parse 1 address with trailing comma');
  26. assertParsedList(
  27. '<foo@gmail.com>, ', ['foo@gmail.com'],
  28. 'Failed to parse 1 address with trailing whitespace and comma');
  29. assertParsedList(
  30. ',<foo@gmail.com>', ['foo@gmail.com'],
  31. 'Failed to parse 1 address with leading comma');
  32. assertParsedList(
  33. ' ,<foo@gmail.com>', ['foo@gmail.com'],
  34. 'Failed to parse 1 address with leading whitespace and comma');
  35. assertParsedList(
  36. '<foo@gmail.com>, <bar@gmail.com>', ['foo@gmail.com', 'bar@gmail.com'],
  37. 'Failed to parse 2 email addresses');
  38. assertParsedList(
  39. '<foo@gmail.com>, <bar@gmail.com>,', ['foo@gmail.com', 'bar@gmail.com'],
  40. 'Failed to parse 2 email addresses and trailing comma');
  41. assertParsedList(
  42. '<foo@gmail.com>, <bar@gmail.com>, ', ['foo@gmail.com', 'bar@gmail.com'],
  43. 'Failed to parse 2 email addresses, trailing comma and whitespace');
  44. assertParsedList(
  45. 'John Doe <john@gmail.com>; Jane Doe <jane@gmail.com>, ' +
  46. '<jerry@gmail.com>',
  47. ['john@gmail.com', 'jane@gmail.com', 'jerry@gmail.com'],
  48. 'Failed to parse addresses with semicolon separator');
  49. }
  50. function testparseListOpenersAndClosers() {
  51. assertParsedList(
  52. 'aaa@gmail.com, "bbb@gmail.com", <ccc@gmail.com>, ' +
  53. '(ddd@gmail.com), [eee@gmail.com]',
  54. [
  55. 'aaa@gmail.com', '"bbb@gmail.com"', 'ccc@gmail.com', '(ddd@gmail.com)',
  56. '[eee@gmail.com]'
  57. ],
  58. 'Failed to handle all 5 opener/closer characters');
  59. }
  60. function testparseListIdn() {
  61. var idnaddr = 'mailtest@\u4F8B\u3048.\u30C6\u30B9\u30C8';
  62. assertParsedList(idnaddr, [idnaddr]);
  63. }
  64. function testparseListWithQuotedSpecialChars() {
  65. var res = assertParsedList(
  66. 'a\\"b\\"c <d@e.f>,"g\\"h\\"i\\\\" <j@k.l>', ['d@e.f', 'j@k.l']);
  67. assertEquals('Wrong name 0', 'a"b"c', res[0].getName());
  68. assertEquals('Wrong name 1', 'g"h"i\\', res[1].getName());
  69. }
  70. function testparseListWithCommaInLocalPart() {
  71. var res = assertParsedList(
  72. '"Doe, John" <doe.john@gmail.com>, <someone@gmail.com>',
  73. ['doe.john@gmail.com', 'someone@gmail.com']);
  74. assertEquals('Doe, John', res[0].getName());
  75. assertEquals('', res[1].getName());
  76. }
  77. function testparseListWithWhitespaceSeparatedEmails() {
  78. var res = assertParsedList(
  79. 'a@b.com <c@d.com> e@f.com "G H" <g@h.com> i@j.com',
  80. ['a@b.com', 'c@d.com', 'e@f.com', 'g@h.com', 'i@j.com']);
  81. assertEquals('G H', res[3].getName());
  82. }
  83. function testparseListSystemNewlines() {
  84. // These Windows newlines can be inserted in IE8, or copied-and-pasted from
  85. // bad data on a Mac, as seen in bug 11081852.
  86. assertParsedList(
  87. 'a@b.com\r\nc@d.com', ['a@b.com', 'c@d.com'],
  88. 'Failed to parse Windows newlines');
  89. assertParsedList(
  90. 'a@b.com\nc@d.com', ['a@b.com', 'c@d.com'],
  91. 'Failed to parse *nix newlines');
  92. assertParsedList(
  93. 'a@b.com\n\rc@d.com', ['a@b.com', 'c@d.com'],
  94. 'Failed to parse obsolete newlines');
  95. assertParsedList(
  96. 'a@b.com\rc@d.com', ['a@b.com', 'c@d.com'],
  97. 'Failed to parse pre-OS X Mac newlines');
  98. }
  99. function testToString() {
  100. var f = function(str) {
  101. return goog.format.EmailAddress.parse(str).toString();
  102. };
  103. // No modification.
  104. assertEquals('JOHN Doe <john@gmail.com>', f('JOHN Doe <john@gmail.com>'));
  105. // Extra spaces.
  106. assertEquals('JOHN Doe <john@gmail.com>', f(' JOHN Doe <john@gmail.com> '));
  107. // No name.
  108. assertEquals('john@gmail.com', f('<john@gmail.com>'));
  109. assertEquals('john@gmail.com', f('john@gmail.com'));
  110. // No address.
  111. assertEquals('JOHN Doe', f('JOHN Doe <>'));
  112. // Special chars in the name.
  113. assertEquals('"JOHN, Doe" <john@gmail.com>', f('JOHN, Doe <john@gmail.com>'));
  114. assertEquals(
  115. '"JOHN(Johnny) Doe" <john@gmail.com>',
  116. f('JOHN(Johnny) Doe <john@gmail.com>'));
  117. assertEquals(
  118. '"JOHN[Johnny] Doe" <john@gmail.com>',
  119. f('JOHN[Johnny] Doe <john@gmail.com>'));
  120. assertEquals(
  121. '"JOHN@work Doe" <john@gmail.com>', f('JOHN@work Doe <john@gmail.com>'));
  122. assertEquals(
  123. '"JOHN:theking Doe" <john@gmail.com>',
  124. f('JOHN:theking Doe <john@gmail.com>'));
  125. assertEquals(
  126. '"JOHN\\\\ Doe" <john@gmail.com>', f('JOHN\\ Doe <john@gmail.com>'));
  127. assertEquals(
  128. '"JOHN.com Doe" <john@gmail.com>', f('JOHN.com Doe <john@gmail.com>'));
  129. // Already quoted.
  130. assertEquals(
  131. '"JOHN, Doe" <john@gmail.com>', f('"JOHN, Doe" <john@gmail.com>'));
  132. // Needless quotes.
  133. assertEquals('JOHN Doe <john@gmail.com>', f('"JOHN Doe" <john@gmail.com>'));
  134. // Not quoted-string, but has double quotes.
  135. assertEquals(
  136. '"JOHN, Doe" <john@gmail.com>', f('JOHN, "Doe" <john@gmail.com>'));
  137. // No special characters other than quotes.
  138. assertEquals('JOHN Doe <john@gmail.com>', f('JOHN "Doe" <john@gmail.com>'));
  139. // Escaped quotes are also removed.
  140. assertEquals(
  141. '"JOHN, Doe" <john@gmail.com>', f('JOHN, \\"Doe\\" <john@gmail.com>'));
  142. }
  143. function doIsValidTest(testFunc, valid, invalid) {
  144. goog.array.forEach(valid, function(str) {
  145. assertTrue('"' + str + '" should be valid.', testFunc(str));
  146. });
  147. goog.array.forEach(invalid, function(str) {
  148. assertFalse('"' + str + '" should be invalid.', testFunc(str));
  149. });
  150. }
  151. function testIsValid() {
  152. var valid = [
  153. 'e@b.eu', '<a.b+foo@c.com>', 'eric <e@b.com>', '"e" <e@b.com>',
  154. 'a@FOO.MUSEUM', 'bla@b.co.ac.uk', 'bla@a.b.com', 'o\'hara@gm.com',
  155. 'plus+is+allowed@gmail.com', '!/#$%&\'*+-=~|`{}?^_@expample.com',
  156. 'confirm-bhk=modulo.org@yahoogroups.com'
  157. ];
  158. var invalid = [
  159. 'e', '', 'e @c.com', 'a@b', 'foo.com', 'foo@c..com', 'test@gma=il.com',
  160. 'aaa@gmail', 'has some spaces@gmail.com', 'has@three@at@signs.com',
  161. '@no-local-part.com', 'み.ん-あ@みんあ.みんあ', 'みんあ@test.com',
  162. 'test@test.みんあ', 'test@みんあ.com', 'fullwidthfullstop@sld' +
  163. '\uff0e' +
  164. 'tld',
  165. 'ideographicfullstop@sld' +
  166. '\u3002' +
  167. 'tld',
  168. 'halfwidthideographicfullstop@sld' +
  169. '\uff61' +
  170. 'tld'
  171. ];
  172. doIsValidTest(goog.format.EmailAddress.isValidAddress, valid, invalid);
  173. }
  174. function testIsValidLocalPart() {
  175. var valid = [
  176. 'e', 'a.b+foo', 'o\'hara', 'user+someone', '!/#$%&\'*+-=~|`{}?^_',
  177. 'confirm-bhk=modulo.org'
  178. ];
  179. var invalid = [
  180. 'A@b@c', 'a"b(c)d,e:f;g<h>i[j\\k]l', 'just"not"right',
  181. 'this is"not\\allowed', 'this\\ still\"not\\\\allowed', 'has some spaces'
  182. ];
  183. doIsValidTest(goog.format.EmailAddress.isValidLocalPartSpec, valid, invalid);
  184. }
  185. function testIsValidDomainPart() {
  186. var valid =
  187. ['example.com', 'dept.example.org', 'long.domain.with.lots.of.dots'];
  188. var invalid = [
  189. '', '@has.an.at.sign', '..has.leading.dots', 'gma=il.com',
  190. 'DoesNotHaveADot', 'sld' +
  191. '\uff0e' +
  192. 'tld',
  193. 'sld' +
  194. '\u3002' +
  195. 'tld',
  196. 'sld' +
  197. '\uff61' +
  198. 'tld'
  199. ];
  200. doIsValidTest(goog.format.EmailAddress.isValidDomainPartSpec, valid, invalid);
  201. }
  202. /**
  203. * Asserts that parsing the inputString produces a list of email addresses
  204. * containing the specified address strings, irrespective of their order.
  205. * @param {string} inputString A raw address list.
  206. * @param {Array<string>} expectedList The expected results.
  207. * @param {string=} opt_message An assertion message.
  208. * @return {string} the resulting email address objects.
  209. */
  210. function assertParsedList(inputString, expectedList, opt_message) {
  211. var message = opt_message || 'Should parse address correctly';
  212. var result = goog.format.EmailAddress.parseList(inputString);
  213. assertEquals(
  214. 'Should have correct # of addresses', expectedList.length, result.length);
  215. for (var i = 0; i < expectedList.length; ++i) {
  216. assertEquals(message, expectedList[i], result[i].getAddress());
  217. }
  218. return result;
  219. }