зеркало из https://github.com/github/fetch.git
Parse form encoded response body.
This commit is contained in:
Родитель
2c1534ac97
Коммит
60271cef8a
15
fetch.js
15
fetch.js
|
@ -74,7 +74,7 @@
|
|||
}
|
||||
|
||||
this.formData = function() {
|
||||
throw new Error('Not implemented yet')
|
||||
return Promise.resolve(decode(this.body))
|
||||
}
|
||||
|
||||
this.json = function() {
|
||||
|
@ -115,6 +115,19 @@
|
|||
}).join('&').replace(/%20/g, '+')
|
||||
}
|
||||
|
||||
function decode(body) {
|
||||
var form = new FormData()
|
||||
body.trim().split('&').forEach(function(bytes) {
|
||||
if (bytes) {
|
||||
var split = bytes.split('=')
|
||||
var name = split.shift().replace(/\+/g, ' ')
|
||||
var value = split.join('=').replace(/\+/g, ' ')
|
||||
form.append(decodeURIComponent(name), decodeURIComponent(value))
|
||||
}
|
||||
})
|
||||
return form
|
||||
}
|
||||
|
||||
function isObject(value) {
|
||||
try {
|
||||
return Object.getPrototypeOf(value) === Object.prototype
|
||||
|
|
12
test/test.js
12
test/test.js
|
@ -8,6 +8,9 @@ MockXHR.responses = {
|
|||
'/error': function(xhr) {
|
||||
xhr.error()
|
||||
},
|
||||
'/form': function(xhr) {
|
||||
xhr.respond(200, 'number=1&space=one+two&empty=&encoded=a%2Bb&')
|
||||
},
|
||||
'/json': function(xhr) {
|
||||
xhr.respond(200, JSON.stringify({name: 'Hubot', login: 'hubot'}))
|
||||
},
|
||||
|
@ -80,6 +83,15 @@ asyncTest('resolves text promise', 1, function() {
|
|||
})
|
||||
})
|
||||
|
||||
asyncTest('parses form encoded response', 1, function() {
|
||||
fetch('/form').then(function(response) {
|
||||
return response.formData()
|
||||
}).then(function(form) {
|
||||
ok(form instanceof FormData, 'Parsed a FormData object')
|
||||
start()
|
||||
})
|
||||
})
|
||||
|
||||
asyncTest('parses json response', 2, function() {
|
||||
fetch('/json').then(function(response) {
|
||||
return response.json()
|
||||
|
|
Загрузка…
Ссылка в новой задаче