Support URLSearchParams POST body

https://fetch.spec.whatwg.org/#body-mixin
上级 57b7f98f
......@@ -19,6 +19,7 @@
"browser": true,
"worker": true,
"globals": {
"JSON": false
"JSON": false,
"URLSearchParams": false
}
}
......@@ -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 {
......@@ -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)) {
......@@ -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')
}
}
}
......
......@@ -14,11 +14,12 @@
"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"
]
......
......@@ -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>
......
......@@ -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>
......
var support = {
searchParams: 'URLSearchParams' in self,
blob: 'FileReader' in self && 'Blob' in self && (function() {
try {
new Blob()
......@@ -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/')
......@@ -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',
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册