【问题标题】:PhantomJS hostfile change or host headerPhantomJS 主机文件更改或主机头
【发布时间】:2016-04-16 04:50:42
【问题描述】:

我想编写 PhantomJS 脚本,允许在网站的 localhost 版本和生产版本上进行测试(localhost 需要解析主机名,不能在 ip 地址上工作)。该脚本基本上只是比较两个版本:加载本地版本,渲染到图像,加载生产版本,渲染到图像并比较这两个版本。我现在正在做的是使用需要大量修改和保存主机文件的主机文件(不是很好的解决方案,因为它会影响其他人并且有时它会被缓存)

还有另一种方法是使用主机头,例如:

产品:http://example.com

本地:http://127.0.01/ 与主机:example.com

与hostfile结果相同,更安全。问题是只有主 html 加载了本地版本、资源、css,图像静态是使用 127.0.0.1 加载的,无法解决....

我的问题是:如何告诉 PhantomJS 对所有请求使用主机标头?

【问题讨论】:

    标签: javascript localhost phantomjs virtualhost ab-testing


    【解决方案1】:

    我找到了答案:D,通过拦截resourceRequest事件,我们可以改变资源的url或header。

    var targetHost=  'example.com'; 
    var targetIP = '127.0.0.1'; 
    var page = require('webpage').create(); 
    page.onResourceRequested = function(request, network){   //Intercept request here, change url,header.   
      var newUrl = request.url.replace(targetHost, targetIP);  
      console.log('Intercepted change url to ', newUrl, targetHost);  
      network.setHeader('Host', targetHost); 
      network.changeUrl(newUrl); 
    };
    page.open(url, function() {});

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-02
      • 2013-06-27
      • 1970-01-01
      • 2021-03-24
      • 2011-07-15
      • 2020-10-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多