From 6a08d93e2c9993b87fc43c1bc2d7ea19f845fab3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Gru=C3=9F?= Date: Wed, 27 May 2015 15:40:10 +0200 Subject: [PATCH 1/8] http post requests implemented --- server/request_parser.cpp | 50 +++++++++++++++++++++++++++++++++++++-- server/request_parser.hpp | 7 +++++- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/server/request_parser.cpp b/server/request_parser.cpp index 584dcbeeb..d57dd305b 100644 --- a/server/request_parser.cpp +++ b/server/request_parser.cpp @@ -42,7 +42,7 @@ namespace http RequestParser::RequestParser() : state(internal_state::method_start), current_header({"", ""}), - selected_compression(no_compression) + selected_compression(no_compression), is_post_header(false) { } @@ -58,6 +58,11 @@ RequestParser::parse(request ¤t_request, char *begin, char *end) } } osrm::tribool result = osrm::tribool::indeterminate; + + if(is_post_header && (begin == end)) + { + result = osrm::tribool::yes; + } return std::make_tuple(result, selected_compression); } @@ -70,8 +75,38 @@ osrm::tribool RequestParser::consume(request ¤t_request, const char input) { return osrm::tribool::no; } + if(input == 'P') + { + state = internal_state::post_O; + return osrm::tribool::indeterminate; + } state = internal_state::method; return osrm::tribool::indeterminate; + case internal_state::post_O: + if(input == 'O') + { + state = internal_state::post_S; + return osrm::tribool::indeterminate; + } + return osrm::tribool::no; + case internal_state::post_S: + if(input == 'S') + { + state = internal_state::post_T; + return osrm::tribool::indeterminate; + } + return osrm::tribool::no; + case internal_state::post_T: + if(input == 'T') + { + is_post_header = true; + state = internal_state::method; + return osrm::tribool::indeterminate; + } + return osrm::tribool::no; + case internal_state::post_request: + current_request.uri.push_back(input); + return osrm::tribool::indeterminate; case internal_state::method: if (input == ' ') { @@ -272,7 +307,18 @@ osrm::tribool RequestParser::consume(request ¤t_request, const char input) return osrm::tribool::indeterminate; } return osrm::tribool::no; - default: // expecting_newline_3 + case internal_state::expecting_newline_3: + if(input == '\n') + { + if(is_post_header) + { + state = internal_state::post_request; + return osrm::tribool::indeterminate; + } + return osrm::tribool::yes; + } + return osrm::tribool::no; + default: // should never be reached return input == '\n' ? osrm::tribool::yes : osrm::tribool::no; } } diff --git a/server/request_parser.hpp b/server/request_parser.hpp index 2b6bf6944..86b08c4cc 100644 --- a/server/request_parser.hpp +++ b/server/request_parser.hpp @@ -80,11 +80,16 @@ class RequestParser space_before_header_value, header_value, expecting_newline_2, - expecting_newline_3 + expecting_newline_3, + post_O, + post_S, + post_T, + post_request } state; header current_header; compression_type selected_compression; + bool is_post_header; }; } // namespace http From a87d89302fb412f4b64a1e77a91fa8cecc70fbfd Mon Sep 17 00:00:00 2001 From: Daniel Patterson Date: Sun, 31 May 2015 09:50:46 -0700 Subject: [PATCH 2/8] Handle POST request when spanning multiple packets --- server/request_parser.cpp | 18 ++++++++++++++++-- server/request_parser.hpp | 1 + 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/server/request_parser.cpp b/server/request_parser.cpp index d57dd305b..07cd52d83 100644 --- a/server/request_parser.cpp +++ b/server/request_parser.cpp @@ -42,7 +42,8 @@ namespace http RequestParser::RequestParser() : state(internal_state::method_start), current_header({"", ""}), - selected_compression(no_compression), is_post_header(false) + selected_compression(no_compression), is_post_header(false), + content_length(0) { } @@ -59,7 +60,7 @@ RequestParser::parse(request ¤t_request, char *begin, char *end) } osrm::tribool result = osrm::tribool::indeterminate; - if(is_post_header && (begin == end)) + if(is_post_header && content_length == 0) { result = osrm::tribool::yes; } @@ -106,6 +107,7 @@ osrm::tribool RequestParser::consume(request ¤t_request, const char input) return osrm::tribool::no; case internal_state::post_request: current_request.uri.push_back(input); + --content_length; return osrm::tribool::indeterminate; case internal_state::method: if (input == ' ') @@ -239,6 +241,17 @@ osrm::tribool RequestParser::consume(request ¤t_request, const char input) { current_request.agent = current_header.value; } + if (boost::iequals(current_header.name, "Content-Length")) + { + try + { + content_length = std::stoi(current_header.value); + } + catch (const std::exception &e) + { + // Ignore the header if the parameter isn't an int + } + } if (input == '\r') { @@ -312,6 +325,7 @@ osrm::tribool RequestParser::consume(request ¤t_request, const char input) { if(is_post_header) { + current_request.uri.push_back('?'); state = internal_state::post_request; return osrm::tribool::indeterminate; } diff --git a/server/request_parser.hpp b/server/request_parser.hpp index 86b08c4cc..3724613a7 100644 --- a/server/request_parser.hpp +++ b/server/request_parser.hpp @@ -90,6 +90,7 @@ class RequestParser header current_header; compression_type selected_compression; bool is_post_header; + int content_length; }; } // namespace http From daa6d02887f3cb79ad53d093b2034d146f986f09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Gru=C3=9F?= Date: Mon, 1 Jun 2015 09:42:22 +0200 Subject: [PATCH 3/8] Content Type validation added --- server/request_parser.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/server/request_parser.cpp b/server/request_parser.cpp index 07cd52d83..502e6e0b8 100644 --- a/server/request_parser.cpp +++ b/server/request_parser.cpp @@ -60,7 +60,7 @@ RequestParser::parse(request ¤t_request, char *begin, char *end) } osrm::tribool result = osrm::tribool::indeterminate; - if(is_post_header && content_length == 0) + if(state == internal_state::post_request && content_length <= 0) { result = osrm::tribool::yes; } @@ -252,6 +252,13 @@ osrm::tribool RequestParser::consume(request ¤t_request, const char input) // Ignore the header if the parameter isn't an int } } + if (boost::iequals(current_header.name, "Content-Type")) + { + if (!boost::icontains(current_header.value, "application/x-www-form-urlencoded")) + { + return osrm::tribool::no; + } + } if (input == '\r') { From eb711787aef5d2629ea76c2bbaa3d2e939fc37c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Gru=C3=9F?= Date: Wed, 3 Jun 2015 15:31:20 +0200 Subject: [PATCH 4/8] tests added --- features/step_definitions/post.rb | 44 +++++++++++++++++++++++++++++++ features/support/post.rb | 23 ++++++++++++++++ features/testbot/post.feature | 23 ++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 features/step_definitions/post.rb create mode 100644 features/support/post.rb create mode 100644 features/testbot/post.feature diff --git a/features/step_definitions/post.rb b/features/step_definitions/post.rb new file mode 100644 index 000000000..20fe143c4 --- /dev/null +++ b/features/step_definitions/post.rb @@ -0,0 +1,44 @@ +When /^I request post I should get$/ do |table| + reprocess + actual = [] + OSRMLoader.load(self,"#{prepared_file}.osrm") do + table.hashes.each_with_index do |row,ri| + request_string = row['request'].split("?") + got = {'request' => row['request'] } + response = request_post_url request_string[0], request_string[1] + + row.each_pair do |k,v| + if k =~ /param:(.*)/ + if v=='(nil)' + params[$1]=nil + elsif v!=nil + params[$1]=v + end + got[k]=v + end + end + + if table.headers.include? 'status_code' + # the only thing we want to test is + # an accepted request + got['status_code'] = response.code.to_s + end + + ok = true + row.keys.each do |key| + if FuzzyMatch.match got[key], row[key] + got[key] = row[key] + else + ok = false + end + end + + unless ok + log_fail row,got, { 'route' => {:query => @query, :response => response} } + end + + actual << got + end + end + table.diff! actual +end \ No newline at end of file diff --git a/features/support/post.rb b/features/support/post.rb new file mode 100644 index 000000000..bc532a870 --- /dev/null +++ b/features/support/post.rb @@ -0,0 +1,23 @@ +require 'net/http' + +HOST = "http://127.0.0.1:#{OSRM_PORT}" + +def request_post_url service, param_string + uri = URI.parse"#{HOST}/#{service}" + @query = uri.to_s + Timeout.timeout(OSRM_TIMEOUT) do + params = {} + values = param_string.split("loc=") + locs = [] + values.each do |value| + locs << "#{value}".gsub(/[&]/, '') + end + locs.reject! { |c| c.empty? } + params.merge!(loc: locs) + Net::HTTP.post_form uri, params + 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/testbot/post.feature b/features/testbot/post.feature new file mode 100644 index 000000000..ce9ca5036 --- /dev/null +++ b/features/testbot/post.feature @@ -0,0 +1,23 @@ +@post @testbot +Feature: POST request + + Background: + Given the profile "testbot" + + Scenario: Accept POST Request + Given the node locations + | node | lat | lon | + | a | 1.00 | 1.00 | + | b | 1.01 | 1.00 | + + And the ways + | nodes | + | ab | + + When I request post I should get + | request | status_code | + | locate?loc=1.0,1.0 | 200 | + | nearest?loc=1.0,1.0 | 200 | + | viaroute?loc=1,1&loc=1.01,1 | 200 | + | match?loc=1,1&loc=1.01,1 | 200 | + | table?loc=1,1&loc=1.01,1 | 200 | From dce917eb74c6b3fd205193077a57a048c29b057f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Gru=C3=9F?= Date: Thu, 4 Jun 2015 17:39:54 +0200 Subject: [PATCH 5/8] post tests via query options available --- features/step_definitions/locate.rb | 3 +- features/step_definitions/nearest.rb | 3 +- features/step_definitions/post.rb | 44 ----------- features/support/locate.rb | 25 ++++-- features/support/match.rb | 25 +++++- features/support/nearest.rb | 25 ++++-- features/support/post.rb | 23 ------ features/support/route.rb | 22 +++++- features/testbot/post.feature | 113 ++++++++++++++++++++++++--- 9 files changed, 189 insertions(+), 94 deletions(-) delete mode 100644 features/step_definitions/post.rb delete mode 100644 features/support/post.rb diff --git a/features/step_definitions/locate.rb b/features/step_definitions/locate.rb index 190795c26..4a0b4d806 100644 --- a/features/step_definitions/locate.rb +++ b/features/step_definitions/locate.rb @@ -9,7 +9,8 @@ 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 - response = request_locate("#{in_node.lat},#{in_node.lon}") + params = @query_params + response = request_locate("#{in_node.lat},#{in_node.lon}", params) 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 099f4e06f..7240a2442 100644 --- a/features/step_definitions/nearest.rb +++ b/features/step_definitions/nearest.rb @@ -9,7 +9,8 @@ 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 - response = request_nearest("#{in_node.lat},#{in_node.lon}") + params = @query_params + response = request_nearest("#{in_node.lat},#{in_node.lon}", params) if response.code == "200" && response.body.empty? == false json = JSON.parse response.body if json['status'] == 0 diff --git a/features/step_definitions/post.rb b/features/step_definitions/post.rb deleted file mode 100644 index 20fe143c4..000000000 --- a/features/step_definitions/post.rb +++ /dev/null @@ -1,44 +0,0 @@ -When /^I request post I should get$/ do |table| - reprocess - actual = [] - OSRMLoader.load(self,"#{prepared_file}.osrm") do - table.hashes.each_with_index do |row,ri| - request_string = row['request'].split("?") - got = {'request' => row['request'] } - response = request_post_url request_string[0], request_string[1] - - row.each_pair do |k,v| - if k =~ /param:(.*)/ - if v=='(nil)' - params[$1]=nil - elsif v!=nil - params[$1]=v - end - got[k]=v - end - end - - if table.headers.include? 'status_code' - # the only thing we want to test is - # an accepted request - got['status_code'] = response.code.to_s - end - - ok = true - row.keys.each do |key| - if FuzzyMatch.match got[key], row[key] - got[key] = row[key] - else - ok = false - end - end - - unless ok - log_fail row,got, { 'route' => {:query => @query, :response => response} } - end - - actual << got - end - end - table.diff! actual -end \ No newline at end of file diff --git a/features/support/locate.rb b/features/support/locate.rb index 900724703..e8406c9ac 100644 --- a/features/support/locate.rb +++ b/features/support/locate.rb @@ -1,10 +1,25 @@ require 'net/http' -def request_locate_url path +def request_locate_url path, method={} @query = path - uri = URI.parse "#{HOST}/#{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 - Net::HTTP.get_response uri + 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." @@ -12,6 +27,6 @@ rescue Timeout::Error raise "*** osrm-routed did not respond." end -def request_locate a - request_locate_url "locate?loc=#{a}" +def request_locate a, method + request_locate_url "locate?loc=#{a}", method end diff --git a/features/support/match.rb b/features/support/match.rb index bf51189a4..3decd2428 100644 --- a/features/support/match.rb +++ b/features/support/match.rb @@ -13,10 +13,31 @@ def request_matching trace=[], timestamps=[], options={} end params = (trace_params + defaults.merge(options).to_param).join('&') params = nil if params=="" - uri = URI.parse ["#{HOST}/match", params].compact.join('?') + + 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 - Net::HTTP.get_response uri + 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." diff --git a/features/support/nearest.rb b/features/support/nearest.rb index 77fc351a9..10e0c76ca 100644 --- a/features/support/nearest.rb +++ b/features/support/nearest.rb @@ -1,10 +1,25 @@ require 'net/http' -def request_nearest_url path +def request_nearest_url path, method={} @query = path - uri = URI.parse "#{HOST}/#{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 - Net::HTTP.get_response uri + 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." @@ -12,6 +27,6 @@ rescue Timeout::Error raise "*** osrm-routed did not respond." end -def request_nearest a - request_nearest_url "nearest?loc=#{a}" +def request_nearest a, method + request_nearest_url "nearest?loc=#{a}", method end diff --git a/features/support/post.rb b/features/support/post.rb deleted file mode 100644 index bc532a870..000000000 --- a/features/support/post.rb +++ /dev/null @@ -1,23 +0,0 @@ -require 'net/http' - -HOST = "http://127.0.0.1:#{OSRM_PORT}" - -def request_post_url service, param_string - uri = URI.parse"#{HOST}/#{service}" - @query = uri.to_s - Timeout.timeout(OSRM_TIMEOUT) do - params = {} - values = param_string.split("loc=") - locs = [] - values.each do |value| - locs << "#{value}".gsub(/[&]/, '') - end - locs.reject! { |c| c.empty? } - params.merge!(loc: locs) - Net::HTTP.post_form uri, params - 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/route.rb b/features/support/route.rb index a8c78227e..615a1f880 100644 --- a/features/support/route.rb +++ b/features/support/route.rb @@ -15,10 +15,28 @@ def request_path path, waypoints=[], options={} locs = waypoints.compact.map { |w| "loc=#{w.lat},#{w.lon}" } params = (locs + options.to_param).join('&') params = nil if params=="" - uri = URI.parse ["#{HOST}/#{path}", params].compact.join('?') + + 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}/#{path}", params].compact.join('?') + elsif request_method.eql? "POST" + uri = URI.parse "#{HOST}/#{path}" + end @query = uri.to_s Timeout.timeout(OSRM_TIMEOUT) do - Net::HTTP.get_response uri + 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." diff --git a/features/testbot/post.feature b/features/testbot/post.feature index ce9ca5036..c2d07c242 100644 --- a/features/testbot/post.feature +++ b/features/testbot/post.feature @@ -4,20 +4,111 @@ Feature: POST request Background: Given the profile "testbot" - Scenario: Accept POST Request + Scenario: Testbot - viaroute POST request Given the node locations - | node | lat | lon | - | a | 1.00 | 1.00 | - | b | 1.01 | 1.00 | + | node | lat | lon | + | a | 55.68740 | 12.52430 | + | b | 55.68745 | 12.52409 | + | c | 55.68711 | 12.52383 | + | x | -55.68740 | 12.52430 | + | y | -55.68745 | 12.52409 | + | z | -55.68711 | 12.52383 | And the ways | nodes | | ab | + | bc | + | xy | + | yz | - When I request post I should get - | request | status_code | - | locate?loc=1.0,1.0 | 200 | - | nearest?loc=1.0,1.0 | 200 | - | viaroute?loc=1,1&loc=1.01,1 | 200 | - | match?loc=1,1&loc=1.01,1 | 200 | - | table?loc=1,1&loc=1.01,1 | 200 | + And the query options + | post | true | + + When I route I should get + | from | to | route | turns | + | a | c | ab,bc | head,left,destination | + | c | a | bc,ab | head,right,destination | + | x | z | xy,yz | head,right,destination | + | z | x | yz,xy | head,left,destination | + + Scenario: Testbot - match POST request + Given the node map + | a | b | c | d | + | e | f | g | h | + + And the ways + | nodes | oneway | + | abcd | yes | + | hgfe | yes | + + And the query options + | post | true | + + When I match I should get + | trace | matchings | + | dcba | hgfe | + + Scenario: Testbot - table POST request + Given the node map + | x | a | b | y | + | | d | e | | + + And the ways + | nodes | oneway | + | abeda | yes | + | 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 | + | y | 500 | 0 | 300 | 200 | + | d | 200 | 300 | 0 | 300 | + | e | 300 | 400 | 100 | 0 | + + Scenario: Testbot - locate POST request + Given the node locations + | node | lat | lon | + | a | -85 | -180 | + | b | 0 | 0 | + | c | 85 | 180 | + | x | -84 | -180 | + | y | 84 | 180 | + + And the ways + | nodes | + | abc | + + And the query options + | post | true | + + When I request locate I should get + | in | out | + | x | a | + | y | c | + + Scenario: Testbot - nearest POST request + Given the node locations + | node | lat | lon | + | a | -85 | -180 | + | b | -85 | -160 | + | c | -85 | -140 | + | x | -84.999 | -180 | + | y | -84.999 | -160 | + | z | -84.999 | -140 | + + And the ways + | nodes | + | abc | + + And the query options + | post | true | + + When I request nearest I should get + | in | out | + | x | a | + | y | b | + | z | c | From 153d38f10c1fbb87221d42766dc49a8f1a2f7986 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Gru=C3=9F?= Date: Fri, 5 Jun 2015 13:26:27 +0200 Subject: [PATCH 6/8] post/get handler added, background section for HTTP request --- features/step_definitions/data.rb | 4 +++ features/step_definitions/locate.rb | 3 +- features/step_definitions/nearest.rb | 3 +- features/support/http.rb | 50 ++++++++++++++++++++++++++++ features/support/locate.rb | 30 +++-------------- features/support/match.rb | 31 ++--------------- features/support/nearest.rb | 30 +++-------------- features/support/route.rb | 28 +++------------- features/testbot/post.feature | 16 +-------- 9 files changed, 73 insertions(+), 122 deletions(-) create mode 100644 features/support/http.rb 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 | From b406844c96b8b381ec85ec70062e347af6b53089 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Gru=C3=9F?= Date: Sun, 7 Jun 2015 11:02:24 +0200 Subject: [PATCH 7/8] rearranged send_request parameters --- features/support/http.rb | 2 +- features/support/match.rb | 2 +- features/support/route.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/features/support/http.rb b/features/support/http.rb index 2233f8286..2eb298547 100644 --- a/features/support/http.rb +++ b/features/support/http.rb @@ -26,7 +26,7 @@ rescue Timeout::Error raise "*** osrm-routed did not respond." end -def send_request uri, waypoints, timestamps, options +def send_request uri, waypoints=[], options={}, timestamps=[] @query = uri.to_s Timeout.timeout(OSRM_TIMEOUT) do if @http_method.eql? "POST" diff --git a/features/support/match.rb b/features/support/match.rb index 2f75f08d8..8bcf7a2c6 100644 --- a/features/support/match.rb +++ b/features/support/match.rb @@ -15,6 +15,6 @@ def request_matching trace=[], timestamps=[], options={} params = nil if params=="" uri = generate_request_url ("match" + '?' + params) - response = send_request uri, trace, timestamps, options + response = send_request uri, trace, options, timestamps end diff --git a/features/support/route.rb b/features/support/route.rb index f7c14e9ff..ca353d6e6 100644 --- a/features/support/route.rb +++ b/features/support/route.rb @@ -21,7 +21,7 @@ def request_path path, waypoints=[], options={} else uri = generate_request_url (path + '?' + params) end - response = send_request uri, waypoints, {}, options + response = send_request uri, waypoints, options end def request_url path From d726ce6340e79807d1d3f1861979f42c3bf9bea4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Gru=C3=9F?= Date: Sun, 7 Jun 2015 12:20:03 +0200 Subject: [PATCH 8/8] removed send_simple_request --- features/step_definitions/locate.rb | 2 +- features/step_definitions/nearest.rb | 2 +- features/support/http.rb | 16 ---------------- features/support/locate.rb | 10 +++++----- features/support/nearest.rb | 8 ++++---- 5 files changed, 11 insertions(+), 27 deletions(-) diff --git a/features/step_definitions/locate.rb b/features/step_definitions/locate.rb index 190795c26..2fd8e627b 100644 --- a/features/step_definitions/locate.rb +++ b/features/step_definitions/locate.rb @@ -9,7 +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 - response = request_locate("#{in_node.lat},#{in_node.lon}") + response = request_locate(in_node) 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 099f4e06f..481c74167 100644 --- a/features/step_definitions/nearest.rb +++ b/features/step_definitions/nearest.rb @@ -9,7 +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 - response = request_nearest("#{in_node.lat},#{in_node.lon}") + response = request_nearest(in_node) 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 index 2eb298547..80dad8a85 100644 --- a/features/support/http.rb +++ b/features/support/http.rb @@ -10,22 +10,6 @@ def generate_request_url 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=[], options={}, timestamps=[] @query = uri.to_s Timeout.timeout(OSRM_TIMEOUT) do diff --git a/features/support/locate.rb b/features/support/locate.rb index 586d0b393..a62e53aa9 100644 --- a/features/support/locate.rb +++ b/features/support/locate.rb @@ -1,12 +1,12 @@ require 'net/http' -def request_locate_url path +def request_locate_url path, node @query = path - + uri = generate_request_url path - response = send_simple_request uri, path + response = send_request uri, [node] end -def request_locate a - request_locate_url "locate?loc=#{a}" +def request_locate node + request_locate_url "locate?loc=#{node.lat},#{node.lon}", node end diff --git a/features/support/nearest.rb b/features/support/nearest.rb index f76ddc9f6..af52b49e0 100644 --- a/features/support/nearest.rb +++ b/features/support/nearest.rb @@ -1,12 +1,12 @@ require 'net/http' -def request_nearest_url path +def request_nearest_url path, node @query = path uri = generate_request_url path - response = send_simple_request uri, path + response = send_request uri, [node] end -def request_nearest a - request_nearest_url "nearest?loc=#{a}" +def request_nearest node + request_nearest_url "nearest?loc=#{node.lat},#{node.lon}", node end