Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
persona
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hexang 息壤平台
persona
Commits
2ead7961
Commit
2ead7961
authored
13 years ago
by
Lloyd Hilaiel
Browse files
Options
Downloads
Patches
Plain Diff
port unit tests to new wsapi_client abstraction to eliminate duplicate code
parent
9547deab
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
browserid/tests/lib/wsapi.js
+11
-115
11 additions, 115 deletions
browserid/tests/lib/wsapi.js
libs/wsapi_client.js
+5
-0
5 additions, 0 deletions
libs/wsapi_client.js
with
16 additions
and
115 deletions
browserid/tests/lib/wsapi.js
+
11
−
115
View file @
2ead7961
...
...
@@ -34,132 +34,28 @@
* ***** END LICENSE BLOCK ***** */
const
http
=
require
(
'
http
'
),
querystring
=
require
(
'
querystring
'
);
wcli
=
require
(
'
../../../libs/wsapi_client
'
);
//
wsapi abstractions trivial cookie jar
var
co
okieJar
=
{};
//
the client "context"
var
co
ntext
=
{};
// fetch the CSRF token for POSTs
var
g_csrf
=
undefined
;
var
fetchCSRF
=
function
(
headers
,
cb
)
{
// used cached csrf if available
if
(
g_csrf
)
return
setTimeout
(
function
()
{
cb
(
g_csrf
);
},
0
);
// the configuration
var
configuration
=
{
browserid
:
'
http://127.0.0.1:62700/
'
}
// otherwise, get a new one
return
http
.
get
({
host
:
'
127.0.0.1
'
,
port
:
'
62700
'
,
path
:
'
/wsapi/csrf
'
,
headers
:
headers
},
function
(
res
)
{
extractCookies
(
res
);
var
body
=
""
;
res
.
on
(
'
data
'
,
function
(
chunk
)
{
body
+=
chunk
;
})
.
on
(
'
end
'
,
function
()
{
g_csrf
=
body
;
cb
(
body
);
});
}).
on
(
'
error
'
,
function
(
e
)
{
cb
();
});
exports
.
clearCookies
=
function
()
{
wcli
.
clearCookies
(
context
);
};
exports
.
clearCookies
=
function
()
{
cookieJar
=
{};
g_csrf
=
undefined
;
};
function
extractCookies
(
res
)
{
if
(
res
.
headers
[
'
set-cookie
'
])
{
res
.
headers
[
'
set-cookie
'
].
forEach
(
function
(
cookie
)
{
var
m
=
/^
([^
;
]
+
)(?:
;.*
)
$/
.
exec
(
cookie
);
if
(
m
)
{
var
x
=
m
[
1
].
split
(
'
=
'
);
cookieJar
[
x
[
0
]]
=
x
[
1
];
}
});
}
}
// A macro for wsapi requests
exports
.
get
=
function
(
path
,
getArgs
)
{
return
function
()
{
var
cb
=
this
.
callback
;
if
(
typeof
getArgs
===
'
object
'
)
path
+=
"
?
"
+
querystring
.
stringify
(
getArgs
);
var
headers
=
{};
if
(
Object
.
keys
(
cookieJar
).
length
)
{
headers
[
'
Cookie
'
]
=
""
;
for
(
var
k
in
cookieJar
)
{
headers
[
'
Cookie
'
]
+=
k
+
"
=
"
+
cookieJar
[
k
];
}
}
http
.
get
({
host
:
'
127.0.0.1
'
,
port
:
'
62700
'
,
path
:
path
,
headers
:
headers
},
function
(
res
)
{
// see if there are any set-cookies that we should honor
extractCookies
(
res
);
var
body
=
''
;
res
.
on
(
'
data
'
,
function
(
chunk
)
{
body
+=
chunk
;
})
.
on
(
'
end
'
,
function
()
{
cb
({
code
:
res
.
statusCode
,
headers
:
res
.
headers
,
body
:
body
});
});
}).
on
(
'
error
'
,
function
(
e
)
{
cb
();
});
wcli
.
get
(
configuration
,
path
,
context
,
getArgs
,
this
.
callback
);
};
};
// FIXME: dedup code
// A macro for wsapi requests
// add CSRF protection to this test
exports
.
post
=
function
(
path
,
postArgs
)
{
return
function
()
{
var
cb
=
this
.
callback
;
var
headers
=
{
'
content-type
'
:
'
application/x-www-form-urlencoded
'
};
fetchCSRF
(
headers
,
function
()
{
if
(
Object
.
keys
(
cookieJar
).
length
)
{
headers
[
'
Cookie
'
]
=
""
;
for
(
var
k
in
cookieJar
)
{
headers
[
'
Cookie
'
]
+=
k
+
"
=
"
+
cookieJar
[
k
];
}
}
if
(
typeof
postArgs
===
'
object
'
)
{
postArgs
[
'
csrf
'
]
=
g_csrf
;
body
=
querystring
.
stringify
(
postArgs
);
}
var
req
=
http
.
request
({
host
:
'
127.0.0.1
'
,
port
:
'
62700
'
,
path
:
path
,
headers
:
headers
,
method
:
"
POST
"
},
function
(
res
)
{
extractCookies
(
res
);
var
body
=
''
;
res
.
on
(
'
data
'
,
function
(
chunk
)
{
body
+=
chunk
;
})
.
on
(
'
end
'
,
function
()
{
cb
({
code
:
res
.
statusCode
,
headers
:
res
.
headers
,
body
:
body
});
});
}).
on
(
'
error
'
,
function
(
e
)
{
cb
();
});
// send the POST
req
.
write
(
body
);
req
.
end
();
});
wcli
.
post
(
configuration
,
path
,
context
,
postArgs
,
this
.
callback
);
};
};
This diff is collapsed.
Click to expand it.
libs/wsapi_client.js
+
5
−
0
View file @
2ead7961
...
...
@@ -69,6 +69,11 @@ function extractCookies(ctx, res) {
}
}
exports
.
clearCookies
=
function
(
ctx
)
{
if
(
ctx
&&
ctx
.
cookieJar
)
delete
ctx
.
cookieJar
;
if
(
ctx
&&
ctx
.
csrf
)
delete
ctx
.
csrf
;
};
exports
.
get
=
function
(
cfg
,
path
,
context
,
getArgs
,
cb
)
{
// parse the server URL (cfg.browserid)
var
uObj
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment