post/get handler added, background section for HTTP request

This commit is contained in:
Andreas Gruß 2015-06-05 13:26:27 +02:00
parent dce917eb74
commit 153d38f10c
9 changed files with 73 additions and 122 deletions

View File

@ -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

View File

@ -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

View File

@ -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

50
features/support/http.rb Normal file
View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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
@ -21,9 +22,6 @@ Feature: POST request
| xy |
| yz |
And the query options
| post | true |
When I route I should get
| from | to | route | turns |
| a | c | ab,bc | head,left,destination |
@ -41,9 +39,6 @@ Feature: POST request
| abcd | yes |
| hgfe | yes |
And the query options
| post | true |
When I match I should get
| trace | matchings |
| dcba | hgfe |
@ -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 |
@ -82,9 +74,6 @@ Feature: POST request
| nodes |
| abc |
And the query options
| post | true |
When I request locate I should get
| in | out |
| x | a |
@ -104,9 +93,6 @@ Feature: POST request
| nodes |
| abc |
And the query options
| post | true |
When I request nearest I should get
| in | out |
| x | a |