httpTest.js (1940B)
1 describe("Zotero.HTTP", function () { 2 var httpd; 3 var port = 16213; 4 5 before(function* () { 6 Components.utils.import("resource://zotero-unit/httpd.js"); 7 8 httpd = new HttpServer(); 9 httpd.start(port); 10 httpd.registerPathHandler( 11 '/test.html', 12 { 13 handle: function (request, response) { 14 response.setStatusLine(null, 200, "OK"); 15 response.write("<html><body><p>Test</p><p>Test 2</p></body></html>"); 16 } 17 } 18 ); 19 }); 20 21 after(function* () { 22 var defer = new Zotero.Promise.defer(); 23 httpd.stop(() => defer.resolve()); 24 yield defer.promise; 25 }); 26 27 describe("#processDocuments()", function () { 28 it("should provide a document object", function* () { 29 var called = false; 30 var url = `http://127.0.0.1:${port}/test.html`; 31 yield Zotero.HTTP.processDocuments( 32 url, 33 function (doc) { 34 assert.equal(doc.location.href, url); 35 assert.equal(doc.querySelector('p').textContent, 'Test'); 36 var p = doc.evaluate('//p', doc, null, XPathResult.ANY_TYPE, null).iterateNext(); 37 assert.equal(p.textContent, 'Test'); 38 called = true; 39 } 40 ); 41 assert.isTrue(called); 42 }); 43 }); 44 45 describe("#loadDocuments()", function () { 46 var win; 47 48 before(function* () { 49 // TEMP: createHiddenBrowser currently needs a parent window 50 win = yield loadBrowserWindow(); 51 }); 52 53 after(function* () { 54 win.close(); 55 }); 56 57 it("should provide a document object", function* () { 58 var called = false; 59 var url = `http://127.0.0.1:${port}/test.html`; 60 yield new Zotero.Promise((resolve) => { 61 Zotero.HTTP.loadDocuments( 62 url, 63 function (doc) { 64 assert.equal(doc.location.href, url); 65 assert.equal(doc.querySelector('p').textContent, 'Test'); 66 var p = doc.evaluate('//p', doc, null, XPathResult.ANY_TYPE, null).iterateNext(); 67 assert.equal(p.textContent, 'Test'); 68 called = true; 69 }, 70 resolve 71 ); 72 }); 73 assert.isTrue(called); 74 }); 75 }); 76 });