Skip to content

Commit 1b7a73d

Browse files
committed
test different status message and query parsing
1 parent ca11422 commit 1b7a73d

File tree

5 files changed

+185
-71
lines changed

5 files changed

+185
-71
lines changed

features/step_definitions/requests.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@
1919
@json['status'].class.should == Fixnum
2020
end
2121

22+
Then /^status code should be (\d+)$/ do |code|
23+
@json = JSON.parse @response.body
24+
@json['status'].should == code.to_i
25+
end
26+
27+
Then /^status message should be "(.*?)"$/ do |message|
28+
@json = JSON.parse @response.body
29+
@json['status_message'].should == message
30+
end
31+
2232
Then /^response should be a well-formed route$/ do
2333
step "response should be well-formed"
2434
@json['status_message'].class.should == String

features/step_definitions/routing.rb

Lines changed: 83 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,36 @@
33
actual = []
44
OSRMBackgroundLauncher.new("#{@osm_file}.osrm") do
55
table.hashes.each_with_index do |row,ri|
6-
waypoints = []
7-
if row['from'] and row['to']
8-
node = find_node_by_name(row['from'])
9-
raise "*** unknown from-node '#{row['from']}" unless node
10-
waypoints << node
11-
12-
node = find_node_by_name(row['to'])
13-
raise "*** unknown to-node '#{row['to']}" unless node
14-
waypoints << node
6+
if row['request']
7+
got = {'request' => row['request'] }
8+
response = request_url row['request']
9+
else
10+
params = {}
11+
waypoints = []
12+
if row['from'] and row['to']
13+
node = find_node_by_name(row['from'])
14+
raise "*** unknown from-node '#{row['from']}" unless node
15+
waypoints << node
1516

16-
got = {'from' => row['from'], 'to' => row['to'] }
17-
elsif row['waypoints']
18-
row['waypoints'].split(',').each do |n|
19-
node = find_node_by_name(n.strip)
20-
raise "*** unknown waypoint node '#{n.strip}" unless node
17+
node = find_node_by_name(row['to'])
18+
raise "*** unknown to-node '#{row['to']}" unless node
2119
waypoints << node
20+
21+
got = {'from' => row['from'], 'to' => row['to'] }
22+
response = request_route waypoints, params
23+
elsif row['waypoints']
24+
row['waypoints'].split(',').each do |n|
25+
node = find_node_by_name(n.strip)
26+
raise "*** unknown waypoint node '#{n.strip}" unless node
27+
waypoints << node
28+
end
29+
got = {'waypoints' => row['waypoints'] }
30+
response = request_route waypoints, params
31+
else
32+
raise "*** no waypoints"
2233
end
23-
got = {'waypoints' => row['waypoints'] }
24-
else
25-
raise "*** no waypoints"
2634
end
2735

28-
params = {}
2936
row.each_pair do |k,v|
3037
if k =~ /param:(.*)/
3138
if v=='(nil)'
@@ -37,9 +44,11 @@
3744
end
3845
end
3946

40-
response = request_route(waypoints, params)
41-
if response.code == "200" && response.body.empty? == false
47+
if response.body.empty? == false
4248
json = JSON.parse response.body
49+
end
50+
51+
if response.body.empty? == false
4352
if json['status'] == 0
4453
instructions = way_list json['route_instructions']
4554
bearings = bearing_list json['route_instructions']
@@ -51,61 +60,64 @@
5160
end
5261
end
5362

54-
if table.headers.include? 'start'
55-
got['start'] = instructions ? json['route_summary']['start_point'] : nil
63+
if table.headers.include? 'status'
64+
got['status'] = json['status'].to_s
65+
end
66+
if table.headers.include? 'message'
67+
got['message'] = json['status_message']
5668
end
57-
if table.headers.include? 'end'
58-
got['end'] = instructions ? json['route_summary']['end_point'] : nil
69+
if table.headers.include? '#' # comment column
70+
got['#'] = row['#'] # copy value so it always match
5971
end
60-
if table.headers.include? 'route'
61-
got['route'] = (instructions || '').strip
62-
if table.headers.include?('distance')
63-
if row['distance']!=''
64-
raise "*** Distance must be specied in meters. (ex: 250m)" unless row['distance'] =~ /\d+m/
72+
73+
if response.code == "200"
74+
if table.headers.include? 'start'
75+
got['start'] = instructions ? json['route_summary']['start_point'] : nil
76+
end
77+
if table.headers.include? 'end'
78+
got['end'] = instructions ? json['route_summary']['end_point'] : nil
79+
end
80+
if table.headers.include? 'route'
81+
got['route'] = (instructions || '').strip
82+
if table.headers.include?('distance')
83+
if row['distance']!=''
84+
raise "*** Distance must be specied in meters. (ex: 250m)" unless row['distance'] =~ /\d+m/
85+
end
86+
got['distance'] = instructions ? "#{json['route_summary']['total_distance'].to_s}m" : ''
6587
end
66-
got['distance'] = instructions ? "#{json['route_summary']['total_distance'].to_s}m" : ''
67-
end
68-
if table.headers.include?('time')
69-
raise "*** Time must be specied in seconds. (ex: 60s)" unless row['time'] =~ /\d+s/
70-
got['time'] = instructions ? "#{json['route_summary']['total_time'].to_s}s" : ''
71-
end
72-
if table.headers.include?('speed')
73-
if row['speed'] != '' && instructions
74-
raise "*** Speed must be specied in km/h. (ex: 50 km/h)" unless row['speed'] =~ /\d+ km\/h/
75-
time = json['route_summary']['total_time']
76-
distance = json['route_summary']['total_distance']
77-
speed = time>0 ? (3.6*distance/time).to_i : nil
78-
got['speed'] = "#{speed} km/h"
79-
else
80-
got['speed'] = ''
88+
if table.headers.include?('time')
89+
raise "*** Time must be specied in seconds. (ex: 60s)" unless row['time'] =~ /\d+s/
90+
got['time'] = instructions ? "#{json['route_summary']['total_time'].to_s}s" : ''
91+
end
92+
if table.headers.include?('speed')
93+
if row['speed'] != '' && instructions
94+
raise "*** Speed must be specied in km/h. (ex: 50 km/h)" unless row['speed'] =~ /\d+ km\/h/
95+
time = json['route_summary']['total_time']
96+
distance = json['route_summary']['total_distance']
97+
speed = time>0 ? (3.6*distance/time).to_i : nil
98+
got['speed'] = "#{speed} km/h"
99+
else
100+
got['speed'] = ''
101+
end
102+
end
103+
if table.headers.include? 'bearing'
104+
got['bearing'] = bearings
105+
end
106+
if table.headers.include? 'compass'
107+
got['compass'] = compasses
108+
end
109+
if table.headers.include? 'turns'
110+
got['turns'] = turns
111+
end
112+
if table.headers.include? 'modes'
113+
got['modes'] = modes
114+
end
115+
if table.headers.include? 'times'
116+
got['times'] = times
117+
end
118+
if table.headers.include? 'distances'
119+
got['distances'] = distances
81120
end
82-
end
83-
if table.headers.include? 'bearing'
84-
got['bearing'] = bearings
85-
end
86-
if table.headers.include? 'compass'
87-
got['compass'] = compasses
88-
end
89-
if table.headers.include? 'turns'
90-
got['turns'] = turns
91-
end
92-
if table.headers.include? 'modes'
93-
got['modes'] = modes
94-
end
95-
if table.headers.include? 'times'
96-
got['times'] = times
97-
end
98-
if table.headers.include? 'distances'
99-
got['distances'] = distances
100-
end
101-
if table.headers.include? 'status'
102-
got['status'] = json['status'].to_s
103-
end
104-
if table.headers.include? 'message'
105-
got['message'] = json['status_message']
106-
end
107-
if table.headers.include? '#' # comment column
108-
got['#'] = row['#'] # copy value so it always match
109121
end
110122
end
111123

features/support/route.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ def request_path path, waypoints=[], options={}
2626
raise "*** osrm-routed did not respond."
2727
end
2828

29+
def request_url path
30+
uri = URI.parse"#{HOST}/#{path}"
31+
@query = uri.to_s
32+
Timeout.timeout(OSRM_TIMEOUT) do
33+
Net::HTTP.get_response uri
34+
end
35+
rescue Errno::ECONNREFUSED => e
36+
raise "*** osrm-routed is not running."
37+
rescue Timeout::Error
38+
raise "*** osrm-routed did not respond."
39+
end
40+
2941
def request_route waypoints, params={}
3042
defaults = { 'output' => 'json', 'instructions' => true, 'alt' => false }
3143
request_path "viaroute", waypoints, defaults.merge(params)

features/testbot/query.feature

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
@routing @query
2+
Feature: Query message parsing
3+
4+
Background:
5+
Given the profile "testbot"
6+
7+
Scenario: Malformed requests
8+
Given the node locations
9+
| node | lat | lon |
10+
| a | 1.00 | 1.00 |
11+
| b | 1.01 | 1.00 |
12+
13+
And the ways
14+
| nodes |
15+
| ab |
16+
17+
When I route I should get
18+
| request | status | message |
19+
| viaroute?loc=1,1&loc=1.01,1 | 0 | Found route between points |
20+
| nonsense | 400 | Bad Request |
21+
| nonsense?loc=1,1&loc=1.01,1 | 400 | Bad Request |
22+
| | 400 | Query string malformed close to position 0 |
23+
| / | 400 | Query string malformed close to position 0 |
24+
| ? | 400 | Query string malformed close to position 0 |
25+
| viaroute/loc= | 400 | Query string malformed close to position 9 |
26+
| viaroute/loc=1 | 400 | Query string malformed close to position 9 |
27+
| viaroute/loc=1,1 | 400 | Query string malformed close to position 9 |
28+
| viaroute/loc=1,1,1 | 400 | Query string malformed close to position 9 |
29+
| viaroute/loc=x | 400 | Query string malformed close to position 9 |
30+
| viaroute/loc=x,y | 400 | Query string malformed close to position 9 |
31+
| viaroute/loc=1,1&loc= | 400 | Query string malformed close to position 9 |
32+
| viaroute/loc=1,1&loc=1 | 400 | Query string malformed close to position 9 |
33+
| viaroute/loc=1,1&loc=1,1 | 400 | Query string malformed close to position 9 |
34+
| viaroute/loc=1,1&loc=1,1,1 | 400 | Query string malformed close to position 9 |
35+
| viaroute/loc=1,1&loc=x | 400 | Query string malformed close to position 9 |
36+
| viaroute/loc=1,1&loc=x,y | 400 | Query string malformed close to position 9 |

features/testbot/status.feature

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@ Feature: Status messages
44
Background:
55
Given the profile "testbot"
66

7+
Scenario: Route found
8+
Given the node map
9+
| a | b |
10+
11+
Given the ways
12+
| nodes |
13+
| ab |
14+
15+
When I route I should get
16+
| from | to | route | status | message |
17+
| a | b | ab | 0 | Found route between points |
18+
| b | a | ab | 0 | Found route between points |
19+
720
Scenario: No route found
821
Given the node map
922
| a | b |
@@ -21,3 +34,34 @@ Feature: Status messages
2134
| c | d | cd | 0 | Found route between points |
2235
| a | c | | 207 | Cannot find route between points |
2336
| b | d | | 207 | Cannot find route between points |
37+
38+
Scenario: Malformed requests
39+
Given the node locations
40+
| node | lat | lon |
41+
| a | 1.00 | 1.00 |
42+
| b | 1.01 | 1.00 |
43+
44+
And the ways
45+
| nodes |
46+
| ab |
47+
48+
When I route I should get
49+
| request | status | message |
50+
| viaroute?loc=1,1&loc=1.01,1 | 0 | Found route between points |
51+
| nonsense | 400 | Bad Request |
52+
| nonsense?loc=1,1&loc=1.01,1 | 400 | Bad Request |
53+
| | 400 | Query string malformed close to position 0 |
54+
| / | 400 | Query string malformed close to position 0 |
55+
| ? | 400 | Query string malformed close to position 0 |
56+
| viaroute/loc= | 400 | Query string malformed close to position 9 |
57+
| viaroute/loc=1 | 400 | Query string malformed close to position 9 |
58+
| viaroute/loc=1,1 | 400 | Query string malformed close to position 9 |
59+
| viaroute/loc=1,1,1 | 400 | Query string malformed close to position 9 |
60+
| viaroute/loc=x | 400 | Query string malformed close to position 9 |
61+
| viaroute/loc=x,y | 400 | Query string malformed close to position 9 |
62+
| viaroute/loc=1,1&loc= | 400 | Query string malformed close to position 9 |
63+
| viaroute/loc=1,1&loc=1 | 400 | Query string malformed close to position 9 |
64+
| viaroute/loc=1,1&loc=1,1 | 400 | Query string malformed close to position 9 |
65+
| viaroute/loc=1,1&loc=1,1,1 | 400 | Query string malformed close to position 9 |
66+
| viaroute/loc=1,1&loc=x | 400 | Query string malformed close to position 9 |
67+
| viaroute/loc=1,1&loc=x,y | 400 | Query string malformed close to position 9 |

0 commit comments

Comments
 (0)