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
16"""Mock instance of ArchivedHttpRequest used for testing."""
17
18
19class ArchivedHttpRequest(object):
20  """Mock instance of ArchivedHttpRequest in HttpArchive."""
21
22  def __init__(self, command, host, path, request_body, headers):
23    """Initialize an ArchivedHttpRequest.
24
25    Args:
26      command: a string (e.g. 'GET' or 'POST').
27      host: a host name (e.g. 'www.google.com').
28      path: a request path (e.g. '/search?q=dogs').
29      request_body: a request body string for a POST or None.
30      headers: [(header1, value1), ...] list of tuples
31    """
32    self.command = command
33    self.host = host
34    self.path = path
35    self.request_body = request_body
36    self.headers = headers
37    self.trimmed_headers = headers
38
39  def __str__(self):
40    return '%s %s%s %s' % (self.command, self.host, self.path,
41                           self.trimmed_headers)
42
43  def __repr__(self):
44    return repr((self.command, self.host, self.path, self.request_body,
45                 self.trimmed_headers))
46
47  def __hash__(self):
48    """Return a integer hash to use for hashed collections including dict."""
49    return hash(repr(self))
50
51  def __eq__(self, other):
52    """Define the __eq__ method to match the hash behavior."""
53    return repr(self) == repr(other)
54
55  def matches(self, command=None, host=None, path=None):
56    """Returns true iff the request matches all parameters."""
57    return ((command is None or command == self.command) and
58            (host is None or host == self.host) and
59            (path is None or path == self.path))
60