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 'TypeError [ERR_UNESCAPED_CHARACTERS]: Request path contains unescaped characters' #42

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cakefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ task 'test', ->
ssl_cert ->
test_cases = glob.sync 'test/js/**/*_test.js'
test_cases.sort() # Consistent test case order.
run 'node_modules/.bin/mocha --colors --slow 200 --timeout 1000 ' +
run 'node_modules/.bin/mocha --colors --slow 200 --timeout 1000 --exit ' +
"--require test/js/helpers/setup.js #{test_cases.join(' ')}"

task 'webtest', ->
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
"engines": {
"node": ">= 6"
},
"dependencies": {},
"dependencies": {
"native-url": "^0.3.4"
},
"devDependencies": {
"async": ">=3.0.1",
"chai": ">=4.2.0",
Expand Down
4 changes: 3 additions & 1 deletion src/001-xml_http_request.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
http = require 'http'
https = require 'https'
os = require 'os'
url = require 'url'
url = require 'native-url'

# The ECMAScript HTTP API.
#
Expand Down Expand Up @@ -105,6 +105,8 @@ class XMLHttpRequest extends XMLHttpRequestEventTarget
throw new SecurityError "HTTP method #{method} is not allowed in XHR"

xhrUrl = @_parseUrl url
xhrUrl.path = encodeURI xhrUrl.path

async = true if async is undefined

switch @readyState
Expand Down
4 changes: 4 additions & 0 deletions test/src/helpers/xhr_server.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ class XhrServer
response.write json.body if json.body
response.end()

# Returns a given URL parameter. Used for URL encoding testing.
@app.all '/_/url/:arg', (request, response) =>
response.json request.params.arg

# Sends data in small chunks. Used for event testing.
@app.post '/_/drip', (request, response) ->
request.connection.setNoDelay()
Expand Down
22 changes: 22 additions & 0 deletions test/src/xhr_test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,28 @@ describe 'XMLHttpRequest', ->
done()
@xhr.send()

describe 'on an unencoded URL', ->
beforeEach ->
@xhr.open 'GET', 'http://localhost:8912/_/url/💻'

it 'encodes correctly', (done) ->
@xhr.onload = (event) =>
expect(@xhr.status).to.equal 200
expect(@xhr.responseText).to.equal '"💻"'
done()
@xhr.send()

describe 'on an encoded URL', ->
beforeEach ->
@xhr.open 'GET', 'http://localhost:8912/_/url/%F0%9F%92%BB'

it 'does not double-encode', (done) ->
@xhr.onload = (event) =>
expect(@xhr.status).to.equal 200
expect(@xhr.responseText).to.equal '"💻"'
done()
@xhr.send()

describe 'on a local gopher GET', ->
describe '#open + #send', ->
it 'throw a NetworkError', ->
Expand Down