]> git.openstreetmap.org Git - nominatim.git/blob - tests/steps/api_setup.py
Merge pull request #305 from lonvia/api-extratags
[nominatim.git] / tests / steps / api_setup.py
1 """ Steps for setting up and sending API requests.
2 """
3
4 from nose.tools import *
5 from lettuce import *
6 import urllib
7 import urllib2
8 import logging
9
10 logger = logging.getLogger(__name__)
11
12 def api_call(requesttype):
13     world.request_type = requesttype
14     world.json_callback = None
15     data = urllib.urlencode(world.params)
16     url = "%s/%s?%s" % (world.config.base_url, requesttype, data)
17     req = urllib2.Request(url=url, headers=world.header)
18     try:
19         fd = urllib2.urlopen(req)
20         world.page = fd.read()
21         world.returncode = 200
22     except urllib2.HTTPError, ex:
23         world.returncode = ex.code
24         world.page = None
25         return
26
27     pageinfo = fd.info()
28     assert_equal('utf-8', pageinfo.getparam('charset').lower())
29     pagetype = pageinfo.gettype()
30
31     fmt = world.params.get('format')
32     if fmt == 'html':
33         assert_equals('text/html', pagetype)
34         world.response_format = fmt
35     elif fmt == 'xml':
36         assert_equals('text/xml', pagetype)
37         world.response_format = fmt
38     elif fmt in ('json', 'jsonv2'):
39         if 'json_callback' in world.params:
40             world.json_callback = world.params['json_callback'].encode('utf8')
41             assert world.page.startswith(world.json_callback + '(')
42             assert world.page.endswith(')')
43             world.page = world.page[(len(world.json_callback)+1):-1]
44             assert_equals('application/javascript', pagetype)
45         else:
46             assert_equals('application/json', pagetype)
47         world.response_format = 'json'
48     else:
49         if requesttype == 'reverse':
50             assert_equals('text/xml', pagetype)
51             world.response_format = 'xml'
52         else:
53             assert_equals('text/html', pagetype)
54             world.response_format = 'html'
55     logger.debug("Page received (%s):" % world.response_format)
56     logger.debug(world.page)
57
58     api_setup_prepare_params(None)
59
60 @before.each_scenario
61 def api_setup_prepare_params(scenario):
62     world.results = []
63     world.params = {}
64     world.header = {}
65
66 @step(u'the request parameters$')
67 def api_setup_parameters(step):
68     """Define the parameters of the request as a hash.
69        Resets parameter list.
70     """
71     world.params = step.hashes[0]
72
73 @step(u'the HTTP header$')
74 def api_setup_parameters(step):
75     """Define additional HTTP header parameters as a hash.
76        Resets parameter list.
77     """
78     world.header = step.hashes[0]
79
80
81 @step(u'sending( \w+)? search query "([^"]*)"( with address)?')
82 def api_setup_search(step, fmt, query, doaddr):
83     world.params['q'] = query.encode('utf8')
84     if doaddr:
85         world.params['addressdetails'] = 1
86     if fmt:
87         world.params['format'] = fmt.strip()
88     api_call('search')
89
90 @step(u'sending( \w+)? structured query( with address)?$')
91 def api_setup_structured_search(step, fmt, doaddr):
92     world.params.update(step.hashes[0])
93     if doaddr:
94         world.params['addressdetails'] = 1
95     if fmt:
96         world.params['format'] = fmt.strip()
97     api_call('search')
98
99 @step(u'looking up (\w+ )?coordinates ([-\d.]+),([-\d.]+)')
100 def api_setup_reverse(step, fmt, lat, lon):
101     world.params['lat'] = lat
102     world.params['lon'] = lon
103     if fmt and fmt.strip():
104         world.params['format'] = fmt.strip()
105     api_call('reverse')
106
107 @step(u'looking up place ([NRW]?\d+)')
108 def api_setup_details_reverse(step, obj):
109     if obj[0] in ('N', 'R', 'W'):
110         # an osm id
111         world.params['osm_type']  = obj[0]
112         world.params['osm_id'] = obj[1:]
113     else:
114         world.params['place_id']  = obj
115     api_call('reverse')
116
117 @step(u'looking up details for ([NRW]?\d+)')
118 def api_setup_details(step, obj):
119     if obj[0] in ('N', 'R', 'W'):
120         # an osm id
121         world.params['osmtype']  = obj[0]
122         world.params['osmid'] = obj[1:]
123     else:
124         world.params['place_id']  = obj
125     api_call('details')
126
127 @step(u'looking up (\w+) places ((?:[a-z]\d+,*)+)')
128 def api_setup_lookup(step, fmt, ids):
129     world.params['osm_ids'] = ids
130     if fmt and fmt.strip():
131         world.params['format'] = fmt.strip()
132     api_call('lookup')
133
134 @step(u'sending an API call (\w+)')
135 def api_general_call(step, call):
136     api_call(call)