fake_url_fetcher.py revision a93a17c8d99d686bd4a1511e5504e5e6cc9fcadf
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. 4 5import os 6 7from future import Future 8 9class _Response(object): 10 def __init__(self): 11 self.content = '' 12 self.headers = { 'content-type': 'none' } 13 self.status_code = 200 14 15class FakeUrlFetcher(object): 16 def __init__(self, base_path): 17 self._base_path = base_path 18 19 def _ReadFile(self, filename): 20 with open(os.path.join(self._base_path, filename), 'r') as f: 21 return f.read() 22 23 def _ListDir(self, directory): 24 # In some tests, we need to test listing a directory from the HTML returned 25 # from SVN. This reads an HTML file that has the directories HTML. 26 if not os.path.isdir(os.path.join(self._base_path, directory)): 27 return self._ReadFile(directory[:-1]) 28 files = os.listdir(os.path.join(self._base_path, directory)) 29 html = '<html><title>Revision: 00000</title>\n' 30 for filename in files: 31 if filename.startswith('.'): 32 continue 33 if os.path.isdir(os.path.join(self._base_path, directory, filename)): 34 html += '<a>' + filename + '/</a>\n' 35 else: 36 html += '<a>' + filename + '</a>\n' 37 html += '</html>' 38 return html 39 40 def FetchAsync(self, url): 41 url = url.rsplit('?', 1)[0] 42 return Future(value=self.Fetch(url)) 43 44 def Fetch(self, url): 45 url = url.rsplit('?', 1)[0] 46 result = _Response() 47 if url.endswith('/'): 48 result.content = self._ListDir(url) 49 else: 50 result.content = self._ReadFile(url) 51 return result 52