Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xjbieke
axios
提交
5bb39f32
A
axios
项目概览
xjbieke
/
axios
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
axios
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
5bb39f32
编写于
12月 02, 2014
作者:
J
Jason Dobry
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Initial interceptor implementation.
上级
d93df704
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
680 addition
and
168 deletion
+680
-168
karma.conf.js
karma.conf.js
+3
-2
lib/axios.js
lib/axios.js
+50
-14
test/specs/interceptors.spec.js
test/specs/interceptors.spec.js
+299
-0
test/specs/options.spec.js
test/specs/options.spec.js
+67
-27
test/specs/promise.spec.js
test/specs/promise.spec.js
+39
-27
test/specs/transform.spec.js
test/specs/transform.spec.js
+70
-34
test/specs/wrapper.spec.js
test/specs/wrapper.spec.js
+120
-55
test/specs/xsrf.spec.js
test/specs/xsrf.spec.js
+32
-9
未找到文件。
karma.conf.js
浏览文件 @
5bb39f32
...
...
@@ -16,20 +16,21 @@ module.exports = function(config) {
// list of files / patterns to load in the browser
files
:
[
'
dist/axios.js
'
,
'
node_modules/es6-promise/dist/promise-1.0.0.js
'
,
'
test/specs/**/*.spec.js
'
],
// list of files to exclude
exclude
:
[
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors
:
{
},
...
...
lib/axios.js
浏览文件 @
5bb39f32
...
...
@@ -13,20 +13,22 @@ var axios = module.exports = function axios(config) {
// Don't allow overriding defaults.withCredentials
config
.
withCredentials
=
config
.
withCredentials
||
defaults
.
withCredentials
;
var
promise
=
new
Promise
(
function
(
resolve
,
reject
)
{
try
{
// For browsers use XHR adapter
if
(
typeof
window
!==
'
undefined
'
)
{
require
(
'
./adapters/xhr
'
)(
resolve
,
reject
,
config
);
}
// For node use HTTP adapter
else
if
(
typeof
process
!==
'
undefined
'
)
{
require
(
'
./adapters/http
'
)(
resolve
,
reject
,
config
);
var
serverRequest
=
function
(
config
)
{
return
new
Promise
(
function
(
resolve
,
reject
)
{
try
{
// For browsers use XHR adapter
if
(
typeof
window
!==
'
undefined
'
)
{
require
(
'
./adapters/xhr
'
)(
resolve
,
reject
,
config
);
}
// For node use HTTP adapter
else
if
(
typeof
process
!==
'
undefined
'
)
{
require
(
'
./adapters/http
'
)(
resolve
,
reject
,
config
);
}
}
catch
(
e
)
{
reject
(
e
);
}
}
catch
(
e
)
{
reject
(
e
);
}
});
});
};
function
deprecatedMethod
(
method
,
instead
,
docs
)
{
try
{
...
...
@@ -41,6 +43,24 @@ var axios = module.exports = function axios(config) {
}
catch
(
e
)
{}
}
var
chain
=
[
serverRequest
,
undefined
];
var
promise
=
Promise
.
resolve
(
config
);
utils
.
forEach
(
axios
.
interceptors
.
request
.
handlers
,
function
(
interceptor
)
{
chain
.
unshift
(
interceptor
.
request
,
interceptor
.
requestError
);
});
utils
.
forEach
(
axios
.
interceptors
.
response
.
handlers
,
function
(
interceptor
)
{
chain
.
push
(
interceptor
.
response
,
interceptor
.
responseError
);
});
while
(
chain
.
length
)
{
var
thenFn
=
chain
.
shift
();
var
rejectFn
=
chain
.
shift
();
promise
=
promise
.
then
(
thenFn
,
rejectFn
);
}
// Provide alias for success
promise
.
success
=
function
success
(
fn
)
{
deprecatedMethod
(
'
success
'
,
'
then
'
,
'
https://github.com/mzabriskie/axios/blob/master/README.md#response-api
'
);
...
...
@@ -73,6 +93,22 @@ axios.all = function (promises) {
};
axios
.
spread
=
require
(
'
./helpers/spread
'
);
// interceptors
axios
.
interceptors
=
{
request
:
{
handlers
:
[],
use
:
function
(
thenFn
,
rejectFn
)
{
axios
.
interceptors
.
request
.
handlers
.
push
({
request
:
thenFn
,
requestError
:
rejectFn
});
}
},
response
:
{
handlers
:
[],
use
:
function
(
thenFn
,
rejectFn
)
{
axios
.
interceptors
.
response
.
handlers
.
push
({
response
:
thenFn
,
responseError
:
rejectFn
});
}
}
};
// Provide aliases for supported request methods
createShortMethods
(
'
delete
'
,
'
get
'
,
'
head
'
);
createShortMethodsWithData
(
'
post
'
,
'
put
'
,
'
patch
'
);
...
...
@@ -98,4 +134,4 @@ function createShortMethodsWithData() {
}));
};
});
}
\ No newline at end of file
}
test/specs/interceptors.spec.js
0 → 100644
浏览文件 @
5bb39f32
describe
(
'
interceptors
'
,
function
()
{
beforeEach
(
function
()
{
jasmine
.
Ajax
.
install
();
});
afterEach
(
function
()
{
jasmine
.
Ajax
.
uninstall
();
axios
.
interceptors
.
request
.
handlers
=
[];
axios
.
interceptors
.
response
.
handlers
=
[];
});
it
(
'
should add a request interceptor
'
,
function
()
{
var
request
;
runs
(
function
()
{
axios
.
interceptors
.
request
.
use
(
function
(
config
)
{
config
.
headers
.
test
=
'
added by interceptor
'
;
return
config
;
});
axios
({
url
:
'
/foo
'
});
});
waitsFor
(
function
()
{
return
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
},
'
waiting for the request
'
,
100
);
runs
(
function
()
{
request
.
response
({
status
:
200
,
responseText
:
'
OK
'
});
expect
(
request
.
requestHeaders
.
test
).
toBe
(
'
added by interceptor
'
);
});
});
it
(
'
should add a request interceptor that returns a new config object
'
,
function
()
{
var
request
;
runs
(
function
()
{
axios
.
interceptors
.
request
.
use
(
function
()
{
return
{
url
:
'
/bar
'
,
method
:
'
post
'
};
});
axios
({
url
:
'
/foo
'
});
});
waitsFor
(
function
()
{
return
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
},
'
waiting for the request
'
,
100
);
runs
(
function
()
{
request
.
response
({
status
:
200
,
responseText
:
'
OK
'
});
expect
(
request
.
method
).
toBe
(
'
post
'
);
expect
(
request
.
url
).
toBe
(
'
/bar
'
);
});
});
it
(
'
should add a request interceptor that returns a promise
'
,
function
()
{
var
request
;
runs
(
function
()
{
axios
.
interceptors
.
request
.
use
(
function
(
config
)
{
return
new
Promise
(
function
(
resolve
)
{
// do something async
setTimeout
(
function
()
{
config
.
headers
.
async
=
'
promise
'
;
resolve
(
config
);
},
10
);
});
});
axios
({
url
:
'
/foo
'
});
});
waitsFor
(
function
()
{
return
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
},
'
waiting for the request
'
,
100
);
runs
(
function
()
{
request
.
response
({
status
:
200
,
responseText
:
'
OK
'
});
expect
(
request
.
requestHeaders
.
async
).
toBe
(
'
promise
'
);
});
});
it
(
'
should add multiple request interceptors
'
,
function
()
{
var
request
;
runs
(
function
()
{
axios
.
interceptors
.
request
.
use
(
function
(
config
)
{
config
.
headers
.
test1
=
'
1
'
;
return
config
;
});
axios
.
interceptors
.
request
.
use
(
function
(
config
)
{
config
.
headers
.
test2
=
'
2
'
;
return
config
;
});
axios
.
interceptors
.
request
.
use
(
function
(
config
)
{
config
.
headers
.
test3
=
'
3
'
;
return
config
;
});
axios
({
url
:
'
/foo
'
});
});
waitsFor
(
function
()
{
return
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
},
'
waiting for the request
'
,
100
);
runs
(
function
()
{
request
.
response
({
status
:
200
,
responseText
:
'
OK
'
});
expect
(
request
.
requestHeaders
.
test1
).
toBe
(
'
1
'
);
expect
(
request
.
requestHeaders
.
test2
).
toBe
(
'
2
'
);
expect
(
request
.
requestHeaders
.
test3
).
toBe
(
'
3
'
);
});
});
it
(
'
should add a response interceptor
'
,
function
()
{
var
request
,
response
;
runs
(
function
()
{
axios
.
interceptors
.
response
.
use
(
function
(
data
)
{
data
.
data
=
data
.
data
+
'
- modified by interceptor
'
;
return
data
;
});
axios
({
url
:
'
/foo
'
}).
then
(
function
(
data
)
{
response
=
data
;
});
});
waitsFor
(
function
()
{
return
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
},
'
waiting for the request
'
,
100
);
runs
(
function
()
{
request
.
response
({
status
:
200
,
responseText
:
'
OK
'
});
});
waitsFor
(
function
()
{
return
response
;
},
'
waiting for the response
'
,
100
);
runs
(
function
()
{
expect
(
response
.
data
).
toBe
(
'
OK - modified by interceptor
'
);
});
});
it
(
'
should add a response interceptor that returns a new data object
'
,
function
()
{
var
request
,
response
;
runs
(
function
()
{
axios
.
interceptors
.
response
.
use
(
function
()
{
return
{
data
:
'
stuff
'
};
});
axios
({
url
:
'
/foo
'
}).
then
(
function
(
data
)
{
response
=
data
;
});
});
waitsFor
(
function
()
{
return
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
},
'
waiting for the request
'
,
100
);
runs
(
function
()
{
request
.
response
({
status
:
200
,
responseText
:
'
OK
'
});
});
waitsFor
(
function
()
{
return
response
;
},
'
waiting for the response
'
,
100
);
runs
(
function
()
{
expect
(
response
.
data
).
toBe
(
'
stuff
'
);
});
});
it
(
'
should add a response interceptor that returns a promise
'
,
function
()
{
var
request
,
response
;
runs
(
function
()
{
axios
.
interceptors
.
response
.
use
(
function
(
data
)
{
return
new
Promise
(
function
(
resolve
)
{
// do something async
setTimeout
(
function
()
{
data
.
data
=
'
you have been promised!
'
;
resolve
(
data
);
},
10
);
});
});
axios
({
url
:
'
/foo
'
}).
then
(
function
(
data
)
{
response
=
data
;
});
});
waitsFor
(
function
()
{
return
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
},
'
waiting for the request
'
,
100
);
runs
(
function
()
{
request
.
response
({
status
:
200
,
responseText
:
'
OK
'
});
});
waitsFor
(
function
()
{
return
response
;
},
'
waiting for the response
'
,
100
);
runs
(
function
()
{
expect
(
response
.
data
).
toBe
(
'
you have been promised!
'
);
});
});
it
(
'
should add multiple response interceptors
'
,
function
()
{
var
request
,
response
;
runs
(
function
()
{
axios
.
interceptors
.
response
.
use
(
function
(
data
)
{
data
.
data
=
data
.
data
+
'
1
'
;
return
data
;
});
axios
.
interceptors
.
response
.
use
(
function
(
data
)
{
data
.
data
=
data
.
data
+
'
2
'
;
return
data
;
});
axios
.
interceptors
.
response
.
use
(
function
(
data
)
{
data
.
data
=
data
.
data
+
'
3
'
;
return
data
;
});
axios
({
url
:
'
/foo
'
}).
then
(
function
(
data
)
{
response
=
data
;
});
});
waitsFor
(
function
()
{
return
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
},
'
waiting for the request
'
,
100
);
runs
(
function
()
{
request
.
response
({
status
:
200
,
responseText
:
'
OK
'
});
});
waitsFor
(
function
()
{
return
response
;
},
'
waiting for the response
'
,
100
);
runs
(
function
()
{
expect
(
response
.
data
).
toBe
(
'
OK123
'
);
});
});
});
test/specs/options.spec.js
浏览文件 @
5bb39f32
...
...
@@ -3,49 +3,89 @@ describe('options', function () {
jasmine
.
Ajax
.
install
();
});
afterEach
(
function
()
{
jasmine
.
Ajax
.
uninstall
();
});
it
(
'
should default method to get
'
,
function
()
{
axios
({
url
:
'
/foo
'
var
request
;
runs
(
function
()
{
axios
({
url
:
'
/foo
'
});
});
var
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
expect
(
request
.
method
).
toBe
(
'
get
'
);
waitsFor
(
function
()
{
return
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
},
'
waiting for the request
'
,
100
);
runs
(
function
()
{
expect
(
request
.
method
).
toBe
(
'
get
'
);
});
});
it
(
'
should accept headers
'
,
function
()
{
axios
({
url
:
'
/foo
'
,
headers
:
{
'
X-Requested-With
'
:
'
XMLHttpRequest
'
}
var
request
;
runs
(
function
()
{
axios
({
url
:
'
/foo
'
,
headers
:
{
'
X-Requested-With
'
:
'
XMLHttpRequest
'
}
});
});
var
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
expect
(
request
.
requestHeaders
[
'
X-Requested-With
'
]).
toEqual
(
'
XMLHttpRequest
'
);
waitsFor
(
function
()
{
return
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
},
'
waiting for the request
'
,
100
);
runs
(
function
()
{
expect
(
request
.
requestHeaders
[
'
X-Requested-With
'
]).
toEqual
(
'
XMLHttpRequest
'
);
});
});
it
(
'
should accept params
'
,
function
()
{
axios
({
url
:
'
/foo
'
,
params
:
{
foo
:
123
,
bar
:
456
}
var
request
;
runs
(
function
()
{
axios
({
url
:
'
/foo
'
,
params
:
{
foo
:
123
,
bar
:
456
}
});
});
var
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
expect
(
request
.
url
).
toBe
(
'
/foo?foo=123&bar=456
'
);
waitsFor
(
function
()
{
return
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
},
'
waiting for the request
'
,
100
);
runs
(
function
()
{
expect
(
request
.
url
).
toBe
(
'
/foo?foo=123&bar=456
'
);
});
});
it
(
'
should allow overriding default headers
'
,
function
()
{
axios
({
url
:
'
/foo
'
,
headers
:
{
'
Accept
'
:
'
foo/bar
'
}
var
request
;
runs
(
function
()
{
axios
({
url
:
'
/foo
'
,
headers
:
{
'
Accept
'
:
'
foo/bar
'
}
});
});
var
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
expect
(
request
.
requestHeaders
[
'
Accept
'
]).
toEqual
(
'
foo/bar
'
);
waitsFor
(
function
()
{
return
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
},
'
waiting for the request
'
,
100
);
runs
(
function
()
{
expect
(
request
.
requestHeaders
[
'
Accept
'
]).
toEqual
(
'
foo/bar
'
);
});
});
});
\ No newline at end of file
});
test/specs/promise.spec.js
浏览文件 @
5bb39f32
...
...
@@ -3,27 +3,36 @@ describe('promise', function () {
jasmine
.
Ajax
.
install
();
});
afterEach
(
function
()
{
jasmine
.
Ajax
.
uninstall
();
});
it
(
'
should provide succinct object to then
'
,
function
()
{
var
response
;
var
fulfilled
=
false
;
var
request
,
response
;
axios
({
url
:
'
/foo
'
}).
then
(
function
(
r
)
{
runs
(
function
()
{
axios
({
url
:
'
/foo
'
}).
then
(
function
(
r
)
{
response
=
r
;
fulfilled
=
true
;
});
var
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
request
.
response
({
status
:
200
,
responseText
:
'
{"hello":"world"}
'
});
waitsFor
(
function
()
{
return
fulfilled
;
return
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
},
'
waiting for the request
'
,
100
);
runs
(
function
()
{
request
.
response
({
status
:
200
,
responseText
:
'
{"hello":"world"}
'
});
});
waitsFor
(
function
()
{
return
response
;
},
'
waiting for the response
'
,
100
);
runs
(
function
()
{
expect
(
typeof
response
).
toEqual
(
'
object
'
);
expect
(
response
.
data
.
hello
).
toEqual
(
'
world
'
);
...
...
@@ -34,32 +43,35 @@ describe('promise', function () {
});
it
(
'
should provide verbose arguments to success
'
,
function
()
{
var
data
;
var
status
;
var
headers
;
var
config
;
var
fulfilled
=
false
;
var
request
,
data
,
status
,
headers
,
config
;
axios
({
url
:
'
/foo
'
}).
success
(
function
(
d
,
s
,
h
,
c
)
{
runs
(
function
()
{
axios
({
url
:
'
/foo
'
}).
success
(
function
(
d
,
s
,
h
,
c
)
{
data
=
d
;
status
=
s
;
headers
=
h
;
config
=
c
;
fulfilled
=
true
;
});
var
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
request
.
response
({
status
:
200
,
responseText
:
'
{"hello":"world"}
'
});
waitsFor
(
function
()
{
return
fulfilled
;
return
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
},
'
waiting for the request
'
,
100
);
runs
(
function
()
{
request
.
response
({
status
:
200
,
responseText
:
'
{"hello":"world"}
'
});
});
waitsFor
(
function
()
{
return
data
;
},
'
waiting for the data
'
,
100
);
runs
(
function
()
{
expect
(
data
.
hello
).
toEqual
(
'
world
'
);
expect
(
status
).
toBe
(
200
);
...
...
@@ -102,4 +114,4 @@ describe('promise', function () {
expect
(
sum
).
toEqual
(
123
+
456
);
});
});
});
\ No newline at end of file
});
test/specs/transform.spec.js
浏览文件 @
5bb39f32
...
...
@@ -3,70 +3,106 @@ describe('transform', function () {
jasmine
.
Ajax
.
install
();
});
afterEach
(
function
()
{
jasmine
.
Ajax
.
uninstall
();
});
it
(
'
should transform JSON to string
'
,
function
()
{
var
request
;
var
data
=
{
foo
:
'
bar
'
};
axios
({
url
:
'
/foo
'
,
method
:
'
post
'
,
data
:
data
runs
(
function
()
{
axios
({
url
:
'
/foo
'
,
method
:
'
post
'
,
data
:
data
});
});
var
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
expect
(
request
.
params
).
toEqual
(
'
{"foo":"bar"}
'
);
waitsFor
(
function
()
{
return
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
},
'
waiting for the request
'
,
100
);
runs
(
function
()
{
expect
(
request
.
params
).
toEqual
(
'
{"foo":"bar"}
'
);
});
});
it
(
'
should override default transform
'
,
function
()
{
var
request
;
var
data
=
{
foo
:
'
bar
'
};
axios
({
url
:
'
/foo
'
,
method
:
'
post
'
,
data
:
data
,
transformRequest
:
function
(
data
)
{
return
data
;
}
runs
(
function
()
{
axios
({
url
:
'
/foo
'
,
method
:
'
post
'
,
data
:
data
,
transformRequest
:
function
(
data
)
{
return
data
;
}
});
});
var
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
expect
(
typeof
request
.
params
).
toEqual
(
'
object
'
);
waitsFor
(
function
()
{
return
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
},
'
waiting for the request
'
,
100
);
runs
(
function
()
{
expect
(
typeof
request
.
params
).
toEqual
(
'
object
'
);
});
});
it
(
'
should allow an Array of transformers
'
,
function
()
{
var
request
;
var
data
=
{
foo
:
'
bar
'
};
axios
({
url
:
'
/foo
'
,
method
:
'
post
'
,
data
:
data
,
transformRequest
:
axios
.
defaults
.
transformRequest
.
concat
(
function
(
data
)
{
return
data
.
replace
(
'
bar
'
,
'
baz
'
);
}
)
runs
(
function
()
{
axios
({
url
:
'
/foo
'
,
method
:
'
post
'
,
data
:
data
,
transformRequest
:
axios
.
defaults
.
transformRequest
.
concat
(
function
(
data
)
{
return
data
.
replace
(
'
bar
'
,
'
baz
'
);
}
)
});
});
var
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
expect
(
request
.
params
).
toEqual
(
'
{"foo":"baz"}
'
);
waitsFor
(
function
()
{
return
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
},
'
waiting for the request
'
,
100
);
runs
(
function
()
{
expect
(
request
.
params
).
toEqual
(
'
{"foo":"baz"}
'
);
});
});
it
(
'
should allowing mutating headers
'
,
function
()
{
var
token
=
Math
.
floor
(
Math
.
random
()
*
Math
.
pow
(
2
,
64
)).
toString
(
36
);
var
request
;
axios
({
url
:
'
/foo
'
,
transformRequest
:
function
(
data
,
headers
)
{
headers
[
'
X-Authorization
'
]
=
token
;
}
runs
(
function
()
{
axios
({
url
:
'
/foo
'
,
transformRequest
:
function
(
data
,
headers
)
{
headers
[
'
X-Authorization
'
]
=
token
;
}
});
});
var
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
expect
(
request
.
requestHeaders
[
'
X-Authorization
'
]).
toEqual
(
token
);
waitsFor
(
function
()
{
return
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
},
'
waiting for the request
'
,
100
);
runs
(
function
()
{
expect
(
request
.
requestHeaders
[
'
X-Authorization
'
]).
toEqual
(
token
);
});
});
});
\ No newline at end of file
});
test/specs/wrapper.spec.js
浏览文件 @
5bb39f32
...
...
@@ -3,102 +3,167 @@ describe('wrapper', function () {
jasmine
.
Ajax
.
install
();
});
afterEach
(
function
()
{
jasmine
.
Ajax
.
uninstall
();
});
it
(
'
should make an http request
'
,
function
()
{
axios
({
url
:
'
/foo
'
var
request
;
runs
(
function
()
{
axios
({
url
:
'
/foo
'
});
});
var
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
expect
(
request
.
url
).
toBe
(
'
/foo
'
);
waitsFor
(
function
()
{
return
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
},
'
waiting for the request
'
,
100
);
runs
(
function
()
{
expect
(
request
.
url
).
toBe
(
'
/foo
'
)
});
});
it
(
'
should default common headers
'
,
function
()
{
axios
({
url
:
'
/foo
'
var
request
;
var
headers
=
axios
.
defaults
.
headers
.
common
;
runs
(
function
()
{
axios
({
url
:
'
/foo
'
});
});
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
]);
waitsFor
(
function
()
{
return
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
},
'
waiting for the request
'
,
100
);
runs
(
function
()
{
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
'
,
url
:
'
/foo
'
,
data
:
'
fizz=buzz
'
var
request
;
var
headers
=
axios
.
defaults
.
headers
.
common
;
runs
(
function
()
{
axios
({
method
:
'
post
'
,
url
:
'
/foo
'
,
data
:
'
fizz=buzz
'
});
});
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
]);
waitsFor
(
function
()
{
return
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
},
'
waiting for the request
'
,
100
);
runs
(
function
()
{
for
(
var
key
in
headers
)
{
if
(
headers
.
hasOwnProperty
(
key
))
{
expect
(
request
.
requestHeaders
[
key
]).
toEqual
(
headers
[
key
]);
}
}
}
}
);
});
it
(
'
should use application/json when posting an object
'
,
function
()
{
axios
({
url
:
'
/foo/bar
'
,
method
:
'
post
'
,
data
:
{
firstName
:
'
foo
'
,
lastName
:
'
bar
'
}
var
request
;
runs
(
function
()
{
axios
({
url
:
'
/foo/bar
'
,
method
:
'
post
'
,
data
:
{
firstName
:
'
foo
'
,
lastName
:
'
bar
'
}
});
});
var
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
expect
(
request
.
requestHeaders
[
'
Content-Type
'
]).
toEqual
(
'
application/json;charset=utf-8
'
);
waitsFor
(
function
()
{
return
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
},
'
waiting for the request
'
,
100
);
runs
(
function
()
{
expect
(
request
.
requestHeaders
[
'
Content-Type
'
]).
toEqual
(
'
application/json;charset=utf-8
'
);
});
});
it
(
'
should support binary data as array buffer
'
,
function
()
{
var
request
;
var
input
=
new
Int8Array
(
2
);
input
[
0
]
=
1
;
input
[
1
]
=
2
;
axios
({
method
:
'
post
'
,
url
:
'
/foo
'
,
data
:
input
.
buffer
runs
(
function
()
{
axios
({
method
:
'
post
'
,
url
:
'
/foo
'
,
data
:
input
.
buffer
});
});
var
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
var
output
=
new
Int8Array
(
request
.
params
.
buffer
);
expect
(
output
.
length
).
toEqual
(
2
);
expect
(
output
[
0
]).
toEqual
(
1
);
expect
(
output
[
1
]).
toEqual
(
2
);
waitsFor
(
function
()
{
return
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
},
'
waiting for the request
'
,
100
);
runs
(
function
()
{
var
output
=
new
Int8Array
(
request
.
params
.
buffer
);
expect
(
output
.
length
).
toEqual
(
2
);
expect
(
output
[
0
]).
toEqual
(
1
);
expect
(
output
[
1
]).
toEqual
(
2
);
});
});
it
(
'
should support binary data as array buffer view
'
,
function
()
{
var
request
;
var
input
=
new
Int8Array
(
2
);
input
[
0
]
=
1
;
input
[
1
]
=
2
;
axios
({
method
:
'
post
'
,
url
:
'
/foo
'
,
data
:
input
runs
(
function
()
{
axios
({
method
:
'
post
'
,
url
:
'
/foo
'
,
data
:
input
});
});
var
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
var
output
=
new
Int8Array
(
request
.
params
.
buffer
);
expect
(
output
.
length
).
toEqual
(
2
);
expect
(
output
[
0
]).
toEqual
(
1
);
expect
(
output
[
1
]).
toEqual
(
2
);
waitsFor
(
function
()
{
return
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
},
'
waiting for the request
'
,
100
);
runs
(
function
()
{
var
output
=
new
Int8Array
(
request
.
params
.
buffer
);
expect
(
output
.
length
).
toEqual
(
2
);
expect
(
output
[
0
]).
toEqual
(
1
);
expect
(
output
[
1
]).
toEqual
(
2
);
});
});
it
(
'
should remove content-type if data is empty
'
,
function
()
{
axios
({
method
:
'
post
'
,
url
:
'
/foo
'
var
request
;
runs
(
function
()
{
axios
({
method
:
'
post
'
,
url
:
'
/foo
'
});
});
var
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
expect
(
request
.
requestHeaders
[
'
content-type
'
]).
toEqual
(
undefined
);
waitsFor
(
function
()
{
return
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
},
'
waiting for the request
'
,
100
);
runs
(
function
()
{
expect
(
request
.
requestHeaders
[
'
content-type
'
]).
toEqual
(
undefined
);
});
});
});
\ No newline at end of file
});
test/specs/xsrf.spec.js
浏览文件 @
5bb39f32
describe
(
'
xsrf
'
,
function
()
{
beforeEach
(
function
()
{
jasmine
.
Ajax
.
install
();
});
afterEach
(
function
()
{
document
.
cookie
=
axios
.
defaults
.
xsrfCookieName
+
'
=;expires=
'
+
new
Date
(
Date
.
now
()
-
86400000
).
toGMTString
();
jasmine
.
Ajax
.
uninstall
();
});
it
(
'
should not set xsrf header if cookie is null
'
,
function
()
{
axios
({
url
:
'
/foo
'
var
request
;
runs
(
function
()
{
axios
({
url
:
'
/foo
'
});
});
var
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
expect
(
request
.
requestHeaders
[
axios
.
defaults
.
xsrfHeaderName
]).
toEqual
(
undefined
);
waitsFor
(
function
()
{
return
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
},
'
waiting for the request
'
,
100
);
runs
(
function
()
{
expect
(
request
.
requestHeaders
[
axios
.
defaults
.
xsrfHeaderName
]).
toEqual
(
undefined
);
});
});
it
(
'
should set xsrf header if cookie is set
'
,
function
()
{
var
request
;
document
.
cookie
=
axios
.
defaults
.
xsrfCookieName
+
'
=12345
'
;
axios
({
url
:
'
/foo
'
runs
(
function
()
{
axios
({
url
:
'
/foo
'
});
});
var
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
expect
(
request
.
requestHeaders
[
axios
.
defaults
.
xsrfHeaderName
]).
toEqual
(
'
12345
'
);
waitsFor
(
function
()
{
return
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
},
'
waiting for the request
'
,
100
);
runs
(
function
()
{
expect
(
request
.
requestHeaders
[
axios
.
defaults
.
xsrfHeaderName
]).
toEqual
(
'
12345
'
);
});
});
});
\ No newline at end of file
});
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录