2012-08-01 21 views
5

Sto provando a eseguire una configurazione di test di cetriolo con Node.js che può testare qualsiasi sito Web utilizzando un iframe. Normalmente l'iframe è un no-go a causa dei limiti di sicurezza del cross script. Tuttavia, se fosse possibile (sono sicuro che lo sia. E mi fido di te per trovare una soluzione) per recuperare il sito web target per il test tramite l'URL richiesto quando viene richiesto un nome URL specifico, in modo che l'iframe verrebbe caricato con una copia del target di test. Fondamentalmente solo un server node.js standard che recupera pagine specifiche basate sul req.url simile ad un router di richiesta indirizzi.Instradamento di richieste HTTP tramite Node.js

Ecco il mio palese tentativo di fare esattamente questo. Recupero della pagina di prova tramite. l'url funziona. Ma sto avendo problemi a passare dal server http all'oggetto di connessione. C'è un modo per "alimentare" la connessione con la risposta del server http?

PS. Ho anche creato una soluzione con due server node.js. Il nodo 1 ha recuperato il target di test e lo ha mescolato con la pagina di test di cetriolo. Nodo 2 che ospita il test del cetriolo. Questa soluzione sta funzionando. Ma crea problemi sui siti web in cui si verificano conflitti di denominazione javascript. Ecco perché la soluzione iframe, che risolve questo problema con l'incapsulamento, è più allettante.

var http = require('http'); 
var connect = require('connect'); 
var port = process.env.PORT || 8788; 

var server = http.createServer(function(req, webres) 
{ 
    var url = req.url; 
    console.log(url); 

    if(url == '/myWebsiteToBeTestedWithCucumberJS') 
    { 
     // Load the web site to be tested "myWebsiteToBeTestedWithCucumberJS" 
      // And update the references 
      // Finaly write the page with the webres 
      // The page will appear to be hosted locally 

     console.log('Loading myWebsiteToBeTestedWithCucumberJS'); 
     webres.writeHead(200, {'content-type': 'text/html, level=1'}); 
     var options = 
     { 
        host: 'www.myWebsiteToBeTestedWithCucumberJS.com, 
        port: 80, 
        path: '/' 
     }; 

     var page = ''; 
     var req = http.get(options, function(res) 
     { 
      console.log("Got response: " + res.statusCode); 
      res.on('data', function(chunk) 
      { 
       page = page + chunk; 
      }); 
      res.on('end', function() 
      { 
        // Change relative paths to absolute (actual web location where images, javascript and stylesheets is placed) 
        page = page.replace(/ href="\/\//g  , ' href="/'); 
        page = page.replace(/ src="\//g   , ' src="www.myWebsiteToBeTestedWithCucumberJS.com'); 
        page = page.replace(/ data-src="\//g  , ' data-src="www.myWebsiteToBeTestedWithCucumberJS.com'); 
        page = page.replace(/ href="\//g   , ' href="www.myWebsiteToBeTestedWithCucumberJS.com'); 

        webres.write(page); 
        webres.end(''); 
      }); 
     }); 
    } 
    else 
    { 
     // Load any file from localhost:8788 
      // This is where the cucumber.js project files are hosted 
     var dirserver  = connect.createServer(); 
     var browserify = require('browserify'); 
     var cukeBundle = browserify({ 
      mount: '/cucumber.js', 
      require: ['cucumber-html', './lib/cucumber', 'gherkin/lib/gherkin/lexer/en'], 
      ignore: ['./cucumber/cli', 'connect'] 
     }); 
     dirserver.use(connect.static(__dirname)); 
     dirserver.use(cukeBundle); 
     dirserver.listen(port); 
    } 
}).on('error', function(e) 
{ 
     console.log("Got error: " + e.message); 
}); 
server.listen(port); 
console.log('Accepting connections on port ' + port + '...'); 

risposta

3

Beh, non è stato poi così difficile.
Essendo nuovo a node.js ho dovuto rendermi conto delle possibilità di utilizzare più listener.
La lettura delle funzioni di nodejitsu mi ha aiutato a risolvere il problema.

Di seguito viene caricato www.myWebsiteToBeTestedWithCucumberJS.com quando specificando l'indirizzo nel seguente modo: http://localhost:9788/myWebsiteToBeTestedWithCucumberJS dove tutte le altre richieste sono gestiti come cucumber.js richieste di siti web.
Spero che questo abbia senso per altri nuovi nodi node.js.

var http = require('http'); 

var connect = require('connect'); 
var port = process.env.PORT || 9788; 

var server = http.createServer(function(req, webres) 
{ 
    var url = req.url; 
    console.log(url); 
    if(url == '/myWebsiteToBeTestedWithCucumberJS') 
    { 
     loadMyWebsiteToBeTestedWithCucumberJS(req, webres); 
    } 
    else 
    { 
     loadLocal(req, webres, url); 
    } 
}).on('error', function(e) 
{ 
     console.log("Got error: " + e.message); 
}); 
server.listen(port); 
console.log('Accepting connections on port ' + port + '...'); 

function loadMyWebsiteToBeTestedWithCucumberJS(req, webres) 
{ 
    console.log('Loading myWebsiteToBeTestedWithCucumberJS'); 
    webres.writeHead(200, {'content-type': 'text/html, level=1'}); 
    var options = 
    { 
       host: 'www.myWebsiteToBeTestedWithCucumberJS.com', 
       port: 80, 
       path: '/' 
    }; 

    var page = ''; 
    var req = http.get(options, function(res) 
    { 
     console.log("Got response: " + res.statusCode); 
     res.on('data', function(chunk) 
     { 
      page = page + chunk; 
     }); 
     res.on('end', function() 
     { 
       page = page.replace(/ href="\/\//g  , ' href="/'); 
       page = page.replace(/ src="\//g   , ' src="http://www.myWebsiteToBeTestedWithCucumberJS.com/'); 
       page = page.replace(/ data-src="\//g  , ' data-src="http://www.myWebsiteToBeTestedWithCucumberJS.com/'); 
       page = page.replace(/ href="\//g   , ' href="http://www.myWebsiteToBeTestedWithCucumberJS.com/'); 

       webres.write(page); 
       webres.end(''); 
     }); 
    }); 

} 

function loadLocal(req, webres, path) 
{ 
    console.log('Loading localhost'); 
    webres.writeHead(200, {'content-type': 'text/html, level=1'}); 
    var options = 
    { 
       host: 'localhost', 
       port: 9787, 
       path: path 
    }; 

    var page = ''; 
    var req = http.get(options, function(res) 
    { 
     console.log("Got response: " + res.statusCode); 
     res.on('data', function(chunk) 
     { 
      page = page + chunk; 
     }); 
     res.on('end', function() 
     { 
       webres.write(page); 
       webres.end(''); 
     }); 
    }); 
} 


// Cucumber site listening on port 9787 
var dirserver  = connect.createServer(); 
var browserify = require('browserify'); 
var cukeBundle = browserify(
{ 
    mount: '/cucumber.js', 
    require: ['cucumber-html', './lib/cucumber', 'gherkin/lib/gherkin/lexer/en'], 
    ignore: ['./cucumber/cli', 'connect'] 
}); 
dirserver.use(connect.static(__dirname)); 
dirserver.use(cukeBundle); 
dirserver.listen(9787); 
+0

Come nota a margine, posso consigliare di dare un'occhiata a Cukestall (https://github.com/jbpros/cukestall). È il potenziale "runner iframe" ufficiale per Cucumber.js. È finalizzato a testare un'applicazione * local * Node.js. Tuttavia, dovrebbe essere abbastanza facile da eseguire e caricare la suite di funzioni su un'app * remota *. – jbpros

+0

hanno un upvote per il nome della funzione 'loadMyWebsiteToBeTestedWithCucumberJS' –

Problemi correlati