当前位置: 首页>>代码示例>>Python>>正文


Python utils.mock_http函数代码示例

本文整理汇总了Python中tests.utils.mock_http函数的典型用法代码示例。如果您正苦于以下问题:Python mock_http函数的具体用法?Python mock_http怎么用?Python mock_http使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了mock_http函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_delete

    def test_delete(self):

        class BasicMost(self.cls):
            name  = fields.Field()
            value = fields.Field()

        b = BasicMost()
        self.assertRaises(ValueError, lambda: b.put())

        request = {
            'uri': 'http://example.com/bwuh',
            'headers': {'accept': 'application/json'},
        }
        content = """{"name": "Molly", "value": 80}"""
        h = utils.mock_http(request, content)
        b = BasicMost.get('http://example.com/bwuh', http=h)
        self.assertEquals(b.value, 80)
        mox.Verify(h)

        headers = {
            'accept':   'application/json',
            'if-match': '7',  # default etag
        }
        request  = dict(uri='http://example.com/bwuh', method='DELETE', headers=headers)
        response = dict(status=204)
        h = utils.mock_http(request, response)
        b.delete(http=h)
        mox.Verify(h)

        self.failIf(b._location is not None)
        self.failIf(hasattr(b, '_etag'))
开发者ID:sekimura,项目名称:remoteobjects,代码行数:31,代码来源:test_http.py

示例2: test_put_no_content

    def test_put_no_content(self):
        """
        Don't try to update from a no-content response.

        """

        class BasicMost(self.cls):
            name  = fields.Field()
            value = fields.Field()

        request = {
            'uri': 'http://example.com/bwuh',
            'headers': {'accept': 'application/json'},
        }
        content = """{"name": "Molly", "value": 80}"""
        h = utils.mock_http(request, content)
        b = BasicMost.get('http://example.com/bwuh', http=h)
        self.assertEquals(b.name, 'Molly')
        mox.Verify(h)

        headers = {
            'accept':       'application/json',
            'content-type': 'application/json',
            'if-match': '7',
        }
        request  = dict(uri='http://example.com/bwuh', method='PUT', headers=headers, body=content)
        response = dict(content="", status=204)
        h = utils.mock_http(request, response)
        b.put(http=h)
        mox.Verify(h)

        self.assertEquals(b.name, 'Molly')
开发者ID:sekimura,项目名称:remoteobjects,代码行数:32,代码来源:test_http.py

示例3: test_put_failure

    def test_put_failure(self):

        class BasicMost(self.cls):
            name  = fields.Field()
            value = fields.Field()

        request = {
            'uri': 'http://example.com/bwuh',
            'headers': {'accept': 'application/json'},
        }
        content = """{"name": "Molly", "value": 80}"""
        h = utils.mock_http(request, content)
        b = BasicMost.get('http://example.com/bwuh', http=h)
        self.assertEquals(b.value, 80)
        mox.Verify(h)

        b.value = 'superluminal'

        headers = {
            'accept':       'application/json',
            'content-type': 'application/json',
            'if-match':     '7',  # default etag
        }
        content = """{"name": "Molly", "value": "superluminal"}"""
        request = dict(uri='http://example.com/bwuh', method='PUT',
                       body=content, headers=headers)
        # Simulate a changed resource.
        response = dict(status=412)
        h = utils.mock_http(request, response)
        self.assertRaises(BasicMost.PreconditionFailed, lambda: b.put(http=h))
        mox.Verify(h)
开发者ID:sekimura,项目名称:remoteobjects,代码行数:31,代码来源:test_http.py

示例4: test_post

    def test_post(self):

        class BasicMost(self.cls):
            name  = fields.Field()
            value = fields.Field()

        class ContainerMost(self.cls):
            name = fields.Field()

        request = {
            'uri': 'http://example.com/asfdasf',
            'headers': {'accept': 'application/json'},
        }
        content = """{"name": "CBS"}"""
        h = utils.mock_http(request, content)
        c = ContainerMost.get('http://example.com/asfdasf', http=h)
        self.assertEquals(c.name, 'CBS')
        mox.Verify(h)

        b = BasicMost(name='Fred Friendly', value=True)

        headers = {'accept': 'application/json'}
        content = """{"name": "Fred Friendly", "value": true}"""
        request = dict(uri='http://example.com/asfdasf', method='POST',
                       body=content, headers=headers)
        response = dict(content=content, status=201, etag='xyz',
                        location='http://example.com/fred')
        h = utils.mock_http(request, response)
        c.post(b, http=h)
        mox.Verify(h)

        self.assertEquals(b._location, 'http://example.com/fred')
        self.assertEquals(b._etag, 'xyz')
开发者ID:apparentlymart,项目名称:remoteobjects,代码行数:33,代码来源:test_http.py

示例5: test_put

    def test_put(self):

        class BasicMost(self.cls):
            name  = fields.Field()
            value = fields.Field()

        b = BasicMost()
        self.assertRaises(ValueError, lambda: b.put())

        request = {
            'uri': 'http://example.com/bwuh',
            'headers': {'accept': 'application/json'},
        }
        content = """{"name": "Molly", "value": 80}"""
        h = utils.mock_http(request, content)
        b = BasicMost.get('http://example.com/bwuh', http=h)
        self.assertEquals(b.name, 'Molly')
        mox.Verify(h)

        headers = {
            'accept':       'application/json',
            'content-type': 'application/json',
            'if-match':     '7',  # default etag
        }
        request  = dict(uri='http://example.com/bwuh', method='PUT', headers=headers, body=content)
        response = dict(content=content, etag='xyz')
        h = utils.mock_http(request, response)
        b.put(http=h)
        mox.Verify(h)

        self.assertEquals(b._etag, 'xyz')
开发者ID:sekimura,项目名称:remoteobjects,代码行数:31,代码来源:test_http.py

示例6: test_set_before_delivery

    def test_set_before_delivery(self):

        class Toy(self.cls):
            names = fields.List(fields.Field())
            foo = fields.Field()

        url = 'http://example.com/whahay'
        headers = {"accept": "application/json"}
        request = dict(uri=url, headers=headers)
        content = """{"names": ["Mollifred"], "foo":"something"}"""
        h = utils.mock_http(request, content)

        # test case where attribute is assigned to object ahead of delivery
        t = Toy.get(url, http=h)
        t.names = ["New name"]
        d = t.to_dict() # this delivers the object

        # self.assertEquals(t.foo, "something")
        self.assertEquals(d['names'][0], "New name")
        self.assertEquals(t.names[0], "New name")

        h = utils.mock_http(request, content)
        # test case where we update_from_dict explictly after setting attributes
        t = Toy.get(url, http=h)
        t.foo = "local change"
        t.update_from_dict({"names": ["local update"]})

        self.assertEquals(t.foo, None)
开发者ID:alex,项目名称:remoteobjects,代码行数:28,代码来源:test_promise.py

示例7: test_decoding

def test_decoding(object_class, json, count):
    request = {"uri": "http://example.com/ohhai", "headers": {"accept": "application/json"}}
    h = utils.mock_http(request, json)

    # warm up remoteobjects
    o = object_class.get("http://example.com/ohhai", http=h)
    o.deliver()

    for _ in xrange(count):
        h = utils.mock_http(request, json)

        t = time.time()
        o = object_class.get("http://example.com/ohhai", http=h)
        o.deliver()
        yield (time.time() - t)
开发者ID:apparentlymart,项目名称:remoteobjects,代码行数:15,代码来源:benchmark_decoding.py

示例8: test_decoding

def test_decoding(object_class, json, count):
    request = {
        'uri': 'http://example.com/ohhai',
        'headers': {'accept': 'application/json'},
    }
    h = utils.mock_http(request, json)

    # warm up remoteobjects
    o = object_class.get('http://example.com/ohhai', http=h)
    o.deliver()

    for _ in xrange(count):
        h = utils.mock_http(request, json)

        t = time.time()
        o = object_class.get('http://example.com/ohhai', http=h)
        o.deliver()
        yield (time.time() - t)
开发者ID:alex,项目名称:remoteobjects,代码行数:18,代码来源:benchmark_decoding.py

示例9: http

    def http(self, key, credentials=None):
        try:
            req = requests[key]
        except KeyError:
            raise Exception("No such mock request %s" % key)

        mock = utils.mock_http(*req)
        mock.endpoint = "http://api.typepad.com"
        typepad.client = mock

        return mock
开发者ID:giorgil,项目名称:python-typepad-api,代码行数:11,代码来源:test_api.py

示例10: test_not_found

    def test_not_found(self):
        self.assert_(self.cls.NotFound)

        class Huh(self.cls):
            name = fields.Field()

        self.assert_(Huh.NotFound)

        request = {
            'uri': 'http://example.com/bwuh',
            'headers': {'accept': 'application/json'},
        }
        response = {'content': '', 'status': 404}
        http = utils.mock_http(request, response)
        self.assertRaises(Huh.NotFound, lambda: Huh.get('http://example.com/bwuh', http=http).name)
        mox.Verify(http)
开发者ID:sekimura,项目名称:remoteobjects,代码行数:16,代码来源:test_http.py

示例11: test_index

    def test_index(self):

        class Toybox(self.cls):
            pass

        url = 'http://example.com/whahay'
        headers = {"accept": "application/json"}
        request = dict(uri=url, headers=headers)
        content = """{"entries":[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}"""
        h = utils.mock_http(request, content)
        mox.Replay(h)

        b = Toybox.get('http://example.com/whahay', http=h)
        self.assertEqual(b[7], 7)

        mox.Verify(h)        
开发者ID:apparentlymart,项目名称:remoteobjects,代码行数:16,代码来源:test_listobject.py

示例12: test_get

    def test_get(self):

        class BasicMost(self.cls):
            name  = fields.Field()
            value = fields.Field()

        request = {
            'uri': 'http://example.com/ohhai',
            'headers': {'accept': 'application/json'},
        }
        content = """{"name": "Fred", "value": 7}"""

        h = utils.mock_http(request, content)
        b = BasicMost.get('http://example.com/ohhai', http=h)
        self.assertEquals(b.name, 'Fred')
        self.assertEquals(b.value, 7)
        mox.Verify(h)
开发者ID:askeing,项目名称:remoteobjects,代码行数:17,代码来源:test_http.py

示例13: test_get_bad_encoding

    def test_get_bad_encoding(self):

        class BasicMost(self.cls):
            name  = fields.Field()
            value = fields.Field()

        request = {
            'uri': 'http://example.com/ohhai',
            'headers': {'accept': 'application/json'},
        }
        content = """{"name": "Fred\xf1", "value": "image by \xefndrew Example"}"""

        h = utils.mock_http(request, content)
        b = BasicMost.get('http://example.com/ohhai', http=h)
        self.assertEquals(b.name, u"Fred\ufffd")
        # Bad characters are replaced with the unicode Replacement Character 0xFFFD.
        self.assertEquals(b.value, u"image by \ufffdrew Example")
        mox.Verify(h)
开发者ID:sekimura,项目名称:remoteobjects,代码行数:18,代码来源:test_http.py

示例14: test_delete_failure

    def test_delete_failure(self):

        class BasicMost(self.cls):
            name  = fields.Field()
            value = fields.Field()

        b = BasicMost(name='Molly', value=80)
        b._location = 'http://example.com/bwuh'
        b._etag = 'asfdasf'

        headers = {
            'accept':   'application/json',
            'if-match': 'asfdasf',
        }
        request  = dict(uri='http://example.com/bwuh', method='DELETE', headers=headers)
        response = dict(status=412)  # Precondition Failed

        h = utils.mock_http(request, response)
        self.assertRaises(BasicMost.PreconditionFailed, lambda: b.delete(http=h))
        mox.Verify(h)
开发者ID:sekimura,项目名称:remoteobjects,代码行数:20,代码来源:test_http.py

示例15: test_basic

    def test_basic(self):
        class Tiny(self.cls):
            name = fields.Field()

        h = mox.MockObject(httplib2.Http)
        mox.Replay(h)

        url = "http://example.com/whahay"
        t = Tiny.get(url, http=h)

        # Check that we didn't do anything.
        mox.Verify(h)

        headers = {"accept": "application/json"}
        request = dict(uri=url, headers=headers)
        content = """{"name": "Mollifred"}"""
        h = utils.mock_http(request, content)
        t._http = h  # inject, oops
        self.assertEquals(t.name, "Mollifred")
        mox.Verify(h)
开发者ID:saymedia,项目名称:remoteobjects,代码行数:20,代码来源:test_promise.py


注:本文中的tests.utils.mock_http函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。