Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix fetch API rasies TypeError when argument is URL object (close #1613) #1623

Merged
merged 20 commits into from
Jun 6, 2018
Merged

Conversation

Farfurix
Copy link
Contributor

@Farfurix Farfurix commented May 18, 2018

fetch supports the following browsers: Chrome, Firefox, Edge, Safari.

References

#1613
#939

Changes

  1. Change arguments processing logic in fetch() src/client/sandbox/fetch.js:
    If the input argument(args[0]) is not a string and is not a Request object,
    we are trying to convert the input argument to string primitive value using String().
    This approach helps us simulate the browsers behavior by throwing the 'TypeError' exception
    if the input object can not be converted to a primitive value.
  2. Fix "request promise should be rejected on invalid calling (Wrong behaviour for window.fetch request called without parameters #939)" test.
  3. Fix XMLHttpRequest: convert url argument to string before getProxyUrl. Fix test/client/before-test.js checkNativeFunctionCalling.
  4. Fix locationWrapper (href setter, assgin, replace): convert url argument to string.
  5. Fix location property case. Add routes in test/client/config-qunit-server-app.js for iframe loading in IE 11 (location property test)
  6. Fix "xhr.response.url" test: exclude Internet Explorer 11 with 0 assertions.
  7. Remove checkNativeFunctionArgs('open', 'xhrOpen', xhr) test case from windows-test.js because of the url argument string conversion.

Fix "request promise should be rejected on invalid calling (GH-939)" test

Browsers process all of these arguments in fetch without Promise rejection:

var testCases = [
    123,
    function () {
    },
    null
];

Simple example:

var testFetch = function (firstArg) {
    fetch(firstArg)
        .then(function (res) {
            console.log(res.url);
        })
        .catch(function (err) {
            console.log(err);
        });
}
> testFetch(123) 
http://127.0.0.1:8080/123

> testFetch(null) 
http://127.0.0.1:8080/null

> testFetch(function () {} ) 
http://127.0.0.1:8080/function%20()%20%7B%20%20%20%20%7D

Safari processed fetch() without Promise rejection

> fetch()
http://127.0.0.1:8080/undefined

Browsers process inaccessible null and undefined arguments without Promise rejection

> testFetch(null) 
http://127.0.0.1:8080/null

> testFetch(undefined) 
http://127.0.0.1:8080/undefined

Object in the fetch first argument

URL:

> testFetch(new URL('http://127.0.0.1:8080/index2.html')) 
http://127.0.0.1:8080/index2.html

Object without toString method:

var firstArg = {
    url: 'http://127.0.0.1:8080/index2.html'
};
> testFetch(firstArg)
http://127.0.0.1:8080/[object%20Object]

Object withtoString method:

var firstArg = {
    url:      'http://127.0.0.1:8080/index2.html',
    toString: function () {
        return this.url;
    }
};
> testFetch(firstArg)
http://127.0.0.1:8080/index2.html

@Farfurix Farfurix requested a review from LavrovArtem May 18, 2018 07:06
@testcafe-build-bot
Copy link
Collaborator

❌ Tests for the commit 984020d have failed. See details:

@@ -53,7 +55,24 @@ export default class FetchSandbox extends SandboxBase {
}

static _isValidRequestArgs (args) {
return typeof args[0] === 'string' || isFetchRequest(args[0]);
if (typeof args[0] === 'string' || isFetchRequest(args[0]))
Copy link
Contributor

@miherlosev miherlosev May 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bad design. _isValidRequestArgs should not modify the arguments.

Rewrite as the following:

return typeof args[0] === 'string' || isFetchRequest(args[0]) || objectWithToStringMethod;

Move argument processing logic for null, undefined and custom object with toString method to FetchSandbox._processArguments(args).

return this.url;
}
},
expectedUrl: 'https://example.com/some-path'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add test for object without toString method

@testcafe-build-bot
Copy link
Collaborator

❌ Tests for the commit 4e699bb have failed. See details:

@testcafe-build-bot
Copy link
Collaborator

❌ Tests for the commit 931baa9 have failed. See details:

@testcafe-build-bot
Copy link
Collaborator

✅ Tests for the commit 1029adf have passed. See details:

});
};

var promises = [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var cases = [
    checkArg({
        toString: function () {
            return {};
        }
    })
];

@testcafe-build-bot
Copy link
Collaborator

❌ Tests for the commit ccb2aeb have failed. See details:

@testcafe-build-bot
Copy link
Collaborator

❌ Tests for the commit f9bed6d have failed. See details:

@Farfurix
Copy link
Contributor Author

@testcafe-build-bot \retest

@testcafe-build-bot
Copy link
Collaborator

❌ Tests for the commit f9bed6d have failed. See details:

@testcafe-build-bot
Copy link
Collaborator

❌ Tests for the commit 8521d2b have failed. See details:

@testcafe-build-bot
Copy link
Collaborator

✅ Tests for the commit c7a2e68 have passed. See details:

@testcafe-build-bot
Copy link
Collaborator

✅ Tests for the commit d02d5b9 have passed. See details:

@testcafe-build-bot
Copy link
Collaborator

❌ Tests for the commit 9c3493f have failed. See details:

@Farfurix
Copy link
Contributor Author

@testcafe-build-bot \retest

@testcafe-build-bot
Copy link
Collaborator

❌ Tests for the commit 9c3493f have failed. See details:

@testcafe-build-bot
Copy link
Collaborator

✅ Tests for the commit 864bd6d have passed. See details:

@testcafe-build-bot
Copy link
Collaborator

❌ Tests for the commit 2d87ed8 have failed. See details:

@testcafe-build-bot
Copy link
Collaborator

❌ Tests for the commit 7f3566a have failed. See details:

@Farfurix
Copy link
Contributor Author

@testcafe-build-bot \retest

@testcafe-build-bot
Copy link
Collaborator

❌ Tests for the commit 7f3566a have failed. See details:

@testcafe-build-bot
Copy link
Collaborator

❌ Tests for the commit aa0d6dd have failed. See details:

@Farfurix Farfurix changed the title [WIP] fix fetch API rasies TypeError when argument is URL object (close #1613) fix fetch API rasies TypeError when argument is URL object (close #1613) May 30, 2018
@testcafe-build-bot
Copy link
Collaborator

❌ Tests for the commit 33ee438 have failed. See details:

@Farfurix
Copy link
Contributor Author

@testcafe-build-bot \retest

@testcafe-build-bot
Copy link
Collaborator

✅ Tests for the commit 33ee438 have passed. See details:

@@ -61,6 +61,10 @@ export default class LocationWrapper extends EventEmitter {
return ensureTrailingSlash(href, locationUrl);
};
const getProxiedHref = href => {
// NOTE: we must convert url-object to 'string' (GH-1613)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this comment

if (inputIsString) {
args[0] = getProxyUrl(input);
if (!inputIsFetchRequest) {
// NOTE: we must convert url-object to 'string' (GH-1613)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this comment

const url = arguments[1];
const urlIsString = typeof input === 'string';

// NOTE: we must convert url-object to 'string' (GH-1613)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this comment

@testcafe-build-bot
Copy link
Collaborator

❌ Tests for the commit 7975445 have failed. See details:

@testcafe-build-bot
Copy link
Collaborator

✅ Tests for the commit 7f37db8 have passed. See details.

return nativeMethods.fetch.apply(this);

if (!FetchSandbox._requestIsValid(args))
return sandbox.window.Promise.reject(new TypeError());
// NOTE: If the input argument(args[0]) is not a `string` and is not a `Request` object,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this comment


FetchSandbox._processArguments(args);
if (!FetchSandbox._sameOriginCheck(args))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move FetchSandbox._sameOriginCheck to the FetchSandbox._processArguments

@@ -192,6 +192,204 @@ test('special pages (GH-339)', function () {
destLocation.forceLocation(storedForcedLocation);
});

test('should process some url types in locationWrapper (href, replace, assign) (GH-1613)', function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test('different url types for locationWrapper methods (href, replace, assign) (GH-1613)'

});
});

test('should throwing an error on invalid url-object in locationWrapper (href, replace, assign) (GH-1613)', function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test('throwing errors on calling locationWrapper methods (href, replace, assign) with invalid arguments

}
});

test('should process some url types in the "location" property (GH-1613)', function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test('different url types for "location" property (GH-1613)')

});
};

var checkIframesLocation = function (nativeIframeUrl, processedIframeUrl) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var checkLocationAssignment = ...

@@ -62,6 +62,52 @@ if (window.fetch) {
});
});

test('should process some argument types (GH-1613)', function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

different url types for 'fetch' (GH-1613)

@@ -76,6 +78,66 @@ test('toString, instanceof, constructor and static properties', function () {
strictEqual(XMLHttpRequest.DONE, nativeMethods.XMLHttpRequest.DONE);
});

test('overridden "open" function should process some url argument types before the native "XMLHttpRequest.open" method calling (GH-1613)', function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test('different url types for xhr.open method (GH-1613)')

nativeMethods.xhrOpen = storedNativeXhrOpen;
});

test('should throwing an error on invalid calling "open" (GH-1613)', function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test('should throwing an error on invalid calling "open" method (GH-1613)'

start();
}
// NOTE: IE11 doesn't support 'XMLHttpRequest.responseURL'
if (browserUtils.isIE11) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this condition need?

@AndreyBelym AndreyBelym closed this Jun 6, 2018
@AndreyBelym AndreyBelym reopened this Jun 6, 2018
@AndreyBelym AndreyBelym closed this Jun 6, 2018
@AndreyBelym AndreyBelym reopened this Jun 6, 2018
@testcafe-build-bot
Copy link
Collaborator

✅ Tests for the commit b35f554 have passed. See details.

@miherlosev miherlosev merged commit 0bb81c9 into DevExpress:master Jun 6, 2018
@Farfurix Farfurix deleted the i1613 branch June 9, 2018 09:43
AndreyBelym pushed a commit to AndreyBelym/testcafe-hammerhead that referenced this pull request Feb 28, 2019
…evExpress#1613) (DevExpress#1623)

* fix `fetch API rasies TypeError when argument is URL object`

* requested changes, fix Safari case

* refactoring

* requested changes

* requested changes

* requested changes: fix tests

* add URL test case

* xhr, location.href, location.assign, location.replace cases

* requested changes: fix xhr.open test, remove unnecessary testcase in window-test.js

* requested changes: refactor xhr.open test

* fix location property case

* try to fix tests

* try to fix tests

* fix location property test

* try to fix location property test

* try to fix location property test

* fix double quotation in location property test

* requested changes

* requested changes

* restore xhr.responseURL test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants