Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
grid5000
g5k-api
Commits
e0c83fa4
Commit
e0c83fa4
authored
Aug 30, 2017
by
MARGERY David
Browse files
Implement and test API* headers as in bug #8489
parent
1953176c
Changes
2
Hide whitespace changes
Inline
Side-by-side
lib/grid5000/router.rb
View file @
e0c83fa4
...
...
@@ -29,6 +29,12 @@ module Grid5000
class
<<
self
def
uri_to
(
request
,
path
,
in_or_out
=
:in
,
relative_or_absolute
=
:relative
)
root_path
=
if
request
.
env
[
'HTTP_X_API_ROOT_PATH'
].
blank?
nil
else
File
.
join
(
"/"
,
(
request
.
env
[
'HTTP_X_API_ROOT_PATH'
]
||
""
))
end
# root_path = if request.env['HTTP_X_API_ROOT_PATH'].blank?
api_version
=
if
request
.
env
[
'HTTP_X_API_VERSION'
].
blank?
nil
else
...
...
@@ -47,8 +53,9 @@ module Grid5000
File
.
join
(
"/"
,
(
request
.
env
[
'HTTP_X_API_MOUNT_PATH'
]
||
""
))
end
# mount_path = if request.env['HTTP_X_API_MOUNT_PATH'].blank?
uri
=
File
.
join
(
"/"
,
*
[
api_version
,
path_prefix
,
path
].
compact
)
uri
.
gsub!
(
mount_path
,
''
)
unless
mount_path
.
nil?
mounted_path
=
path
mounted_path
.
gsub!
(
/^
#{
mount_path
}
/
,
''
)
unless
mount_path
.
nil?
uri
=
File
.
join
(
"/"
,
*
[
root_path
,
api_version
,
path_prefix
,
mounted_path
].
compact
)
uri
=
"/"
if
uri
.
blank?
# abasu / dmargery - bug ref 7360 - for correct URI construction
if
in_or_out
==
:out
||
relative_or_absolute
==
:absolute
...
...
spec/lib/grid5000/router_spec.rb
View file @
e0c83fa4
...
...
@@ -33,6 +33,28 @@ describe Grid5000::Router do
Grid5000
::
Router
.
uri_to
(
request
,
"/sites/rennes/jobs"
).
should
==
"/grid5000/sites/rennes/jobs"
end
it
"should take into account X-Api-Root-Path header"
do
request
=
double
(
Rack
::
MockRequest
,
:env
=>
{
'HTTP_X_API_ROOT_PATH'
=>
'proxies/grid5000'
})
Grid5000
::
Router
.
uri_to
(
request
,
"/sites/rennes/jobs"
).
should
==
"/proxies/grid5000/sites/rennes/jobs"
end
it
"should take into account X-Api-Mount-Path header"
do
request
=
double
(
Rack
::
MockRequest
,
:env
=>
{
'HTTP_X_API_MOUNT_PATH'
=>
'sites/rennes'
})
Grid5000
::
Router
.
uri_to
(
request
,
"/sites/rennes/jobs"
).
should
==
"/jobs"
end
it
"should only substitute X-Api-Mount-Path header at the start of url"
do
request
=
double
(
Rack
::
MockRequest
,
:env
=>
{
'HTTP_X_API_MOUNT_PATH'
=>
'/rennes'
})
Grid5000
::
Router
.
uri_to
(
request
,
"/sites/rennes/jobs"
).
should
==
"/sites/rennes/jobs"
end
it
"should take into account both X-Api-Version and X-Api-Path-Prefix headers"
do
request
=
double
(
Rack
::
MockRequest
,
:env
=>
{
'HTTP_X_API_VERSION'
=>
'sid'
,
...
...
@@ -41,6 +63,34 @@ describe Grid5000::Router do
Grid5000
::
Router
.
uri_to
(
request
,
"/sites/rennes/jobs"
).
should
==
"/sid/grid5000/sites/rennes/jobs"
end
it
"Should properly combine X-API-[Mount-Path,Version,Path-Prefix] headers"
do
request
=
double
(
Rack
::
MockRequest
,
:env
=>
{
'HTTP_X_API_MOUNT_PATH'
=>
'/sites/rennes/'
,
'HTTP_X_API_VERSION'
=>
'sid'
,
'HTTP_X_API_PATH_PREFIX'
=>
'g5k-api'
})
Grid5000
::
Router
.
uri_to
(
request
,
"/sites/rennes/jobs"
).
should
==
"/sid/g5k-api/jobs"
end
it
"Should properly combine X-API-[Root-Path,Version,Path-Prefix] headers"
do
request
=
double
(
Rack
::
MockRequest
,
:env
=>
{
'HTTP_X_API_ROOT_PATH'
=>
'proxies/grid5000'
,
'HTTP_X_API_VERSION'
=>
'sid'
,
'HTTP_X_API_PATH_PREFIX'
=>
'g5k-api'
})
Grid5000
::
Router
.
uri_to
(
request
,
"/sites/rennes/jobs"
).
should
==
"/proxies/grid5000/sid/g5k-api/sites/rennes/jobs"
end
it
"Should properly combine all X-API headers supported"
do
request
=
double
(
Rack
::
MockRequest
,
:env
=>
{
'HTTP_X_API_ROOT_PATH'
=>
'sites/fr/grid5000'
,
'HTTP_X_API_MOUNT_PATH'
=>
'/sites/'
,
'HTTP_X_API_VERSION'
=>
'sid'
,
'HTTP_X_API_PATH_PREFIX'
=>
'g5k-api'
})
Grid5000
::
Router
.
uri_to
(
request
,
"/sites/rennes/jobs"
).
should
==
"/sites/fr/grid5000/sid/g5k-api/rennes/jobs"
end
it
"should take into account the parameters of the config file with empty path"
do
Rails
.
my_config
(
"base_uri_out"
.
to_sym
).
should
==
"http://api-out.local"
request
=
double
(
Rack
::
MockRequest
,
:env
=>
{
...
...
Write
Preview
Supports
Markdown
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