Commit e0c83fa4 authored by MARGERY David's avatar MARGERY David
Browse files

Implement and test API* headers as in bug #8489

parent 1953176c
......@@ -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
......
......@@ -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 => {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment