diff --git a/features/step_definitions/data.rb b/features/step_definitions/data.rb index f1e58bae2..524fd322c 100644 --- a/features/step_definitions/data.rb +++ b/features/step_definitions/data.rb @@ -174,3 +174,7 @@ end Given /^data is loaded with datastore$/ do @load_method = 'datastore' end + +Given /^the HTTP method "([^"]*)"$/ do |method| + @http_method = method +end diff --git a/features/step_definitions/locate.rb b/features/step_definitions/locate.rb index 4a0b4d806..190795c26 100644 --- a/features/step_definitions/locate.rb +++ b/features/step_definitions/locate.rb @@ -9,8 +9,7 @@ When /^I request locate I should get$/ do |table| out_node = find_node_by_name row['out'] raise "*** unknown out-node '#{row['out']}" unless out_node - params = @query_params - response = request_locate("#{in_node.lat},#{in_node.lon}", params) + response = request_locate("#{in_node.lat},#{in_node.lon}") if response.code == "200" && response.body.empty? == false json = JSON.parse response.body if json['status'] == 0 diff --git a/features/step_definitions/nearest.rb b/features/step_definitions/nearest.rb index 7240a2442..099f4e06f 100644 --- a/features/step_definitions/nearest.rb +++ b/features/step_definitions/nearest.rb @@ -9,8 +9,7 @@ When /^I request nearest I should get$/ do |table| out_node = find_node_by_name row['out'] raise "*** unknown out-node '#{row['out']}" unless out_node - params = @query_params - response = request_nearest("#{in_node.lat},#{in_node.lon}", params) + response = request_nearest("#{in_node.lat},#{in_node.lon}") if response.code == "200" && response.body.empty? == false json = JSON.parse response.body if json['status'] == 0 diff --git a/features/support/http.rb b/features/support/http.rb new file mode 100644 index 000000000..2233f8286 --- /dev/null +++ b/features/support/http.rb @@ -0,0 +1,50 @@ +require 'net/http' + +def generate_request_url path + if @http_method.eql? "POST" + pos = path.index('?') - 1 + service = path[0..pos] + uri = URI.parse "#{HOST}/#{service}" + else + uri = URI.parse "#{HOST}/#{path}" + end +end + +def send_simple_request uri, path + Timeout.timeout(OSRM_TIMEOUT) do + if @http_method.eql? "POST" + pos = path.index('=') + 1 + path.slice!(0, pos) + response = Net::HTTP.post_form uri, "loc" => path + else + response = Net::HTTP.get_response uri + end + end +rescue Errno::ECONNREFUSED => e + raise "*** osrm-routed is not running." +rescue Timeout::Error + raise "*** osrm-routed did not respond." +end + +def send_request uri, waypoints, timestamps, options + @query = uri.to_s + Timeout.timeout(OSRM_TIMEOUT) do + if @http_method.eql? "POST" + datas = {} + if waypoints.length > 0 + datas[:loc] = waypoints.compact.map { |w| "#{w.lat},#{w.lon}" } + end + if timestamps.length > 0 + datas[:t] = timestamps.compact.map { |t| "#{t}" } + end + datas.merge! options + response = Net::HTTP.post_form uri, datas + else + response = Net::HTTP.get_response uri + end + end +rescue Errno::ECONNREFUSED => e + raise "*** osrm-routed is not running." +rescue Timeout::Error + raise "*** osrm-routed did not respond." +end diff --git a/features/support/locate.rb b/features/support/locate.rb index e8406c9ac..586d0b393 100644 --- a/features/support/locate.rb +++ b/features/support/locate.rb @@ -1,32 +1,12 @@ require 'net/http' -def request_locate_url path, method={} +def request_locate_url path @query = path - if method.has_key?("post") - request_method = "POST" - else - request_method = "GET" - end - if request_method.eql? "GET" - uri = URI.parse "#{HOST}/#{path}" - elsif request_method.eql? "POST" - uri = URI.parse "#{HOST}/locate" - end - Timeout.timeout(OSRM_TIMEOUT) do - if request_method.eql? "GET" - Net::HTTP.get_response uri - elsif request_method.eql? "POST" - path.slice!(0, 11) - Net::HTTP.post_form uri, "loc" => path - end - end -rescue Errno::ECONNREFUSED => e - raise "*** osrm-routed is not running." -rescue Timeout::Error - raise "*** osrm-routed did not respond." + uri = generate_request_url path + response = send_simple_request uri, path end -def request_locate a, method - request_locate_url "locate?loc=#{a}", method +def request_locate a + request_locate_url "locate?loc=#{a}" end diff --git a/features/support/match.rb b/features/support/match.rb index 3decd2428..2f75f08d8 100644 --- a/features/support/match.rb +++ b/features/support/match.rb @@ -14,34 +14,7 @@ def request_matching trace=[], timestamps=[], options={} params = (trace_params + defaults.merge(options).to_param).join('&') params = nil if params=="" - if options.has_key?("post") - request_method = "POST" - options.delete("post") - else - request_method = "GET" - end - if request_method.eql? "GET" - uri = URI.parse ["#{HOST}/match", params].compact.join('?') - elsif request_method.eql? "POST" - uri = URI.parse "#{HOST}/match" - end - @query = uri.to_s - Timeout.timeout(OSRM_TIMEOUT) do - if request_method.eql? "GET" - Net::HTTP.get_response uri - elsif request_method.eql? "POST" - datas = {} - datas[:loc] = trace.compact.map { |w| "#{w.lat},#{w.lon}" } - if ts.length > 0 - datas[:t] = timestamps.compact.map { |t| "#{t}" } - end - datas.merge! options - Net::HTTP.post_form uri, datas - end - end -rescue Errno::ECONNREFUSED => e - raise "*** osrm-routed is not running." -rescue Timeout::Error - raise "*** osrm-routed did not respond." + uri = generate_request_url ("match" + '?' + params) + response = send_request uri, trace, timestamps, options end diff --git a/features/support/nearest.rb b/features/support/nearest.rb index 10e0c76ca..f76ddc9f6 100644 --- a/features/support/nearest.rb +++ b/features/support/nearest.rb @@ -1,32 +1,12 @@ require 'net/http' -def request_nearest_url path, method={} +def request_nearest_url path @query = path - if method.has_key?("post") - request_method = "POST" - else - request_method = "GET" - end - if request_method.eql? "GET" - uri = URI.parse "#{HOST}/#{path}" - elsif request_method.eql? "POST" - uri = URI.parse "#{HOST}/nearest" - end - Timeout.timeout(OSRM_TIMEOUT) do - if request_method.eql? "GET" - Net::HTTP.get_response uri - elsif request_method.eql? "POST" - path.slice!(0, 12) - Net::HTTP.post_form uri, "loc" => path - end - end -rescue Errno::ECONNREFUSED => e - raise "*** osrm-routed is not running." -rescue Timeout::Error - raise "*** osrm-routed did not respond." + uri = generate_request_url path + response = send_simple_request uri, path end -def request_nearest a, method - request_nearest_url "nearest?loc=#{a}", method +def request_nearest a + request_nearest_url "nearest?loc=#{a}" end diff --git a/features/support/route.rb b/features/support/route.rb index 615a1f880..f7c14e9ff 100644 --- a/features/support/route.rb +++ b/features/support/route.rb @@ -16,32 +16,12 @@ def request_path path, waypoints=[], options={} params = (locs + options.to_param).join('&') params = nil if params=="" - if options.has_key?("post") - request_method = "POST" - options.delete("post") + if params == nil + uri = generate_request_url (path) else - request_method = "GET" + uri = generate_request_url (path + '?' + params) end - if request_method.eql? "GET" - uri = URI.parse ["#{HOST}/#{path}", params].compact.join('?') - elsif request_method.eql? "POST" - uri = URI.parse "#{HOST}/#{path}" - end - @query = uri.to_s - Timeout.timeout(OSRM_TIMEOUT) do - if request_method.eql? "GET" - Net::HTTP.get_response uri - elsif request_method.eql? "POST" - datas = {} - datas[:loc] = waypoints.compact.map { |w| "#{w.lat},#{w.lon}" } - datas.merge! options - Net::HTTP.post_form uri, datas - end - end -rescue Errno::ECONNREFUSED => e - raise "*** osrm-routed is not running." -rescue Timeout::Error - raise "*** osrm-routed did not respond." + response = send_request uri, waypoints, {}, options end def request_url path diff --git a/features/testbot/post.feature b/features/testbot/post.feature index c2d07c242..ff2282406 100644 --- a/features/testbot/post.feature +++ b/features/testbot/post.feature @@ -3,6 +3,7 @@ Feature: POST request Background: Given the profile "testbot" + And the HTTP method "POST" Scenario: Testbot - viaroute POST request Given the node locations @@ -20,9 +21,6 @@ Feature: POST request | bc | | xy | | yz | - - And the query options - | post | true | When I route I should get | from | to | route | turns | @@ -40,9 +38,6 @@ Feature: POST request | nodes | oneway | | abcd | yes | | hgfe | yes | - - And the query options - | post | true | When I match I should get | trace | matchings | @@ -59,9 +54,6 @@ Feature: POST request | xa | | | by | | - And the query options - | post | true | - When I request a travel time matrix I should get | | x | y | d | e | | x | 0 | 300 | 400 | 300 | @@ -81,9 +73,6 @@ Feature: POST request And the ways | nodes | | abc | - - And the query options - | post | true | When I request locate I should get | in | out | @@ -103,9 +92,6 @@ Feature: POST request And the ways | nodes | | abc | - - And the query options - | post | true | When I request nearest I should get | in | out |