mockhttprequest.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/env python
  2. # Copyright 2010 Google Inc. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """Mock instance of ArchivedHttpRequest used for testing."""
  16. class ArchivedHttpRequest(object):
  17. """Mock instance of ArchivedHttpRequest in HttpArchive."""
  18. def __init__(self, command, host, path, request_body, headers):
  19. """Initialize an ArchivedHttpRequest.
  20. Args:
  21. command: a string (e.g. 'GET' or 'POST').
  22. host: a host name (e.g. 'www.google.com').
  23. path: a request path (e.g. '/search?q=dogs').
  24. request_body: a request body string for a POST or None.
  25. headers: [(header1, value1), ...] list of tuples
  26. """
  27. self.command = command
  28. self.host = host
  29. self.path = path
  30. self.request_body = request_body
  31. self.headers = headers
  32. self.trimmed_headers = headers
  33. def __str__(self):
  34. return '%s %s%s %s' % (self.command, self.host, self.path,
  35. self.trimmed_headers)
  36. def __repr__(self):
  37. return repr((self.command, self.host, self.path, self.request_body,
  38. self.trimmed_headers))
  39. def __hash__(self):
  40. """Return a integer hash to use for hashed collections including dict."""
  41. return hash(repr(self))
  42. def __eq__(self, other):
  43. """Define the __eq__ method to match the hash behavior."""
  44. return repr(self) == repr(other)
  45. def matches(self, command=None, host=None, path=None):
  46. """Returns true iff the request matches all parameters."""
  47. return ((command is None or command == self.command) and
  48. (host is None or host == self.host) and
  49. (path is None or path == self.path))