2
0

http-file-upload.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. (function (root, factory) {
  2. define([
  3. "jasmine",
  4. "jquery",
  5. "converse-core",
  6. "mock",
  7. "test-utils"], factory);
  8. } (this, function (jasmine, $, converse, mock, test_utils) {
  9. "use strict";
  10. var Strophe = converse.env.Strophe;
  11. var $iq = converse.env.$iq;
  12. var _ = converse.env._;
  13. describe("XEP-0363: HTTP File Upload", function () {
  14. describe("Discovering support", function () {
  15. it("is done automatically", mock.initConverseWithAsync(function (done, _converse) {
  16. var IQ_stanzas = _converse.connection.IQ_stanzas;
  17. var IQ_ids = _converse.connection.IQ_ids;
  18. test_utils.waitUntil(function () {
  19. return _.filter(IQ_stanzas, function (iq) {
  20. return iq.nodeTree.querySelector('query[xmlns="http://jabber.org/protocol/disco#info"]');
  21. }).length > 0;
  22. }, 300).then(function () {
  23. /* <iq type='result'
  24. * from='plays.shakespeare.lit'
  25. * to='romeo@montague.net/orchard'
  26. * id='info1'>
  27. * <query xmlns='http://jabber.org/protocol/disco#info'>
  28. * <identity
  29. * category='server'
  30. * type='im'/>
  31. * <feature var='http://jabber.org/protocol/disco#info'/>
  32. * <feature var='http://jabber.org/protocol/disco#items'/>
  33. * </query>
  34. * </iq>
  35. */
  36. var info_IQ_id = IQ_ids[0];
  37. var stanza = $iq({
  38. 'type': 'result',
  39. 'from': 'localhost',
  40. 'to': 'dummy@localhost/resource',
  41. 'id': info_IQ_id
  42. }).c('query', {'xmlns': 'http://jabber.org/protocol/disco#info'})
  43. .c('identity', {
  44. 'category': 'server',
  45. 'type': 'im'}).up()
  46. .c('feature', {
  47. 'var': 'http://jabber.org/protocol/disco#info'}).up()
  48. .c('feature', {
  49. 'var': 'http://jabber.org/protocol/disco#items'});
  50. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  51. var entities = _converse.disco_entities;
  52. expect(entities.length).toBe(2); // We have an extra entity, which is the user's JID
  53. expect(entities.get(_converse.domain).features.length).toBe(2);
  54. expect(entities.get(_converse.domain).identities.length).toBe(1);
  55. return test_utils.waitUntil(function () {
  56. // Converse.js sees that the entity has a disco#items feature,
  57. // so it will make a query for it.
  58. return _.filter(IQ_stanzas, function (iq) {
  59. return iq.nodeTree.querySelector('query[xmlns="http://jabber.org/protocol/disco#items"]');
  60. }).length > 0;
  61. }, 300);
  62. }).then(function () {
  63. /* <iq from='montague.tld'
  64. * id='step_01'
  65. * to='romeo@montague.tld/garden'
  66. * type='result'>
  67. * <query xmlns='http://jabber.org/protocol/disco#items'>
  68. * <item jid='upload.montague.tld' name='HTTP File Upload' />
  69. * <item jid='conference.montague.tld' name='Chatroom Service' />
  70. * </query>
  71. * </iq>
  72. */
  73. var items_IQ_id = IQ_ids[IQ_ids.length-1];
  74. var stanza = $iq({
  75. 'type': 'result',
  76. 'from': 'localhost',
  77. 'to': 'dummy@localhost/resource',
  78. 'id': items_IQ_id
  79. }).c('query', {'xmlns': 'http://jabber.org/protocol/disco#items'})
  80. .c('item', {
  81. 'jid': 'upload.localhost',
  82. 'name': 'HTTP File Upload'}).up()
  83. .c('item', {
  84. 'jid': 'conference.localhost',
  85. 'name': 'Chatrooms Service'});
  86. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  87. var entities = _converse.disco_entities;
  88. expect(entities.length).toBe(4); // We have an extra entity, which is the user's JID
  89. return test_utils.waitUntil(function () {
  90. // Converse.js sees that the entity has a disco#items feature,
  91. // so it will make a query for it.
  92. return _.filter(IQ_stanzas, function (iq) {
  93. return iq.nodeTree.querySelector('iq[to="upload.localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]');
  94. }).length > 0;
  95. }, 300);
  96. }).then(function () {
  97. var stanza = _.filter(IQ_stanzas, function (iq) {
  98. return iq.nodeTree.querySelector('iq[to="upload.localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]');
  99. })[0];
  100. var IQ_id = IQ_ids[IQ_stanzas.indexOf(stanza)];
  101. expect(stanza.toLocaleString()).toBe(
  102. "<iq from='dummy@localhost/resource' to='upload.localhost' type='get' xmlns='jabber:client' id='"+IQ_id+"'>"+
  103. "<query xmlns='http://jabber.org/protocol/disco#info'/>"+
  104. "</iq>");
  105. // Upload service responds and reports a maximum file size of 5MiB
  106. /* <iq from='upload.montague.tld'
  107. * id='step_02'
  108. * to='romeo@montague.tld/garden'
  109. * type='result'>
  110. * <query xmlns='http://jabber.org/protocol/disco#info'>
  111. * <identity category='store'
  112. * type='file'
  113. * name='HTTP File Upload' />
  114. * <feature var='urn:xmpp:http:upload:0' />
  115. * <x type='result' xmlns='jabber:x:data'>
  116. * <field var='FORM_TYPE' type='hidden'>
  117. * <value>urn:xmpp:http:upload:0</value>
  118. * </field>
  119. * <field var='max-file-size'>
  120. * <value>5242880</value>
  121. * </field>
  122. * </x>
  123. * </query>
  124. * </iq>
  125. */
  126. stanza = $iq({'type': 'result', 'to': 'dummy@localhost/resource', 'id': IQ_id, 'from': 'upload.localhost'})
  127. .c('query', {'xmlns': 'http://jabber.org/protocol/disco#info'})
  128. .c('identity', {'category':'store', 'type':'file', 'name':'HTTP File Upload'}).up()
  129. .c('feature', {'var':'urn:xmpp:http:upload:0'}).up()
  130. .c('x', {'type':'result', 'xmlns':'jabber:x:data'})
  131. .c('field', {'var':'FORM_TYPE', 'type':'hidden'})
  132. .c('value').t('urn:xmpp:http:upload:0').up().up()
  133. .c('field', {'var':'max-file-size'})
  134. .c('value').t('5242880');
  135. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  136. var entities = _converse.disco_entities;
  137. expect(entities.get('upload.localhost').identities.where({'category': 'store'}).length).toBe(1);
  138. done();
  139. })
  140. }));
  141. });
  142. });
  143. }));