Skip to content

Commit

Permalink
fix 'Wrong behaviour for window.fetch request called without paramete…
Browse files Browse the repository at this point in the history
…rs' (close #939) (#946)

* fix 'Wrong behaviour for window.fetch request called without parameters' (close #939)

* add new line
  • Loading branch information
miherlosev authored and LavrovArtem committed Nov 17, 2016
1 parent 7c1cb53 commit e843838
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 63 deletions.
7 changes: 7 additions & 0 deletions src/client/sandbox/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@ export default class FetchSandbox extends SandboxBase {
args[1] = FetchSandbox._processRequestInit(init);
}

static _isValidRequestArgs (args) {
return typeof args[0] === 'string' || isFetchRequest(args[0]);
}

static _requestIsValid (args) {
if (!FetchSandbox._isValidRequestArgs(args))
return false;

var url = null;
var requestMode = null;

Expand Down
147 changes: 84 additions & 63 deletions test/client/fixtures/sandbox/fetch-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ if (window.fetch) {
},
body: JSON.stringify(data)
})
.then(function (response) {
return response.json();
})
.then(function (json) {
strictEqual(JSON.stringify(json), JSON.stringify(data));
start();
});
.then(function (response) {
return response.json();
})
.then(function (json) {
strictEqual(JSON.stringify(json), JSON.stringify(data));
start();
});
});

asyncTest('the internal 222 status code should be replaced with 0 on the client side', function () {
Expand Down Expand Up @@ -167,13 +167,13 @@ if (window.fetch) {
'Content-Type': 'application/json; charset=UTF-8'
}
})
.then(function (response) {
return response.json();
})
.then(function (headers) {
strictEqual('omit', headers[xhrHeaders.fetchRequestCredentials]);
start();
});
.then(function (response) {
return response.json();
})
.then(function (headers) {
strictEqual('omit', headers[xhrHeaders.fetchRequestCredentials]);
start();
});
});

asyncTest('headers is window.Headers', function () {
Expand All @@ -185,13 +185,13 @@ if (window.fetch) {
method: 'post',
headers: testHeaders
})
.then(function (response) {
return response.json();
})
.then(function (headers) {
strictEqual('omit', headers[xhrHeaders.fetchRequestCredentials]);
start();
});
.then(function (response) {
return response.json();
})
.then(function (headers) {
strictEqual('omit', headers[xhrHeaders.fetchRequestCredentials]);
start();
});
});

module('non-default values');
Expand All @@ -202,13 +202,13 @@ if (window.fetch) {
headers: { 'Content-Type': 'application/json; charset=UTF-8' },
credentials: 'same-origin'
})
.then(function (response) {
return response.json();
})
.then(function (headers) {
strictEqual('same-origin', headers[xhrHeaders.fetchRequestCredentials]);
start();
});
.then(function (response) {
return response.json();
})
.then(function (headers) {
strictEqual('same-origin', headers[xhrHeaders.fetchRequestCredentials]);
start();
});
});

asyncTest('headers is window.Headers', function () {
Expand All @@ -221,13 +221,13 @@ if (window.fetch) {
headers: testHeaders,
credentials: 'same-origin'
})
.then(function (response) {
return response.json();
})
.then(function (headers) {
strictEqual('same-origin', headers[xhrHeaders.fetchRequestCredentials]);
start();
});
.then(function (response) {
return response.json();
})
.then(function (headers) {
strictEqual('same-origin', headers[xhrHeaders.fetchRequestCredentials]);
start();
});
});
});

Expand All @@ -241,13 +241,13 @@ if (window.fetch) {
'Content-Type': 'application/json; charset=UTF-8'
}
})
.then(function (response) {
return response.json();
})
.then(function (headers) {
strictEqual('https://example.com', headers[xhrHeaders.origin]);
start();
});
.then(function (response) {
return response.json();
})
.then(function (headers) {
strictEqual('https://example.com', headers[xhrHeaders.origin]);
start();
});
});

asyncTest('headers is window.Headers', function () {
Expand All @@ -259,13 +259,13 @@ if (window.fetch) {
method: 'post',
headers: testHeaders
})
.then(function (response) {
return response.json();
})
.then(function (headers) {
strictEqual('https://example.com', headers[xhrHeaders.origin]);
start();
});
.then(function (response) {
return response.json();
})
.then(function (headers) {
strictEqual('https://example.com', headers[xhrHeaders.origin]);
start();
});
});

module('Request');
Expand All @@ -279,13 +279,13 @@ if (window.fetch) {
});

fetch(request)
.then(function (response) {
return response.json();
})
.then(function (headers) {
strictEqual('https://example.com', headers[xhrHeaders.origin]);
start();
});
.then(function (response) {
return response.json();
})
.then(function (headers) {
strictEqual('https://example.com', headers[xhrHeaders.origin]);
start();
});
});

asyncTest('headers is window.Headers', function () {
Expand All @@ -299,13 +299,13 @@ if (window.fetch) {
});

fetch(request)
.then(function (response) {
return response.json();
})
.then(function (headers) {
strictEqual('https://example.com', headers[xhrHeaders.origin]);
start();
});
.then(function (response) {
return response.json();
})
.then(function (headers) {
strictEqual('https://example.com', headers[xhrHeaders.origin]);
start();
});
});
});
});
Expand All @@ -327,6 +327,27 @@ if (window.fetch) {
start();
});
});

test('request promise should be rejected on invalid calling (GH-939)', function () {
var testCases = [
[],
[123],
[function () { }],
[null]
];

var createTestCasePromise = function (args) {
return fetch.apply(window, args)
.then(function () {
ok(false, 'wrong state of the request promise');
})
.catch(function () {
ok(true);
});
};

return Promise.all(testCases.map(createTestCasePromise));
});
});
}

Expand Down

0 comments on commit e843838

Please sign in to comment.