1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
7 from future
import Gettable
, Future
10 class _Response(object):
11 def __init__(self
, content
=''):
12 self
.content
= content
13 self
.headers
= {'Content-Type': 'none'}
14 self
.status_code
= 200
17 class FakeUrlFetcher(object):
18 def __init__(self
, base_path
):
19 self
._base
_path
= base_path
20 # Mock capabilities. Perhaps this class should be MockUrlFetcher.
23 self
._async
_resolve
_count
= 0
25 def _ReadFile(self
, filename
):
26 with
open(os
.path
.join(self
._base
_path
, filename
), 'r') as f
:
29 def _ListDir(self
, directory
):
30 # In some tests, we need to test listing a directory from the HTML returned
31 # from SVN. This reads an HTML file that has the directories HTML.
32 if not os
.path
.isdir(os
.path
.join(self
._base
_path
, directory
)):
33 return self
._ReadFile
(directory
[:-1])
34 files
= os
.listdir(os
.path
.join(self
._base
_path
, directory
))
35 html
= '<html><title>Revision: 00000</title>\n'
36 for filename
in files
:
37 if filename
.startswith('.'):
39 if os
.path
.isdir(os
.path
.join(self
._base
_path
, directory
, filename
)):
40 html
+= '<a>' + filename
+ '/</a>\n'
42 html
+= '<a>' + filename
+ '</a>\n'
46 def FetchAsync(self
, url
):
47 self
._async
_count
+= 1
48 url
= url
.rsplit('?', 1)[0]
50 self
._async
_resolve
_count
+= 1
51 return self
._DoFetch
(url
)
52 return Future(delegate
=Gettable(resolve
))
56 return self
._DoFetch
(url
)
58 def _DoFetch(self
, url
):
59 url
= url
.rsplit('?', 1)[0]
62 result
.content
= self
._ListDir
(url
)
64 result
.content
= self
._ReadFile
(url
)
67 def CheckAndReset(self
, sync_count
=0, async_count
=0, async_resolve_count
=0):
68 '''Returns a tuple (success, error). Use in tests like:
69 self.assertTrue(*fetcher.CheckAndReset(...))
72 for desc
, expected
, actual
in (
73 ('sync_count', sync_count
, self
._sync
_count
),
74 ('async_count', async_count
, self
._async
_count
),
75 ('async_resolve_count', async_resolve_count
,
76 self
._async
_resolve
_count
)):
77 if actual
!= expected
:
78 errors
.append('%s: expected %s got %s' % (desc
, expected
, actual
))
80 return (len(errors
) == 0, ', '.join(errors
))
87 self
._async
_resolve
_count
= 0
90 class FakeURLFSFetcher(object):
91 '''Use a file_system to resolve fake fetches. Mimics the interface of Google
95 def __init__(self
, file_system
, base_path
):
96 self
._base
_path
= base_path
97 self
._file
_system
= file_system
99 def FetchAsync(self
, url
, **kwargs
):
100 return Future(value
=self
.Fetch(url
))
102 def Fetch(self
, url
, **kwargs
):
103 return _Response(self
._file
_system
.ReadSingle(
104 self
._base
_path
+ '/' + url
).Get())
107 class MockURLFetcher(object):
108 def __init__(self
, fetcher
):
109 self
._fetcher
= fetcher
112 def Fetch(self
, url
, **kwargs
):
113 self
._fetch
_count
+= 1
114 return self
._fetcher
.Fetch(url
, **kwargs
)
116 def FetchAsync(self
, url
, **kwargs
):
117 self
._fetch
_async
_count
+= 1
118 future
= self
._fetcher
.FetchAsync(url
, **kwargs
)
120 self
._fetch
_resolve
_count
+= 1
122 return Future(delegate
=Gettable(resolve
))
124 def CheckAndReset(self
,
127 fetch_resolve_count
=0):
129 for desc
, expected
, actual
in (
130 ('fetch_count', fetch_count
, self
._fetch
_count
),
131 ('fetch_async_count', fetch_async_count
, self
._fetch
_async
_count
),
132 ('fetch_resolve_count', fetch_resolve_count
,
133 self
._fetch
_resolve
_count
)):
134 if actual
!= expected
:
135 errors
.append('%s: expected %s got %s' % (desc
, expected
, actual
))
137 return (len(errors
) == 0, ', '.join(errors
))
142 self
._fetch
_count
= 0
143 self
._fetch
_async
_count
= 0
144 self
._fetch
_resolve
_count
= 0