1 """ Steps for setting up and sending API requests.
4 from nose.tools import *
10 logger = logging.getLogger(__name__)
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)
18 fd = urllib2.urlopen(req)
19 world.page = fd.read()
20 world.returncode = 200
21 except urllib2.HTTPError, ex:
22 world.returncode = ex.code
27 assert_equal('utf-8', pageinfo.getparam('charset').lower())
28 pagetype = pageinfo.gettype()
30 fmt = world.params.get('format')
32 assert_equals('text/html', pagetype)
33 world.response_format = fmt
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'].encode('utf8')
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)
45 assert_equals('application/json', pagetype)
46 world.response_format = 'json'
48 if requesttype == 'reverse':
49 assert_equals('text/xml', pagetype)
50 world.response_format = 'xml'
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)
57 api_setup_prepare_params(None)
60 def api_setup_prepare_params(scenario):
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.
70 world.params = step.hashes[0]
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.
77 world.header = step.hashes[0]
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')
84 world.params['addressdetails'] = 1
86 world.params['format'] = fmt.strip()
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])
93 world.params['addressdetails'] = 1
95 world.params['format'] = fmt.strip()
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()
106 @step(u'looking up place ([NRW]?\d+)')
107 def api_setup_details_reverse(step, obj):
108 if obj[0] in ('N', 'R', 'W'):
110 world.params['osm_type'] = obj[0]
111 world.params['osm_id'] = obj[1:]
113 world.params['place_id'] = obj
116 @step(u'looking up details for ([NRW]?\d+)')
117 def api_setup_details(step, obj):
118 if obj[0] in ('N', 'R', 'W'):
120 world.params['osmtype'] = obj[0]
121 world.params['osmid'] = obj[1:]
123 world.params['place_id'] = obj
126 @step(u'looking up (\w+) places ((?:[a-z]\d+,*)+)')
127 def api_setup_lookup(step, fmt, ids):
128 world.params['osm_ids'] = ids
129 if fmt and fmt.strip():
130 world.params['format'] = fmt.strip()
133 @step(u'sending an API call (\w+)')
134 def api_general_call(step, call):