提交 9c2528d9 编写于 作者: M Matt Zabriskie

Adding initial source

上级 2c3a3bc0
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
module.exports = require('./lib/axios');
\ No newline at end of file
var Promise = require('es6-promise').Promise;
function axios(options) {
options = merge({
method: 'get'
}, options);
var promise = new Promise(function (resolve, reject) {
var request = new(XMLHttpRequest || ActiveXObject)('MSXML2.XMLHTTP.3.0');
function onload() {
if (request.status >= 200 && request.status < 300) {
resolve(parse(request.responseText));
} else {
onerror();
}
}
function onerror() {
reject(new Error('Can\'t connect to ' + JSON.stringify(options.url)));
}
try {
request.open(options.method, options.url, true);
request.onreadystatechange = function () {
if (request.readyState === 4) {
onload();
}
};
request.onload = request.load = onload;
request.onerror = request.error = onerror;
var headers = merge(
defaults.headers.common,
defaults.headers[options.method] || {},
options.headers || {}
);
for (var key in headers) {
request.setRequestHeader(key, headers[key]);
}
} catch (e) {
reject(e);
}
request.send(options.data || null);
});
promise.success = function (fn) {
promise.then(function(response) {
fn(response);
});
return promise;
};
promise.error = function(fn) {
promise.then(null, function(response) {
fn(response);
});
return promise;
};
return promise;
}
var CONTENT_TYPE_APPLICATION_JSON = {'Content-Type': 'application/json;charset=utf-8'};
var defaults = axios.defaults = {
headers: {
common: {'Accept': 'application/json, text/plain, */*'},
patch: merge(CONTENT_TYPE_APPLICATION_JSON),
post: merge(CONTENT_TYPE_APPLICATION_JSON),
put: merge(CONTENT_TYPE_APPLICATION_JSON)
}
};
function parse(response) {
try {
return JSON.parse(response);
} catch(e) {
return response;
}
}
function merge() {
var result = {};
forEach(arguments, function (obj) {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
result[key] = obj[key];
}
}
});
return result;
}
function forEach(arr, fn) {
for (var i=0, l=arr.length; i<l; i++) {
fn.call(null, arr[i], i, arr);
}
}
function createShortMethods() {
forEach(arguments, function (method) {
axios[method] = function (url, options) {
return axios(merge(options || {}, {
method: method,
url: url
}));
};
});
}
function createShortMethodsWithData() {
forEach(arguments, function (method) {
axios[method] = function (url, data, options) {
return axios(merge(options || {}, {
method: method,
url: url,
data: data
}));
};
});
}
createShortMethods('delete', 'get', 'head');
createShortMethodsWithData('post', 'put', 'patch');
module.exports = axios;
\ No newline at end of file
describe('axios', function () {
describe('api', function () {
it('should have request method helpers', function () {
expect(typeof axios.get).toEqual('function');
expect(typeof axios.head).toEqual('function');
expect(typeof axios.delete).toEqual('function');
expect(typeof axios.post).toEqual('function');
expect(typeof axios.put).toEqual('function');
expect(typeof axios.patch).toEqual('function');
});
it('should have promise method helpers', function () {
var promise = axios();
expect(typeof promise.then).toEqual('function');
expect(typeof promise.catch).toEqual('function');
expect(typeof promise.success).toEqual('function');
expect(typeof promise.error).toEqual('function');
});
it('should have defaults', function () {
expect(typeof axios.defaults).toEqual('object');
expect(typeof axios.defaults.headers).toEqual('object');
});
});
describe('wrapper', function () {
beforeEach(function () {
jasmine.Ajax.install();
});
it('should make an http request', function () {
axios({
url: '/foo'
});
var request = jasmine.Ajax.requests.mostRecent();
expect(request.url).toBe('/foo');
});
it('should default common headers', function () {
axios();
var request = jasmine.Ajax.requests.mostRecent();
var headers = axios.defaults.headers.common;
for (var key in headers) {
if (headers.hasOwnProperty(key)) {
expect(request.requestHeaders[key]).toEqual(headers[key]);
}
}
});
it('should add extra headers for post', function () {
axios({
method: 'post'
});
var request = jasmine.Ajax.requests.mostRecent();
var headers = axios.defaults.headers.post;
for (var key in headers) {
if (headers.hasOwnProperty(key)) {
expect(request.requestHeaders[key]).toEqual(headers[key]);
}
}
});
});
describe('options', function () {
beforeEach(function () {
jasmine.Ajax.install();
});
it('should default method to get', function () {
axios();
var request = jasmine.Ajax.requests.mostRecent();
expect(request.method).toBe('get');
});
it('should accept headers', function () {
axios({
headers: {
'X-Requested-With': 'XMLHttpRequest'
}
});
var request = jasmine.Ajax.requests.mostRecent();
expect(request.requestHeaders['X-Requested-With']).toEqual('XMLHttpRequest');
});
it('should allow overriding default headers', function () {
axios({
headers: {
'Accept': 'foo/bar'
}
});
var request = jasmine.Ajax.requests.mostRecent();
expect(request.requestHeaders['Accept']).toEqual('foo/bar');
});
});
});
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册