My favorites | Sign in
Project Logo
                
Changes to /trunk/apptestsuite/client/atompubbase/mockhttp.py
r970 vs. r971   Edit
  Compare: vs.   Format:
Revision r971
Go to: 
Project members, sign in to write a code review
/trunk/apptestsuite/client/atompubbase/mockhttp.py   r970 /trunk/apptestsuite/client/atompubbase/mockhttp.py   r971
1 import urlparse 1 import urlparse
2 import urllib
2 import httplib2 3 import httplib2
3 from email import message_from_string, message_from_file 4 from email import message_from_string, message_from_file
4 import os 5 import os
5 6
6 HTTP_SRC_DIR = "./tests/" 7
7 8
8 class MockHttp: 9 class MockHttp:
9 """ 10 """
10 A mock for httplib2.Http that takes its 11 A mock for httplib2.Http that takes its
11 response headers and bodies from files on disk 12 response headers and bodies from files on disk
12 """ 13 """
13 def __init__(self, directory): 14 def __init__(self, directory):
14 self.directory = directory 15 self.directory = directory
15 self.hit_counter = {} 16 self.hit_counter = {}
16 17
17 def request(self, uri, method="GET", body=None, headers=None, redirections=5): 18 def request(self, uri, method="GET", body=None, headers=None, redirections=5):
18 counter = self.hit_counter.get(method+uri, 0) 19 counter = self.hit_counter.get(method+uri, 0)
19 counter += 1 20 counter += 1
20 self.hit_counter[method+uri] = counter 21 self.hit_counter[method+uri] = counter
21 path = urlparse.urlparse(uri)[2] 22 path = urlparse.urlparse(uri)[2]
22 fname = os.path.join(self.directory, method, path.strip("/") + ".file") 23 fname = os.path.join(self.directory, method, urllib.quote(path.strip("/")) + ".file")
23 fname_next = fname + "." + str(counter) 24 fname_next = fname + "." + str(counter)
24 if os.path.exists(fname_next): 25 if os.path.exists(fname_next):
25 fname = fname_next 26 fname = fname_next
26 if os.path.exists(fname): 27 if os.path.exists(fname):
27 f = file(fname, "r") 28 f = file(fname, "r")
28 response = message_from_file(f) 29 response = message_from_file(f)
29 f.close() 30 f.close()
30 body = response.get_payload() 31 body = response.get_payload()
31 headers = httplib2.Response(response) 32 headers = httplib2.Response(response)
32 return (headers, body) 33 return (headers, body)
33 else: 34 else:
34 return (httplib2.Response({"status": "404"}), "") 35 return (httplib2.Response({"status": "404"}), "")
35 36
36 def add_credentials(self, name, password): 37 def add_credentials(self, name, password):
37 pass 38 pass
38 39
39 40
40 class MockRecorder(httplib2.Http): 41 class MockRecorder(httplib2.Http):
41 def __init__(self, h, directory): 42 def __init__(self, h, directory):
42 self.h = h 43 self.h = h
43 self.directory = directory 44 self.directory = directory
44 self.hit_counter = {} 45 self.hit_counter = {}
45 46
46 def request(self, uri, method="GET", body=None, headers=None, redirections=5): 47 def request(self, uri, method="GET", body=None, headers=None, redirections=5):
47 counter = self.hit_counter.get(method+uri, 0) 48 counter = self.hit_counter.get(method+uri, 0)
48 counter += 1 49 counter += 1
49 self.hit_counter[method+uri] = counter 50 self.hit_counter[method+uri] = counter
50 headers, body = self.h.request(uri, method, body, headers, redirections) 51 headers, body = self.h.request(uri, method, body, headers, redirections)
51 path = urlparse.urlparse(uri)[2] 52 path = urlparse.urlparse(uri)[2]
52 fname = os.path.join(self.directory, method, path.strip("/") + ".file") 53 fname = os.path.join(self.directory, method, urllib.quote(path.strip("/")) + ".file")
53 if counter >= 2: 54 if counter >= 2:
54 fname = fname + "." + str(counter) 55 fname = fname + "." + str(counter)
55 dirname = os.path.dirname(fname) 56 dirname = os.path.dirname(fname)
56 if not os.path.exists(dirname): 57 if not os.path.exists(dirname):
57 os.makedirs(dirname) 58 os.makedirs(dirname)
58 59
59 f = file(fname, "w") 60 f = file(fname, "w")
60 f.write( 61 f.write(
61 "\r\n".join(["%s: %s" % (key, value) for key, value in headers.iteritems()]) 62 "\r\n".join(["%s: %s" % (key, value) for key, value in headers.iteritems()])
62 ) 63 )
63 f.write("\r\n\r\n") 64 f.write("\r\n\r\n")
64 f.write(body) 65 f.write(body)
65 f.close() 66 f.close()
66 return (headers, body) 67 return (headers, body)
67 68
68 def add_credentials(self, name, password): 69 def add_credentials(self, name, password):
69 h.add_credentials(name, password) 70 h.add_credentials(name, password)
Hosted by Google Code