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.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)
19 fd = urllib2.urlopen(req)
20 world.page = fd.read()
21 world.returncode = 200
22 except urllib2.HTTPError, ex:
23 world.returncode = ex.code
28 assert_equal('utf-8', pageinfo.getparam('charset').lower())
29 pagetype = pageinfo.gettype()
31 fmt = world.params.get('format')
33 assert_equals('text/html', pagetype)
34 world.response_format = fmt
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)
46 assert_equals('application/json', pagetype)
47 world.response_format = 'json'
49 if requesttype == 'reverse':
50 assert_equals('text/xml', pagetype)
51 world.response_format = 'xml'
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)
58 api_setup_prepare_params(None)
61 def api_setup_prepare_params(scenario):
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.
71 world.params = step.hashes[0]
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.
78 world.header = step.hashes[0]
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')
85 world.params['addressdetails'] = 1
87 world.params['format'] = fmt.strip()
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])
94 world.params['addressdetails'] = 1
96 world.params['format'] = fmt.strip()
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()
107 @step(u'looking up place ([NRW]?\d+)')
108 def api_setup_details_reverse(step, obj):
109 if obj[0] in ('N', 'R', 'W'):
111 world.params['osm_type'] = obj[0]
112 world.params['osm_id'] = obj[1:]
114 world.params['place_id'] = obj
117 @step(u'looking up details for ([NRW]?\d+)')
118 def api_setup_details(step, obj):
119 if obj[0] in ('N', 'R', 'W'):
121 world.params['osmtype'] = obj[0]
122 world.params['osmid'] = obj[1:]
124 world.params['place_id'] = obj
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()
134 @step(u'sending an API call (\w+)')
135 def api_general_call(step, call):