|
| 1 | +# Copyright 2015 Google Inc. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import unittest2 |
| 16 | + |
| 17 | + |
| 18 | +class Test_lookup_bucket(unittest2.TestCase): |
| 19 | + |
| 20 | + def _callFUT(self, bucket_name, connection=None): |
| 21 | + from gcloud.storage.api import lookup_bucket |
| 22 | + return lookup_bucket(bucket_name, connection=connection) |
| 23 | + |
| 24 | + def test_lookup_bucket_miss(self): |
| 25 | + from gcloud.storage.connection import Connection |
| 26 | + PROJECT = 'project' |
| 27 | + NONESUCH = 'nonesuch' |
| 28 | + conn = Connection(PROJECT) |
| 29 | + URI = '/'.join([ |
| 30 | + conn.API_BASE_URL, |
| 31 | + 'storage', |
| 32 | + conn.API_VERSION, |
| 33 | + 'b', |
| 34 | + 'nonesuch?project=%s' % PROJECT, |
| 35 | + ]) |
| 36 | + http = conn._http = Http( |
| 37 | + {'status': '404', 'content-type': 'application/json'}, |
| 38 | + '{}', |
| 39 | + ) |
| 40 | + bucket = self._callFUT(NONESUCH, connection=conn) |
| 41 | + self.assertEqual(bucket, None) |
| 42 | + self.assertEqual(http._called_with['method'], 'GET') |
| 43 | + self.assertEqual(http._called_with['uri'], URI) |
| 44 | + |
| 45 | + def _lookup_bucket_hit_helper(self, use_default=False): |
| 46 | + from gcloud.storage._testing import _monkey_defaults |
| 47 | + from gcloud.storage.bucket import Bucket |
| 48 | + from gcloud.storage.connection import Connection |
| 49 | + PROJECT = 'project' |
| 50 | + BLOB_NAME = 'blob-name' |
| 51 | + conn = Connection(PROJECT) |
| 52 | + URI = '/'.join([ |
| 53 | + conn.API_BASE_URL, |
| 54 | + 'storage', |
| 55 | + conn.API_VERSION, |
| 56 | + 'b', |
| 57 | + '%s?project=%s' % (BLOB_NAME, PROJECT), |
| 58 | + ]) |
| 59 | + http = conn._http = Http( |
| 60 | + {'status': '200', 'content-type': 'application/json'}, |
| 61 | + '{"name": "%s"}' % BLOB_NAME, |
| 62 | + ) |
| 63 | + |
| 64 | + if use_default: |
| 65 | + with _monkey_defaults(connection=conn): |
| 66 | + bucket = self._callFUT(BLOB_NAME) |
| 67 | + else: |
| 68 | + bucket = self._callFUT(BLOB_NAME, connection=conn) |
| 69 | + |
| 70 | + self.assertTrue(isinstance(bucket, Bucket)) |
| 71 | + self.assertTrue(bucket.connection is conn) |
| 72 | + self.assertEqual(bucket.name, BLOB_NAME) |
| 73 | + self.assertEqual(http._called_with['method'], 'GET') |
| 74 | + self.assertEqual(http._called_with['uri'], URI) |
| 75 | + |
| 76 | + def test_lookup_bucket_hit(self): |
| 77 | + self._lookup_bucket_hit_helper(use_default=False) |
| 78 | + |
| 79 | + def test_lookup_bucket_use_default(self): |
| 80 | + self._lookup_bucket_hit_helper(use_default=True) |
| 81 | + |
| 82 | + |
| 83 | +class Http(object): |
| 84 | + |
| 85 | + _called_with = None |
| 86 | + |
| 87 | + def __init__(self, headers, content): |
| 88 | + from httplib2 import Response |
| 89 | + self._response = Response(headers) |
| 90 | + self._content = content |
| 91 | + |
| 92 | + def request(self, **kw): |
| 93 | + self._called_with = kw |
| 94 | + return self._response, self._content |
0 commit comments