简体   繁体   English

如何使用sinon.js伪造图像的响应

[英]How to use sinon.js to fake the response of image

I'm learning how to use sinon.js. 我正在学习如何使用sinon.js。 I can fake normal AJAX but I need to request a image and I don't get xhr.response (it is undefined). 我可以伪造正常的AJAX,但我需要请求一个图像,我不会得到xhr.response(它是未定义的)。 How can I use sinon.js to fake the response of an image? 我如何使用sinon.js伪造图像的响应?

var url = 'http://www.google.com.hk/logos/2013/antoni_gauds_161st_birthday-1539005.2-  hp.jpg';
server.respondWith("GET", url, [200, {}, '']);
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'arraybuffer';
xhr.addEventListener('load', function() {
//this.response is undefined
})
xhr.send(null);

How can I fake this image request? 我怎么能伪造这个图像请求?

I think an idea: 我想一个想法:

describe('...',function(){
    var xhr;
    before(function(){
       xhr = sinon.useFakeXMLHttpRequest();
       xhr.onCreate = function (x) {
      console.log(x)
       };
       //....
    }
    after(function(){
      xhr.restore();
    });
    it('....',function(){
       xhr.prototype.response = new ArrayBuffer(1024)
       // ... 
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM