Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
jio
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
18
Merge Requests
18
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
nexedi
jio
Commits
31489917
Commit
31489917
authored
Mar 19, 2019
by
Romain Courteaud
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
test ajax Form Data handling
parent
671878a0
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
89 additions
and
8 deletions
+89
-8
test/node/ajax.tests.js
test/node/ajax.tests.js
+89
-8
No files found.
test/node/ajax.tests.js
View file @
31489917
...
...
@@ -17,12 +17,11 @@
* See COPYING file for full licensing terms.
* See https://www.nexedi.com/licensing for rationale and options.
*/
/*global FormData, sinon, RSVP, jIO, QUnit*/
/*global FormData, sinon, RSVP, jIO, QUnit
, XMLHttpRequest
*/
(
function
(
jIO
,
QUnit
,
FormData
)
{
"
use strict
"
;
var
test
=
QUnit
.
test
,
equal
=
QUnit
.
equal
,
deepEqual
=
QUnit
.
deepEqual
,
stop
=
QUnit
.
stop
,
start
=
QUnit
.
start
,
expect
=
QUnit
.
expect
,
...
...
@@ -36,19 +35,26 @@
this
.
server
=
sinon
.
fakeServer
.
create
();
this
.
server
.
autoRespond
=
true
;
this
.
server
.
autoRespondAfter
=
5
;
this
.
spySetRequestHeader
=
sinon
.
spy
(
XMLHttpRequest
.
prototype
,
"
setRequestHeader
"
);
},
teardown
:
function
()
{
this
.
spySetRequestHeader
.
restore
();
delete
this
.
spySetRequestHeader
;
this
.
server
.
restore
();
delete
this
.
server
;
}
});
test
(
"
FormData handling
"
,
function
()
{
test
(
"
FormData handling
without headers
"
,
function
()
{
stop
();
expect
(
3
);
expect
(
7
);
var
url
=
"
https://www.example.org/com/bar
"
,
server
=
this
.
server
;
server
=
this
.
server
,
context
=
this
;
this
.
server
.
respondWith
(
"
POST
"
,
url
,
[
200
,
{},
'
OK
'
]);
...
...
@@ -56,6 +62,60 @@
.
then
(
function
()
{
var
form_data
=
new
FormData
();
form_data
.
append
(
"
foo
"
,
"
bar
"
);
form_data
.
append
(
"
foo2
"
,
"
bar2
"
,
"
barfilename2
"
);
return
jIO
.
util
.
ajax
({
type
:
'
POST
'
,
url
:
url
,
data
:
form_data
});
})
.
then
(
function
()
{
var
content_type
=
"
multipart/form-data; boundary=----------------------------
"
,
boundary
;
equal
(
context
.
spySetRequestHeader
.
callCount
,
1
);
equal
(
context
.
spySetRequestHeader
.
firstCall
.
args
[
0
],
"
Content-Type
"
);
equal
(
context
.
spySetRequestHeader
.
firstCall
.
args
[
1
].
length
,
content_type
.
length
+
10
);
boundary
=
context
.
spySetRequestHeader
.
firstCall
.
args
[
1
].
slice
(
"
multipart/form-data; boundary=
"
.
length
);
equal
(
server
.
requests
.
length
,
1
);
equal
(
server
.
requests
[
0
].
method
,
"
POST
"
);
equal
(
server
.
requests
[
0
].
url
,
url
);
equal
(
server
.
requests
[
0
].
requestBody
,
'
--
'
+
boundary
+
'
\r\n
Content-Disposition: form-data; name="foo"
\r\n\r\n
bar
\r\n
'
+
'
--
'
+
boundary
+
'
\r\n
Content-Disposition: form-data; name="foo2";
'
+
'
filename="barfilename2"
\r\n\r\n
bar2
\r\n
'
+
'
--
'
+
boundary
+
'
--
\r\n
'
);
})
.
always
(
function
()
{
start
();
});
});
test
(
"
FormData handling with headers
"
,
function
()
{
stop
();
expect
(
9
);
var
url
=
"
https://www.example.org/com/bar
"
,
server
=
this
.
server
,
context
=
this
;
this
.
server
.
respondWith
(
"
POST
"
,
url
,
[
200
,
{},
'
OK
'
]);
return
new
RSVP
.
Queue
()
.
then
(
function
()
{
var
form_data
=
new
FormData
();
form_data
.
append
(
"
foo
"
,
"
bar
"
);
form_data
.
append
(
"
foo2
"
,
"
bar2
"
,
"
barfilename2
"
);
return
jIO
.
util
.
ajax
({
type
:
'
POST
'
,
url
:
url
,
...
...
@@ -64,12 +124,33 @@
});
})
.
then
(
function
()
{
var
content_type
=
"
multipart/form-data; boundary=----------------------------
"
,
boundary
;
equal
(
context
.
spySetRequestHeader
.
callCount
,
2
);
equal
(
context
.
spySetRequestHeader
.
firstCall
.
args
[
0
],
"
bar
"
);
equal
(
context
.
spySetRequestHeader
.
firstCall
.
args
[
1
],
"
foo
"
);
equal
(
context
.
spySetRequestHeader
.
secondCall
.
args
[
0
],
"
Content-Type
"
);
equal
(
context
.
spySetRequestHeader
.
secondCall
.
args
[
1
].
length
,
content_type
.
length
+
10
);
boundary
=
context
.
spySetRequestHeader
.
secondCall
.
args
[
1
].
slice
(
"
multipart/form-data; boundary=
"
.
length
);
equal
(
server
.
requests
.
length
,
1
);
equal
(
server
.
requests
[
0
].
method
,
"
POST
"
);
equal
(
server
.
requests
[
0
].
url
,
url
);
equal
(
server
.
requests
[
0
].
requestBody
,
undefined
);
deepEqual
(
server
.
requests
[
0
].
requestHeaders
,
{
'
X-ACCESS-TOKEN
'
:
'
footoken
'
});
equal
(
server
.
requests
[
0
].
requestBody
,
'
--
'
+
boundary
+
'
\r\n
Content-Disposition: form-data; name="foo"
\r\n\r\n
bar
\r\n
'
+
'
--
'
+
boundary
+
'
\r\n
Content-Disposition: form-data; name="foo2";
'
+
'
filename="barfilename2"
\r\n\r\n
bar2
\r\n
'
+
'
--
'
+
boundary
+
'
--
\r\n
'
);
})
.
always
(
function
()
{
start
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment