Fetch-style signature overload
This commit is contained in:
parent
bcb81c4b03
commit
beccd16dd7
2 changed files with 66 additions and 18 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
"use strict"
|
"use strict"
|
||||||
|
|
||||||
var buildQueryString = require("../querystring/build")
|
var buildQueryString = require("../querystring/build")
|
||||||
|
var Promise = require("../promise/promise")
|
||||||
|
|
||||||
module.exports = function($window, Stream) {
|
module.exports = function($window, Stream) {
|
||||||
var callbackCount = 0
|
var callbackCount = 0
|
||||||
|
|
@ -8,7 +9,18 @@ module.exports = function($window, Stream) {
|
||||||
var oncompletion
|
var oncompletion
|
||||||
function setCompletionCallback(callback) {oncompletion = callback}
|
function setCompletionCallback(callback) {oncompletion = callback}
|
||||||
|
|
||||||
function request(args) {
|
function request(args, extra) {
|
||||||
|
if(typeof args === "string"){
|
||||||
|
var url = args
|
||||||
|
|
||||||
|
if(typeof extra === "object") args = extra
|
||||||
|
else args = {}
|
||||||
|
|
||||||
|
if(typeof args.url === "undefined") args.url = url
|
||||||
|
}
|
||||||
|
|
||||||
|
if(typeof args.method === "undefined") args.method = "GET"
|
||||||
|
|
||||||
var stream = Stream()
|
var stream = Stream()
|
||||||
if (args.initialValue !== undefined) stream(args.initialValue)
|
if (args.initialValue !== undefined) stream(args.initialValue)
|
||||||
args.method = args.method.toUpperCase()
|
args.method = args.method.toUpperCase()
|
||||||
|
|
|
||||||
|
|
@ -27,13 +27,49 @@ o.spec("xhr", function() {
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
o("works via POST", function(done) {
|
o("implicit GET method", function(done){
|
||||||
|
var s = new Date
|
||||||
mock.$defineRoutes({
|
mock.$defineRoutes({
|
||||||
"GET /item": function() {
|
"GET /item": function() {
|
||||||
return {status: 200, responseText: JSON.stringify({a: 1})}
|
return {status: 200, responseText: JSON.stringify({a: 1})}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
xhr({method: "GET", url: "/item"}).run(function(data) {
|
xhr({url: "/item"}).run(function(data) {
|
||||||
|
o(data).deepEquals({a: 1})
|
||||||
|
}).run(function() {
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
o("first argument can be a string aliasing url property", function(done){
|
||||||
|
var s = new Date
|
||||||
|
mock.$defineRoutes({
|
||||||
|
"GET /item": function() {
|
||||||
|
return {status: 200, responseText: JSON.stringify({a: 1})}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
xhr("/item").run(function(data) {
|
||||||
|
o(data).deepEquals({a: 1})
|
||||||
|
}).run(function() {
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
o("works via POST", function(done) {
|
||||||
|
mock.$defineRoutes({
|
||||||
|
"POST /item": function() {
|
||||||
|
return {status: 200, responseText: JSON.stringify({a: 1})}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
xhr({method: "POST", url: "/item"}).run(function(data) {
|
||||||
|
o(data).deepEquals({a: 1})
|
||||||
|
}).run(done)
|
||||||
|
})
|
||||||
|
o("first argument can act as URI with second argument providing options", function(done) {
|
||||||
|
mock.$defineRoutes({
|
||||||
|
"POST /item": function() {
|
||||||
|
return {status: 200, responseText: JSON.stringify({a: 1})}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
xhr("/item", {method: "POST"}).run(function(data) {
|
||||||
o(data).deepEquals({a: 1})
|
o(data).deepEquals({a: 1})
|
||||||
}).run(done)
|
}).run(done)
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue