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