123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- require("./chai.helper");
- var domHelper = require("./dom.helper");
- var HttpFake = require("./http-fake.helper");
- describe("jsonp", function () {
- var server = HttpFake.createServer();
- // we need to refer to this from the tests, but we allow
- // the server to randomly get a port before setting it
- var host;
- before(function (done) {
- domHelper();
- server.start(function () {
- host = "localhost:" + server.port;
- done.apply(null, arguments);
- });
- });
- afterEach(function () {
- server.clearFakes();
- });
- after(function (done) {
- server.stop(done);
- });
- it("should load an external JSONP script with callback in the URI", function (done) {
- var expected = { JSONP_LOADED: true };
- server.registerFake(
- // response config
- {
- data: function (req) {
- var callback = req.query["callback"];
- return callback + "(" + JSON.stringify(expected) + ");";
- }
- },
- // request matcher
- { path: '/1' }
- );
- // make chai's expect function available in the execution context
- // for the script
- window.expect = expect;
- // this is the function referenced in the querystring sent
- // to the server
- window.parseResponse = function (data) {
- window.expect(data).to.eql(expected);
- done();
- };
- $.jsonP({
- // NB querystring callback set to the function defined above
- url: "http://" + host + "/1?callback=window.parseResponse",
- error: function (xhr) {
- console.log(xhr);
- done(new Error("could not load jsonp script"));
- }
- });
- });
- it("should load an external JSONP script with anonymous callback", function (done) {
- var expected = { JSONP_ANON: true };
- server.registerFake(
- // response config
- {
- data: function (req) {
- var callback = req.query["callback"];
- return callback + "(" + JSON.stringify(expected) + ");";
- }
- },
- // request matcher
- {
- path: '/2',
- // ensure that the callback parameter is set to
- // a function name generated by appframework
- query: function (reqQuery) {
- return /jsonp_callback[\d]{1,}/.test(reqQuery["callback"]);
- }
- }
- );
- // make chai's expect function available in the execution context
- // for the script
- window.expect = expect;
- $.jsonP({
- url: "http://" + host + "/2?callback=?",
- success: function (data) {
- window.expect(data).to.eql(expected);
- done();
- },
- error: function (xhr) {
- console.log(xhr);
- done(new Error("could not load jsonp script"));
- }
- });
- });
- });
|