Skip to content

Commit

Permalink
Merge pull request JakeChampion#304 from github/url-search-params
Browse files Browse the repository at this point in the history
Add support for URLSearchParams POST body
  • Loading branch information
dgraham committed Mar 30, 2016
2 parents 57b7f98 + 2ead77d commit cff0ebb
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"browser": true,
"worker": true,
"globals": {
"JSON": false
"JSON": false,
"URLSearchParams": false
}
}
5 changes: 5 additions & 0 deletions fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
}

var support = {
searchParams: 'URLSearchParams' in self,
iterable: 'Symbol' in self && 'iterator' in Symbol,
blob: 'FileReader' in self && 'Blob' in self && (function() {
try {
Expand Down Expand Up @@ -193,6 +194,8 @@
this._bodyBlob = body
} else if (support.formData && FormData.prototype.isPrototypeOf(body)) {
this._bodyFormData = body
} else if (support.searchParams && URLSearchParams.prototype.isPrototypeOf(body)) {
this._bodyText = body.toString()
} else if (!body) {
this._bodyText = ''
} else if (support.arrayBuffer && ArrayBuffer.prototype.isPrototypeOf(body)) {
Expand All @@ -207,6 +210,8 @@
this.headers.set('content-type', 'text/plain;charset=UTF-8')
} else if (this._bodyBlob && this._bodyBlob.type) {
this.headers.set('content-type', this._bodyBlob.type)
} else if (support.searchParams && URLSearchParams.prototype.isPrototypeOf(body)) {
this.headers.set('content-type', 'application/x-www-form-urlencoded;charset=UTF-8')
}
}
}
Expand Down
14 changes: 5 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,17 @@
"version": "0.11.0",
"main": "fetch.js",
"repository": "github/fetch",
"licenses": [
{
"type": "MIT",
"url": "https://github.com/github/fetch/blob/master/LICENSE"
}
],
"license": "MIT",
"devDependencies": {
"bower": "1.3.8",
"chai": "1.10.0",
"jshint": "2.8.0",
"mocha-phantomjs": "3.5.2",
"mocha": "2.1.0",
"phantomjs": "1.9.19"
"mocha-phantomjs": "3.5.2",
"phantomjs": "1.9.19",
"url-search-params": "0.5.0"
},
"files" : [
"files": [
"LICENSE",
"fetch.js"
]
Expand Down
1 change: 1 addition & 0 deletions test/test-worker.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</head>
<body>
<div id="mocha"></div>
<script src="/node_modules/url-search-params/build/url-search-params.js"></script>
<script src="/node_modules/mocha/mocha.js"></script>

<script>
Expand Down
1 change: 1 addition & 0 deletions test/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</head>
<body>
<div id="mocha"></div>
<script src="/node_modules/url-search-params/build/url-search-params.js"></script>
<script src="/node_modules/chai/chai.js"></script>
<script src="/node_modules/mocha/mocha.js"></script>
<script>
Expand Down
32 changes: 32 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var support = {
searchParams: 'URLSearchParams' in self,
blob: 'FileReader' in self && 'Blob' in self && (function() {
try {
new Blob()
Expand Down Expand Up @@ -311,6 +312,18 @@ suite('Request', function() {
})
})

featureDependent(test, support.searchParams, 'sends URLSearchParams body', function() {
return fetch('/request', {
method: 'post',
body: new URLSearchParams('a=1&b=2')
}).then(function(response) {
return response.json()
}).then(function(request) {
assert.equal(request.method, 'POST')
assert.equal(request.data, 'a=1&b=2')
})
})

test('construct with url', function() {
var request = new Request('https://fetch.spec.whatwg.org/')
assert.equal(request.url, 'https://fetch.spec.whatwg.org/')
Expand Down Expand Up @@ -442,6 +455,25 @@ suite('Request', function() {
assert.equal(req.headers.get('content-type'), 'image/png')
})

featureDependent(test, support.searchParams, 'construct with URLSearchParams body sets Content-Type header', function() {
var req = new Request('https://fetch.spec.whatwg.org/', {
method: 'post',
body: new URLSearchParams('a=1&b=2')
})

assert.equal(req.headers.get('content-type'), 'application/x-www-form-urlencoded;charset=UTF-8')
})

featureDependent(test, support.searchParams, 'construct with URLSearchParams body and explicit Content-Type header', function() {
var req = new Request('https://fetch.spec.whatwg.org/', {
method: 'post',
headers: { 'Content-Type': 'image/png' },
body: new URLSearchParams('a=1&b=2')
})

assert.equal(req.headers.get('content-type'), 'image/png')
})

test('clone request', function() {
var req = new Request('https://fetch.spec.whatwg.org/', {
method: 'post',
Expand Down

0 comments on commit cff0ebb

Please sign in to comment.