2017-08-19 10:57:54 +00:00
describe ( "Zotero.HTTP" , function ( ) {
var httpd ;
var port = 16213 ;
2018-09-11 05:23:15 +00:00
var baseURL = ` http://127.0.0.1: ${ port } / `
var testURL = baseURL + 'test.html' ;
var redirectLocation = baseURL + 'test2.html' ;
2017-08-19 10:57:54 +00:00
before ( function * ( ) {
Components . utils . import ( "resource://zotero-unit/httpd.js" ) ;
httpd = new HttpServer ( ) ;
httpd . start ( port ) ;
httpd . registerPathHandler (
'/test.html' ,
{
handle : function ( request , response ) {
response . setStatusLine ( null , 200 , "OK" ) ;
response . write ( "<html><body><p>Test</p><p>Test 2</p></body></html>" ) ;
}
}
) ;
2018-09-11 05:23:15 +00:00
httpd . registerPathHandler (
'/redirect' ,
{
handle : function ( request , response ) {
response . setHeader ( 'Location' , redirectLocation ) ;
response . setStatusLine ( null , 301 , "Moved Permanently" ) ;
response . write ( ` <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> \n <html><head> \n <title>301 Moved Permanently</title> \n </head><body> \n <h1>Moved Permanently</h1> \n <p>The document has moved <a href=" ${ redirectLocation } ">here</a>.</p> \n </body></html> ` ) ;
}
}
) ;
2017-08-19 10:57:54 +00:00
} ) ;
after ( function * ( ) {
var defer = new Zotero . Promise . defer ( ) ;
httpd . stop ( ( ) => defer . resolve ( ) ) ;
yield defer . promise ;
} ) ;
2018-09-11 05:23:15 +00:00
describe ( "#request()" , function ( ) {
it ( "should succeed with 3xx status if followRedirects is false" , async function ( ) {
var req = await Zotero . HTTP . request (
'GET' ,
baseURL + 'redirect' ,
{
followRedirects : false
}
) ;
assert . equal ( req . status , 301 ) ;
assert . equal ( req . getResponseHeader ( 'Location' ) , redirectLocation ) ;
} ) ;
} ) ;
2017-08-19 10:57:54 +00:00
describe ( "#processDocuments()" , function ( ) {
it ( "should provide a document object" , function * ( ) {
var called = false ;
yield Zotero . HTTP . processDocuments (
2018-09-11 05:23:15 +00:00
testURL ,
2017-08-19 10:57:54 +00:00
function ( doc ) {
2018-09-11 05:23:15 +00:00
assert . equal ( doc . location . href , testURL ) ;
2017-08-19 10:57:54 +00:00
assert . equal ( doc . querySelector ( 'p' ) . textContent , 'Test' ) ;
var p = doc . evaluate ( '//p' , doc , null , XPathResult . ANY _TYPE , null ) . iterateNext ( ) ;
assert . equal ( p . textContent , 'Test' ) ;
called = true ;
}
) ;
assert . isTrue ( called ) ;
} ) ;
} ) ;
describe ( "#loadDocuments()" , function ( ) {
var win ;
before ( function * ( ) {
// TEMP: createHiddenBrowser currently needs a parent window
win = yield loadBrowserWindow ( ) ;
} ) ;
after ( function * ( ) {
win . close ( ) ;
} ) ;
it ( "should provide a document object" , function * ( ) {
var called = false ;
yield new Zotero . Promise ( ( resolve ) => {
Zotero . HTTP . loadDocuments (
2018-09-11 05:23:15 +00:00
testURL ,
2017-08-19 10:57:54 +00:00
function ( doc ) {
2018-09-11 05:23:15 +00:00
assert . equal ( doc . location . href , testURL ) ;
2017-08-19 10:57:54 +00:00
assert . equal ( doc . querySelector ( 'p' ) . textContent , 'Test' ) ;
var p = doc . evaluate ( '//p' , doc , null , XPathResult . ANY _TYPE , null ) . iterateNext ( ) ;
assert . equal ( p . textContent , 'Test' ) ;
called = true ;
} ,
resolve
) ;
} ) ;
assert . isTrue ( called ) ;
} ) ;
} ) ;
} ) ;