jameslnewell / xhr-mock

Utility for mocking XMLHttpRequest.
196 stars 48 forks source link

Test URL with Query string #101

Open ssk910 opened 4 years ago

ssk910 commented 4 years ago

Occurred an Error

HTTP method : GET URL : http://www.example.com?id=user1234&pw=!Q%40W%23E%24R&name=sghwang Error msg : xhr-mock: No handler returned a response for the request.

(node:30976) UnhandledPromiseRejectionWarning: AssertionError: expected false to be true
    at Assertion.fail (C:\private\dev\java\enliple\rebuild\agent\mobon.ad.tracker.agent\node_modules\should\cjs
\should.js:275:17)
    at Assertion.value (C:\private\dev\java\enliple\rebuild\agent\mobon.ad.tracker.agent\node_modules\should\cj
s\should.js:356:19)
    at C:\private\dev\java\enliple\rebuild\agent\mobon.ad.tracker.agent\test\script\bdd\lib\ajax\AJAXer.spec.ts
:50:40
    at MockXMLHttpRequest.callbackListener [as onreadystatechange] (C:\private\dev\java\enliple\rebuild\agent\m
obon.ad.tracker.agent\script\lib\ajax\AJAXerCore.ts:211:11)
    at MockXMLHttpRequest.MockEventTarget.dispatchEvent (C:\private\dev\java\enliple\rebuild\agent\mobon.ad.tra
cker.agent\node_modules\xhr-mock\lib\MockEventTarget.js:43:20)
    at MockXMLHttpRequest.reportError (C:\private\dev\java\enliple\rebuild\agent\mobon.ad.tracker.agent\node_mo
dules\xhr-mock\lib\MockXMLHttpRequest.js:418:14)
    at MockXMLHttpRequest.handleError (C:\private\dev\java\enliple\rebuild\agent\mobon.ad.tracker.agent\node_mo
dules\xhr-mock\lib\MockXMLHttpRequest.js:537:14)
    at MockXMLHttpRequest.<anonymous> (C:\private\dev\java\enliple\rebuild\agent\mobon.ad.tracker.agent\node_mo
dules\xhr-mock\lib\MockXMLHttpRequest.js:388:30)
    at step (C:\private\dev\java\enliple\rebuild\agent\mobon.ad.tracker.agent\node_modules\xhr-mock\lib\MockXML
HttpRequest.js:50:23)
    at Object.throw (C:\private\dev\java\enliple\rebuild\agent\mobon.ad.tracker.agent\node_modules\xhr-mock\lib
\MockXMLHttpRequest.js:31:53)
    at rejected (C:\private\dev\java\enliple\rebuild\agent\mobon.ad.tracker.agent\node_modules\xhr-mock\lib\Moc
kXMLHttpRequest.js:23:65)

Passed without any errors

If I test with an URL which has a slash before question mark, It passed without any errors.

HTTP method : GET URL : http://www.example.com/?id=user1234&pw=!Q%40W%23E%24R&name=sghwang


Conclusion

I searched on StackOverflow about this error. It says xhr-mock compares the URLs by doing req.url().toString() === url I think it's a bug.

gflohr commented 4 years ago

IMHO, this bug is just one aspect of a more general problem. The real XHR implementation normalizes all URL strings before the request is sent. Converting an empty path into a single slash is just one such normalization.

Likewise, default ports are stripped off, and the hostname part is normalized. For example, http://0177.0.0.1, http://0x7f.0.0.1, or http://0000177.0x0.0000.0x1 are different funny ways of writing http://127.0.0.1/, but a browser will treat them all in exactly the same manner. You can easily try it out by clicking the links.

So, why not just normalize all URLs for incoming requests with new URL(url).href? That would fix all of these problems. But, of course, it would introduce a small incompatibility with previous versions.

See http://www.guido-flohr.net/the-gory-details-of-url-validation/ for more such details.