コード例 #1
0
 def test_copy(self):
     all_pairs = [('A', '1'), ('A', '2'), ('B', 'c')]
     h1 = HTTPHeaders()
     for k, v in all_pairs:
         h1.add(k, v)
     h2 = h1.copy()
     h3 = copy.copy(h1)
     h4 = copy.deepcopy(h1)
     for headers in [h1, h2, h3, h4]:
         # All the copies are identical, no matter how they were
         # constructed.
         self.assertEqual(list(sorted(headers.get_all())), all_pairs)
     for headers in [h2, h3, h4]:
         # Neither the dict or its member lists are reused.
         self.assertIsNot(headers, h1)
         self.assertIsNot(headers.get_list('A'), h1.get_list('A'))
コード例 #2
0
 def test_copy(self):
     all_pairs = [('A', '1'), ('A', '2'), ('B', 'c')]
     h1 = HTTPHeaders()
     for k, v in all_pairs:
         h1.add(k, v)
     h2 = h1.copy()
     h3 = copy.copy(h1)
     h4 = copy.deepcopy(h1)
     for headers in [h1, h2, h3, h4]:
         # All the copies are identical, no matter how they were
         # constructed.
         self.assertEqual(list(sorted(headers.get_all())), all_pairs)
     for headers in [h2, h3, h4]:
         # Neither the dict or its member lists are reused.
         self.assertIsNot(headers, h1)
         self.assertIsNot(headers.get_list('A'), h1.get_list('A'))
コード例 #3
0
class PreparedRequest(_PreparedRequest):
    """The fully mutable :class:`PreparedRequest <PreparedRequest>` object,
    containing the exact bytes that will be sent to the server and combine
    one to one to a single actual request

    Generated from either a :class:`Request <Request>` object or manually.
    The only difference matters is param headers is a
    `tornado.httputil.HTTPHeaders` object.

    Usage::

      >>> import trip
      >>> req = trip.Request('GET', 'http://httpbin.org/get')
      >>> r = req.prepare()
      <PreparedRequest [GET]>

      >>> s = trip.Session()
      >>> s.send(r)
      <Response [200]>
    """
    def __init__(self):
        _PreparedRequest.__init__(self)
        self.headers = default_headers()
        self.ssl_options = None

        self.host = None
        self.port = None
        self.af = None
        self.decompress = None
        self.start_line = None

    def prepare(self,
                method=None,
                url=None,
                headers=None,
                files=None,
                data=None,
                params=None,
                auth=None,
                cookies=None,
                hooks=None,
                json=None):
        """Prepares the entire request with the given parameters."""

        _PreparedRequest.prepare(self, method, url, headers, files, data,
                                 params, auth, cookies, hooks, json)
        self.adapt_prepare()

    def adapt_prepare(self):
        """Prepares the special trip parameters."""
        parsed = urlsplit(self.url)
        if parsed.scheme not in ('http', 'https'):
            raise ValueError('Unsupported url scheme: %s' % self.url)

        self.host, self.port = get_host_and_port(self.url)

        self.af = AF_INET

        self.decompress = 'gzip' in \
            self.headers.get('Accept-Encoding', '')

        req_path = ((parsed.path or '/') +
                    (('?' + parsed.query) if parsed.query else ''))
        self.start_line = RequestStartLine(self.method, req_path, '')

        self.headers = HTTPHeaders(self.headers)

        if 'Connection' not in self.headers:
            self.headers['Connection'] = 'close'
        if 'Host' not in self.headers:
            self.headers['Host'] = self.host

    def copy(self):
        p = PreparedRequest()
        p.method = self.method
        p.url = self.url
        p.headers = self.headers.copy() if self.headers is not None else None
        p._cookies = _copy_cookie_jar(self._cookies)
        p.body = self.body
        p.hooks = self.hooks
        p._body_position = self._body_position
        return p