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


Python request.Request类代码示例

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


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

示例1: test_headers

def test_headers():
    headers = {'X-Header1': ['h1'], 'X-Header2': 'h2'}
    req = Request('GET', 'http://go.com/', '', headers)
    assert req.headers == {'X-Header1': 'h1', 'X-Header2': 'h2'}

    req.add_header('X-Header1', 'h11')
    assert req.headers == {'X-Header1': 'h11', 'X-Header2': 'h2'}
开发者ID:darioush,项目名称:vcrpy,代码行数:7,代码来源:test_request.py

示例2: test_headers

def test_headers():
    headers = {"X-Header1": ["h1"], "X-Header2": "h2"}
    req = Request("GET", "http://go.com/", "", headers)
    assert req.headers == {"X-Header1": "h1", "X-Header2": "h2"}

    req.add_header("X-Header1", "h11")
    assert req.headers == {"X-Header1": "h11", "X-Header2": "h2"}
开发者ID:IvanMalison,项目名称:vcrpy,代码行数:7,代码来源:test_request.py

示例3: test_remove_json_post_data_parameters

def test_remove_json_post_data_parameters():
    body = b'{"id": "secret", "foo": "bar", "baz": "qux"}'
    request = Request('POST', 'http://google.com', body, {})
    request.headers['Content-Type'] = 'application/json'
    remove_post_data_parameters(request, ['id'])
    request_body_json = json.loads(request.body.decode('utf-8'))
    expected_json = json.loads(b'{"foo": "bar", "baz": "qux"}'.decode('utf-8'))
    assert request_body_json == expected_json
开发者ID:jayvdb,项目名称:vcrpy,代码行数:8,代码来源:test_filters.py

示例4: request

 def request(self, method, url, body=None, headers=None):
     '''Persist the request metadata in self._vcr_request'''
     self._vcr_request = Request(
         method=method,
         uri=self._uri(url),
         body=body,
         headers=headers or {}
     )
     log.debug('Got {0}'.format(self._vcr_request))
开发者ID:hartsock,项目名称:vcrpy,代码行数:9,代码来源:__init__.py

示例5: test_replace_json_post_data_parameters

def test_replace_json_post_data_parameters():
    # This tests all of:
    #   1. keeping a parameter
    #   2. removing a parameter
    #   3. replacing a parameter
    #   4. replacing a parameter using a callable
    #   5. removing a parameter using a callable
    #   6. replacing a parameter that doesn't exist
    body = b'{"one": "keep", "two": "lose", "three": "change", "four": "shout", "five": "whisper"}'
    request = Request('POST', 'http://google.com', body, {})
    request.headers['Content-Type'] = 'application/json'
    replace_post_data_parameters(request, [
        ('two', None),
        ('three', 'tada'),
        ('four', lambda key, value, request: value.upper()),
        ('five', lambda key, value, request: None),
        ('six', 'doesntexist'),
    ])
    request_data = json.loads(request.body.decode('utf-8'))
    expected_data = json.loads('{"one": "keep", "three": "tada", "four": "SHOUT"}')
    assert request_data == expected_data
开发者ID:Bjwebb,项目名称:vcrpy,代码行数:21,代码来源:test_filters.py

示例6: request

 def request(self, method, url, body=None, headers=None):
     '''Persist the request metadata in self._vcr_request'''
     self._vcr_request = Request(
         protocol=self._protocol,
         host=self.real_connection.host,
         port=self.real_connection.port,
         method=method,
         path=url,
         body=body,
         headers=headers or {}
     )
     log.debug('Got {0}'.format(self._vcr_request))
开发者ID:ghotiv,项目名称:vcrpy,代码行数:12,代码来源:__init__.py

示例7: putrequest

 def putrequest(self, method, url, *args, **kwargs):
     """
     httplib gives you more than one way to do it.  This is a way
     to start building up a request.  Usually followed by a bunch
     of putheader() calls.
     """
     self._vcr_request = Request(
         method=method,
         uri=self._uri(url),
         body="",
         headers={}
     )
     log.debug('Got {0}'.format(self._vcr_request))
开发者ID:hartsock,项目名称:vcrpy,代码行数:13,代码来源:__init__.py

示例8: deserialize

def deserialize(cassette_string, serializer):
    try:
        data = serializer.deserialize(cassette_string)
    # Old cassettes used to use yaml object thingy so I have to
    # check for some fairly stupid exceptions here
    except (ImportError, yaml.constructor.ConstructorError):
        _warn_about_old_cassette_format()
    if _looks_like_an_old_cassette(data):
        _warn_about_old_cassette_format()

    requests = [Request._from_dict(r['request']) for r in data['interactions']]
    responses = [
        compat.convert_to_bytes(r['response']) for r in data['interactions']
    ]
    return requests, responses
开发者ID:JanLikar,项目名称:vcrpy,代码行数:15,代码来源:serialize.py

示例9: putrequest

 def putrequest(self, method, url, *args, **kwargs):
     """
     httplib gives you more than one way to do it.  This is a way
     to start building up a request.  Usually followed by a bunch
     of putheader() calls.
     """
     self._vcr_request = Request(
         protocol=self._protocol,
         host=self.real_connection.host,
         port=self.real_connection.port,
         method=method,
         path=url,
         body="",
         headers={}
     )
开发者ID:aah,项目名称:vcrpy,代码行数:15,代码来源:__init__.py

示例10: deserialize

def deserialize(cassette_string):
    data = json.loads(cassette_string)
    requests = [Request._from_dict(r['request']) for r in data]
    responses = [_fix_response_unicode(r['response']) for r in data]
    return requests, responses
开发者ID:VerosK,项目名称:vcrpy,代码行数:5,代码来源:jsonserializer.py

示例11: VCRConnection

class VCRConnection(object):
    # A reference to the cassette that's currently being patched in
    cassette = None

    def _port_postfix(self):
        """
        Returns empty string for the default port and ':port' otherwise
        """
        port = self.real_connection.port
        default_port = {'https': 433, 'http': 80}[self._protocol]
        return ':{0}'.format(port) if port != default_port else ''

    def _uri(self, url):
        """Returns request absolute URI"""
        uri = "{0}://{1}{2}{3}".format(
            self._protocol,
            self.real_connection.host,
            self._port_postfix(),
            url,
        )
        return uri

    def _url(self, uri):
        """Returns request selector url from absolute URI"""
        prefix = "{0}://{1}{2}".format(
            self._protocol,
            self.real_connection.host,
            self._port_postfix(),
        )
        return uri.replace(prefix, '', 1)

    def request(self, method, url, body=None, headers=None):
        '''Persist the request metadata in self._vcr_request'''
        self._vcr_request = Request(
            method=method,
            uri=self._uri(url),
            body=body,
            headers=headers or {}
        )
        log.debug('Got {0}'.format(self._vcr_request))

        # Note: The request may not actually be finished at this point, so
        # I'm not sending the actual request until getresponse().  This
        # allows me to compare the entire length of the response to see if it
        # exists in the cassette.

    def putrequest(self, method, url, *args, **kwargs):
        """
        httplib gives you more than one way to do it.  This is a way
        to start building up a request.  Usually followed by a bunch
        of putheader() calls.
        """
        self._vcr_request = Request(
            method=method,
            uri=self._uri(url),
            body="",
            headers={}
        )
        log.debug('Got {0}'.format(self._vcr_request))

    def putheader(self, header, *values):
        for value in values:
            self._vcr_request.add_header(header, value)

    def send(self, data):
        '''
        This method is called after request(), to add additional data to the
        body of the request.  So if that happens, let's just append the data
        onto the most recent request in the cassette.
        '''
        self._vcr_request.body = (self._vcr_request.body or '') + data

    def close(self):
        # Note: the real connection will only close if it's open, so
        # no need to check that here.
        self.real_connection.close()

    def endheaders(self, *args, **kwargs):
        """
        Normally, this would atually send the request to the server.
        We are not sending the request until getting the response,
        so bypass this method for now.
        """
        pass

    def getresponse(self, _=False):
        '''Retrieve the response'''
        # Check to see if the cassette has a response for this request. If so,
        # then return it
        if self.cassette.can_play_response_for(self._vcr_request):
            log.info(
                "Playing response for {0} from cassette".format(
                    self._vcr_request
                )
            )
            response = self.cassette.play_response(self._vcr_request)
            return VCRHTTPResponse(response)
        else:
            if self.cassette.write_protected and self.cassette.filter_request(self._vcr_request):
                raise CannotOverwriteExistingCassetteException(
#.........这里部分代码省略.........
开发者ID:hartsock,项目名称:vcrpy,代码行数:101,代码来源:__init__.py

示例12: test_remove_all_json_post_data_parameters

def test_remove_all_json_post_data_parameters():
    body = b'{"id": "secret", "foo": "bar"}'
    request = Request('POST', 'http://google.com', body, {})
    request.headers['Content-Type'] = 'application/json'
    replace_post_data_parameters(request, [('id', None), ('foo', None)])
    assert request.body == b'{}'
开发者ID:Bjwebb,项目名称:vcrpy,代码行数:6,代码来源:test_filters.py

示例13: test_remove_nonexistent_json_post_data_parameters

def test_remove_nonexistent_json_post_data_parameters():
    body = b'{}'
    request = Request('POST', 'http://google.com', body, {})
    request.headers['Content-Type'] = 'application/json'
    remove_post_data_parameters(request, ['id'])
    assert request.body == b'{}'
开发者ID:jayvdb,项目名称:vcrpy,代码行数:6,代码来源:test_filters.py

示例14: deserialize

def deserialize(cassette_string):
    data = json.loads(cassette_string)
    requests = [Request._from_dict(r['request']) for r in data]
    responses = [compat.convert_to_bytes(r['response']) for r in data]
    return requests, responses
开发者ID:ghotiv,项目名称:vcrpy,代码行数:5,代码来源:jsonserializer.py

示例15: test_remove_all_json_post_data_parameters

def test_remove_all_json_post_data_parameters():
    body = b'{"id": "secret", "foo": "bar"}'
    request = Request('POST', 'http://google.com', body, {})
    request.add_header('Content-Type', 'application/json')
    remove_post_data_parameters(request, ['id', 'foo'])
    assert request.body == b'{}'
开发者ID:IvanMalison,项目名称:vcrpy,代码行数:6,代码来源:test_filters.py


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