示例#1
0
    def test_sessions_simultaneous(self):
        def request_callback(request, uri, headers):
            received_cookie_value = request.headers.get('cookie', None)

            if received_cookie_value is not None:
                return 200, headers, 'Cookie %s received' % received_cookie_value
            else:
                return 200, headers, 'Cookie not sent'

        httpretty.register_uri(httpretty.GET,
                               self.URL_SEND_COOKIE + '1',
                               adding_headers={'Set-Cookie': '11111111'})

        httpretty.register_uri(httpretty.GET,
                               self.URL_SEND_COOKIE + '2',
                               adding_headers={'Set-Cookie': '222222222'})

        httpretty.register_uri(httpretty.GET,
                               self.URL_CHECK_COOKIE,
                               body=request_callback)

        uri_opener = ExtendedUrllib()
        session_1 = uri_opener.get_new_session()
        session_2 = uri_opener.get_new_session()

        uri_opener.GET(URL(self.URL_SEND_COOKIE + '1'), session=session_1)
        uri_opener.GET(URL(self.URL_SEND_COOKIE + '2'), session=session_2)

        resp = uri_opener.GET(URL(self.URL_CHECK_COOKIE), session=session_1)
        self.assertIn('Cookie 11111111 received', resp.body)

        resp = uri_opener.GET(URL(self.URL_CHECK_COOKIE), session=session_2)
        self.assertIn('Cookie 222222222 received', resp)
示例#2
0
    def test_sessions_basic(self):
        httpretty.register_uri(
            httpretty.GET,
            self.URL_SEND_COOKIE,
            adding_headers={'Set-Cookie': self.COOKIE_VALUE})

        httpretty.register_uri(httpretty.GET,
                               self.URL_CHECK_COOKIE,
                               body=self.request_callback)

        uri_opener = ExtendedUrllib()
        session_1 = uri_opener.get_new_session()
        session_2 = uri_opener.get_new_session()

        uri_opener.GET(URL(self.URL_SEND_COOKIE), session=session_1)

        resp = uri_opener.GET(URL(self.URL_CHECK_COOKIE),
                              cookies=True,
                              session=session_1)
        self.assertIn('Cookie received', resp)

        resp = uri_opener.GET(URL(self.URL_CHECK_COOKIE),
                              cookies=True,
                              session=session_2)
        self.assertIn('Cookie not sent', resp)