~ubuntu-branches/ubuntu/karmic/gears/karmic

« back to all changes in this revision

Viewing changes to gears/test/testcases/cgi/reverse_geocoder.py

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Lesicnik
  • Date: 2009-04-30 19:15:25 UTC
  • Revision ID: james.westby@ubuntu.com-20090430191525-0790sb5wzg8ou0xb
Tags: upstream-0.5.21.0~svn3334+dfsg
ImportĀ upstreamĀ versionĀ 0.5.21.0~svn3334+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python2.4
 
2
 
 
3
""" A dummy network location provider that is used for testing reverse
 
4
geocoding. For fix requests it does not return a position. For reverse geocode
 
5
requests, it returns a fixed address.
 
6
"""
 
7
 
 
8
class FixRequest(object):
 
9
  """ Represents a fix request. """
 
10
 
 
11
  def __init__(self, request_body):
 
12
    """ Initialize FixRequest with a JSON string.
 
13
 
 
14
    Args:
 
15
      request_body: Request body as JSON string.
 
16
    """
 
17
    # simplejson module is imported by the server, see
 
18
    # /test/runner/testwebserver.py
 
19
    json_decoder = simplejson.JSONDecoder()
 
20
    self.request = json_decoder.decode(request_body)
 
21
 
 
22
  def IsValidRequest(self):
 
23
    return self.request.has_key("version") and self.request.has_key("host")
 
24
 
 
25
  def IsReverseGeocodeRequest(self):
 
26
    if self.request.has_key("location"):
 
27
      location = self.request["location"]
 
28
      if location.has_key("latitude") and location.has_key("longitude"):
 
29
        return True
 
30
    return False
 
31
 
 
32
 
 
33
# A successful reverse-geocode response. We must include a valid latitude and
 
34
# longitude, but Gears will return to JavaScript the values from the reverse
 
35
# geocode request, not these values.
 
36
REVERSE_GEOCODE_REQUEST_RESPONSE = """{
 
37
  "location": {
 
38
    "latitude": 9.9,
 
39
    "longitude": 9.9,
 
40
    "address": {
 
41
      "street_number": "76",
 
42
      "street": "Buckingham Palace Road",
 
43
      "postal_code": "SW1W 9TQ",
 
44
      "city": "London",
 
45
      "county": "London",
 
46
      "region": "London",
 
47
      "country": "United Kingdom",
 
48
      "country_code": "uk"
 
49
    }
 
50
  }
 
51
}"""
 
52
 
 
53
# An empty position response, indicates no location fixes were found.
 
54
FIX_REQUEST_RESPONSE = """{}"""
 
55
 
 
56
 
 
57
def send_response(request_handler, code, response):
 
58
  """ Helper function to construct a HTTP response.
 
59
 
 
60
  Args:
 
61
    request_handler: The request handler instance to issue the response
 
62
    code: The integer HTTP status code for the response
 
63
    response: The string body of the response.
 
64
  """
 
65
 
 
66
  request_handler.send_response(code)
 
67
  if code == 200 :
 
68
    request_handler.send_header('Content-type', 'application/json')
 
69
  request_handler.end_headers()
 
70
  request_handler.outgoing.append(response)
 
71
 
 
72
 
 
73
# We accept only POST requests with Content-Type application/json.
 
74
if self.command == 'POST':
 
75
  if self.headers.get('Content-Type') == 'application/json':
 
76
    if self.body:
 
77
      fix_request = FixRequest(self.body)
 
78
 
 
79
      # Hard-coded rules to return desired responses
 
80
      if not fix_request.IsValidRequest():
 
81
        send_response(self, 400, "Invalid request")
 
82
      elif fix_request.IsReverseGeocodeRequest():
 
83
        send_response(self, 200, REVERSE_GEOCODE_REQUEST_RESPONSE)
 
84
      else:
 
85
        send_response(self, 200, FIX_REQUEST_RESPONSE)
 
86
    else:
 
87
      send_response(self, 400, "Empty request")
 
88
  else:
 
89
    send_response(self, 400, "Content-Type should be application/json")
 
90
else:
 
91
  send_response(self, 405, "Request must be HTTP POST.")