Coverage for manila/tests/test_api.py: 38%

32 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2026-02-18 22:19 +0000

1# Copyright 2010 United States Government as represented by the 

2# Administrator of the National Aeronautics and Space Administration. 

3# All Rights Reserved. 

4# 

5# Licensed under the Apache License, Version 2.0 (the "License"); you may 

6# not use this file except in compliance with the License. You may obtain 

7# a copy of the License at 

8# 

9# http://www.apache.org/licenses/LICENSE-2.0 

10# 

11# Unless required by applicable law or agreed to in writing, software 

12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 

13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 

14# License for the specific language governing permissions and limitations 

15# under the License. 

16 

17"""Unit tests for the API endpoint.""" 

18 

19import http.client as http_client 

20import io 

21import webob 

22 

23 

24class FakeHttplibSocket(object): 

25 """A fake socket implementation for http_client.HTTPResponse, trivial.""" 

26 def __init__(self, response_string): 

27 self.response_string = response_string 

28 self._buffer = io.StringIO(response_string) 

29 

30 def makefile(self, _mode, _other): 

31 """Returns the socket's internal buffer.""" 

32 return self._buffer 

33 

34 

35class FakeHttplibConnection(object): 

36 """A fake http_client.HTTPConnection for boto. 

37 

38 requests made via this connection actually get translated and routed into 

39 our WSGI app, we then wait for the response and turn it back into 

40 the http_client.HTTPResponse that boto expects. 

41 """ 

42 def __init__(self, app, host, is_secure=False): 

43 self.app = app 

44 self.host = host 

45 

46 def request(self, method, path, data, headers): 

47 req = webob.Request.blank(path) 

48 req.method = method 

49 req.body = data 

50 req.headers = headers 

51 req.headers['Accept'] = 'text/html' 

52 req.host = self.host 

53 # Call the WSGI app, get the HTTP response 

54 resp = str(req.get_response(self.app)) 

55 # For some reason, the response doesn't have "HTTP/1.0 " prepended; I 

56 # guess that's a function the web server usually provides. 

57 resp = "HTTP/1.0 %s" % resp 

58 self.sock = FakeHttplibSocket(resp) 

59 self.http_response = http_client.HTTPResponse(self.sock) 

60 # NOTE(vish): boto is accessing private variables for some reason 

61 self._HTTPConnection__response = self.http_response 

62 self.http_response.begin() 

63 

64 def getresponse(self): 

65 return self.http_response 

66 

67 def getresponsebody(self): 

68 return self.sock.response_string 

69 

70 def close(self): 

71 """Required for compatibility with boto/tornado.""" 

72 pass