Exemplo n.º 1
0
 def setUp(self):
     session = HttpSession(http_method='GET', http_url='http://localhost:7010/console')
     logger = MockLogger()
     self.requestor = HttpRequestor(session, None, logger)
Exemplo n.º 2
0
def make_session_request(session_id, url=None, follow_redirects=False, **kwargs):
    
        session = HttpSession.objects.get(pk=session_id)
        logger = make_session_request.get_logger(**kwargs)    
        logger.info("Starting request for session %s" % session.id)

        requestor = HttpRequestor(session, url, logger)
        
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        if not requestor.start_request(sock):
            return False

        logger.info("Sent request, waiting for response.")
        
        requestor.read_response_headers()
        
        is_redirect, location = requestor.is_redirect_to_location()
        if is_redirect and follow_redirects:
        
            requestor.close()
        
            if location:
                logger.info("Redirected to %s" % location)
            
                HttpRedirect(url=location, session=session).save()
                make_session_request.delay(session.id, location, follow_redirects)
                
                return True
            else:
                logger.error("Response was a redirect, but no Location header was given")
                return False
        
        requestor.read_body()       
        requestor.close()
                
        session.http_response = requestor.complete_response_text()
        session.time_completed = datetime.datetime.now()
        session.save()
        return True
Exemplo n.º 3
0
class HttpRequestorTest(TestCase):
    
    def setUp(self):
        session = HttpSession(http_method='GET', http_url='http://localhost:7010/console')
        logger = MockLogger()
        self.requestor = HttpRequestor(session, None, logger)
    
    def test_parsing_simple_response_headers(self):
        sock = MockSocket(response_data='HTTP/1.1 200 OK\r\nServer: nginx\r\nDate: Sat, 26 Jun 2010 04:03:37 GMT\r\nContent-Type: application/json; charset=utf-8\r\n\r\n')

        self.requestor.start_request(sock)
        self.requestor.read_response_headers()
        
        self.failUnlessEqual(self.requestor.header_value('server'), 'nginx')
        self.failUnlessEqual(self.requestor.header_value('content-type'), 'application/json; charset=utf-8')
        self.failUnlessEqual(self.requestor.header_value('date'), 'Sat, 26 Jun 2010 04:03:37 GMT')


    def test_parsing_redirect_response(self):
        sock = MockSocket(response_data='HTTP/1.0 307 Temporary Redirect\r\nServer: nginx\r\nDate: Sat, 26 Jun 2010 04:03:37 GMT\r\nLocation: http://news.ycombinator.com/\r\n\r\n')

        self.requestor.start_request(sock)
        self.requestor.read_response_headers()
        
        is_redirect, location = self.requestor.is_redirect_to_location()
        
        self.assertTrue(is_redirect)
        self.failUnlessEqual(location, 'http://news.ycombinator.com/')

    def test_reading_response_with_content_length(self):
        sock = MockSocket(response_data='HTTP/1.0 200 OK\r\nServer: nginx\r\nDate: Sat, 26 Jun 2010 04:03:37 GMT\r\nContent-length: 4\r\n\r\nGoodBad')

        self.requestor.start_request(sock)
        self.requestor.read_response_headers()
        self.requestor.read_body()
        
        response = self.requestor.raw_response
        
        self.failUnlessEqual(response, 'Good')

    def test_reading_response_with_not_enough_content(self):
        sock = MockSocket(response_data='HTTP/1.0 200 OK\r\nServer: nginx\r\nDate: Sat, 26 Jun 2010 04:03:37 GMT\r\nContent-length: 40\r\n\r\nNotQuiteEnough')

        self.requestor.start_request(sock)
        self.requestor.read_response_headers()
        self.requestor.read_body()
        
        response = self.requestor.raw_response
        
        self.failUnlessEqual(response, 'NotQuiteEnough')
        
    def test_reading_response_with_no_content_length(self):
        sock = MockSocket(response_data='HTTP/1.0 200 OK\r\nServer: nginx\r\nDate: Sat, 26 Jun 2010 04:03:37 GMT\r\n\r\nThis content has no specified length.')

        self.requestor.start_request(sock)
        self.requestor.read_response_headers()
        self.requestor.read_body()
        
        response = self.requestor.raw_response
        
        self.failUnlessEqual(response, 'This content has no specified length.')