1# -*- coding: utf-8 -*- 2# Licensed to the Apache Software Foundation (ASF) under one or more 3# contributor license agreements. See the NOTICE file distributed with 4# this work for additional information regarding copyright ownership. 5# The ASF licenses this file to You under the Apache License, Version 2.0 6# (the "License"); you may not use this file except in compliance with 7# the License. You may obtain 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, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16import sys 17import unittest 18 19from libcloud.utils.py3 import httplib 20from libcloud.test.file_fixtures import ComputeFileFixtures 21from libcloud.common.base import Connection, Response, JsonResponse, XmlResponse 22from libcloud.test import MockHttp 23 24 25class FileFixturesTests(unittest.TestCase): 26 27 def test_success(self): 28 f = ComputeFileFixtures('meta') 29 self.assertEqual("Hello, World!", f.load('helloworld.txt')) 30 31 def test_failure(self): 32 f = ComputeFileFixtures('meta') 33 self.assertRaises(IOError, f.load, 'nil') 34 35 def test_unicode(self): 36 f = ComputeFileFixtures('meta') 37 self.assertEqual(u"Ś", f.load('unicode.txt')) 38 39 40class MockHttpFileFixturesTests(unittest.TestCase): 41 """ 42 Test the behaviour of MockHttp 43 """ 44 def setUp(self): 45 Connection.conn_class = TestMockHttp 46 Connection.responseCls = Response 47 self.connection = Connection() 48 49 def test_unicode_response(self): 50 r = self.connection.request("/unicode") 51 self.assertEqual(r.parse_body(), u"Ś") 52 53 def test_json_unicode_response(self): 54 self.connection.responseCls = JsonResponse 55 r = self.connection.request("/unicode/json") 56 self.assertEqual(r.object, {'test': u"Ś"}) 57 58 def test_xml_unicode_response(self): 59 self.connection.responseCls = XmlResponse 60 response = self.connection.request("/unicode/xml") 61 self.assertEqual(response.object.text, u"Ś") 62 63 64class TestMockHttp(MockHttp): 65 fixtures = ComputeFileFixtures('meta') 66 67 def _unicode(self, method, url, body, headers): 68 body = self.fixtures.load('unicode.txt') 69 return (httplib.OK, body, {}, httplib.responses[httplib.OK]) 70 71 def _unicode_json(self, method, url, body, headers): 72 body = self.fixtures.load('unicode.json') 73 return (httplib.OK, body, {}, httplib.responses[httplib.OK]) 74 75 def _unicode_xml(self, method, url, body, headers): 76 body = self.fixtures.load('unicode.xml') 77 return (httplib.OK, body, {}, httplib.responses[httplib.OK]) 78 79 def _ascii(self, method, url, body, headers): 80 body = self.fixtures.load('helloworld.txt') 81 return (httplib.OK, body, {}, httplib.responses[httplib.OK]) 82 83 84if __name__ == '__main__': 85 sys.exit(unittest.main()) 86