Ejemplo n.º 1
0
    def test_failed_request(self):
        error = AssertionError('Mock application failure SIMULATION')
        request_id = 10
        request = [
            Record(FCGI_BEGIN_REQUEST, pack_begin_request(FCGI_RESPONDER, 0),
                   request_id),
            Record(FCGI_PARAMS, '', request_id),
            Record(FCGI_STDIN, '', request_id),
        ]
        response = self._handle_one_request(request_id,
                                            request,
                                            app=app(exception=error))
        assert response.stdout.eof_received
        headers, body = response.parse()
        assert headers.get('Status', '').startswith('500 ')

        request_id = 11
        request = [
            Record(FCGI_BEGIN_REQUEST, pack_begin_request(FCGI_RESPONDER, 0),
                   request_id),
            Record(FCGI_PARAMS, '', request_id),
            Record(FCGI_STDIN, '', request_id),
        ]
        response = self._handle_one_request(request_id,
                                            request,
                                            app=app(delay=1, exception=error))
Ejemplo n.º 2
0
    def test_keep_conn(self):
        data = binary_data()
        requests = [
            # keep "connection" open
            Record(FCGI_BEGIN_REQUEST,
                   pack_begin_request(FCGI_RESPONDER, FCGI_KEEP_CONN), 3),
            Record(FCGI_PARAMS, pack_env(REQUEST_METHOD='POST'), 3),
            Record(FCGI_PARAMS, '', 3),
            Record(FCGI_STDIN, data, 3),
            Record(FCGI_STDIN, '', 3),
        ]

        # following requests should be served too
        for request_id in (4, 44, 444):
            requests += [
                Record(FCGI_BEGIN_REQUEST,
                       pack_begin_request(FCGI_RESPONDER, 0), request_id),
                Record(FCGI_PARAMS, pack_env(REQUEST_METHOD='POST'),
                       request_id),
                Record(FCGI_PARAMS, '', request_id),
                Record(FCGI_STDIN, data, request_id),
                Record(FCGI_STDIN, '', request_id),
            ]
        for response in self._handle_requests((3, 4, 44, 444), requests):
            assert response.request_status == FCGI_REQUEST_COMPLETE
    def test_request_multiplexing(self):
        req_id = next_req_id()
        req_id_2 = next_req_id()
        req_id_3 = next_req_id()
        role = FCGI_RESPONDER
        flags = 0
        records = (
            (FCGI_BEGIN_REQUEST, pack_begin_request(role, flags), req_id),
            (FCGI_PARAMS, pack_env(), req_id),
            (FCGI_BEGIN_REQUEST, pack_begin_request(role, flags), req_id_2),
            (FCGI_BEGIN_REQUEST, pack_begin_request(role, flags), req_id_3),
            (FCGI_PARAMS, pack_env(), req_id_3),
            (FCGI_PARAMS, pack_env(), req_id_2),
            (FCGI_PARAMS, '', req_id_2),
            (FCGI_PARAMS, '', req_id),
            (FCGI_ABORT_REQUEST, '', req_id_3),
        )

        handler = run_handler(records, role=role)

        assert not handler.requests
        assert handler.request_handler.call_count == 2
        assert handler.conn.close.called

        for r_id in req_id, req_id_2, req_id_3:
            rec = find_rec(handler, FCGI_END_REQUEST, r_id)
            assert rec and unpack_end_request(rec.content)

            for stream in FCGI_STDOUT, FCGI_STDERR:
                assert '' == read_stream(handler, stream, r_id)
Ejemplo n.º 4
0
    def test_keep_conn(self):
        data = binary_data()
        requests = [
            # keep "connection" open
            Record(FCGI_BEGIN_REQUEST, pack_begin_request(
                   FCGI_RESPONDER, FCGI_KEEP_CONN), 3),
            Record(FCGI_PARAMS, pack_env(REQUEST_METHOD='POST'), 3),
            Record(FCGI_PARAMS, '', 3),
            Record(FCGI_STDIN, data, 3),
            Record(FCGI_STDIN, '', 3),
        ]

        # following requests should be served too
        for request_id in (4, 44, 444):
            requests += [
                Record(FCGI_BEGIN_REQUEST, pack_begin_request(
                       FCGI_RESPONDER, 0), request_id),
                Record(FCGI_PARAMS,
                       pack_env(REQUEST_METHOD='POST'), request_id),
                Record(FCGI_PARAMS, '', request_id),
                Record(FCGI_STDIN, data, request_id),
                Record(FCGI_STDIN, '', request_id),
            ]
        for response in self._handle_requests((3, 4, 44, 444), requests):
            assert response.request_status == FCGI_REQUEST_COMPLETE
    def test_abort_request(self):
        req_id = next_req_id()
        role = FCGI_RESPONDER
        flags = 0
        records = (
            (FCGI_BEGIN_REQUEST, pack_begin_request(role, flags), req_id),
            (FCGI_PARAMS, pack_env(), req_id),
            (FCGI_PARAMS, '', req_id),
            # request_handler gets spawned after PARAMS is "closed"
            # lets give it a chance to run
            0.1,
            # then abort it
            (FCGI_ABORT_REQUEST, '', req_id),
        )

        # use request_handler that waits on STDIN so we can abort
        # it while it's running
        handler = run_handler(records, role=role,
                              request_handler=copy_stdin_to_stdout)

        rec = find_rec(handler, FCGI_END_REQUEST, req_id)
        assert rec and unpack_end_request(rec.content)

        for stream in FCGI_STDOUT, FCGI_STDERR:
            assert '' == read_stream(handler, stream, req_id)
Ejemplo n.º 6
0
 def test_abort_request(self):
     request_id = 6
     request = [
         Record(FCGI_BEGIN_REQUEST, pack_begin_request(FCGI_RESPONDER, 0),
                request_id),
         Record(FCGI_ABORT_REQUEST, '', request_id),
     ]
     response = self._handle_one_request(request_id, request)
     assert response.request_status == FCGI_REQUEST_COMPLETE
Ejemplo n.º 7
0
 def test_abort_request(self):
     request_id = 6
     request = [
         Record(FCGI_BEGIN_REQUEST, pack_begin_request(
                FCGI_RESPONDER, 0), request_id),
         Record(FCGI_ABORT_REQUEST, '', request_id),
     ]
     response = self._handle_one_request(request_id, request)
     assert response.request_status == FCGI_REQUEST_COMPLETE
Ejemplo n.º 8
0
 def test_wrong_role(self):
     request_id = 5
     request = [
         Record(FCGI_BEGIN_REQUEST, pack_begin_request(
                FCGI_RESPONDER, 0), request_id),
     ]
     response = self._handle_one_request(
         request_id, request, role=FCGI_FILTER)
     assert response.request_status == FCGI_UNKNOWN_ROLE
Ejemplo n.º 9
0
 def test_wrong_role(self):
     request_id = 5
     request = [
         Record(FCGI_BEGIN_REQUEST, pack_begin_request(FCGI_RESPONDER, 0),
                request_id),
     ]
     response = self._handle_one_request(request_id,
                                         request,
                                         role=FCGI_FILTER)
     assert response.request_status == FCGI_UNKNOWN_ROLE
Ejemplo n.º 10
0
 def test_authorizer(self):
     request_id = 13
     request = [
         Record(FCGI_BEGIN_REQUEST,
                pack_begin_request(FCGI_AUTHORIZER, 0), request_id),
         Record(FCGI_PARAMS, pack_env(), request_id),
         Record(FCGI_PARAMS, '', request_id),
     ]
     response = self._handle_one_request(
         request_id, request, role=FCGI_AUTHORIZER, app=app(response=''))
     assert response.request_status == FCGI_REQUEST_COMPLETE
Ejemplo n.º 11
0
 def test_authorizer(self):
     request_id = 13
     request = [
         Record(FCGI_BEGIN_REQUEST, pack_begin_request(FCGI_AUTHORIZER, 0),
                request_id),
         Record(FCGI_PARAMS, pack_env(), request_id),
         Record(FCGI_PARAMS, '', request_id),
     ]
     response = self._handle_one_request(request_id,
                                         request,
                                         role=FCGI_AUTHORIZER,
                                         app=app(response=''))
     assert response.request_status == FCGI_REQUEST_COMPLETE
Ejemplo n.º 12
0
 def test_responder(self):
     request_id = 1
     request = (
         Record(FCGI_BEGIN_REQUEST,
                pack_begin_request(FCGI_RESPONDER, 0), request_id),
         Record(FCGI_PARAMS, pack_env(REQUEST_METHOD='POST', HTTPS='yes'),
                request_id),
         Record(FCGI_PARAMS, '', request_id),
         Record(FCGI_STDIN, binary_data(), request_id),
         Record(FCGI_STDIN, '', request_id),
     )
     response = self._handle_one_request(request_id, request)
     assert response.request_status == FCGI_REQUEST_COMPLETE
Ejemplo n.º 13
0
 def test_empty_response(self):
     request_id = 12
     request = [
         Record(FCGI_BEGIN_REQUEST, pack_begin_request(
                FCGI_RESPONDER, 0), request_id),
         Record(FCGI_PARAMS, '', request_id),
         Record(FCGI_STDIN, '', request_id),
     ]
     response = self._handle_one_request(request_id, request,
                                         app=app(response=''))
     assert response.stdout.eof_received
     headers, body = response.parse()
     assert len(body) == 0, repr(body)
Ejemplo n.º 14
0
 def test_multiplexer(self):
     data = binary_data()
     requests = [
         Record(FCGI_BEGIN_REQUEST, pack_begin_request(
                FCGI_RESPONDER, 0), 8),
         Record(FCGI_PARAMS, pack_env(REQUEST_METHOD='POST'), 8),
         Record(FCGI_BEGIN_REQUEST, pack_begin_request(
                FCGI_RESPONDER, 0), 9),
         Record(FCGI_PARAMS, pack_env(REQUEST_METHOD='POST'), 9),
         Record(FCGI_PARAMS, '', 9),
         Record(FCGI_PARAMS, '', 8),
         Record(FCGI_STDIN, data, 9),
         Record(FCGI_STDIN, data, 8),
         Record(FCGI_STDIN, '', 9),
         Record(FCGI_STDIN, '', 8),
     ]
     for response in self._handle_requests((8, 9), requests):
         assert response.request_status == FCGI_REQUEST_COMPLETE
         assert response.stdout.eof_received
         headers, body = response.parse()
         assert headers.get('Status') == '200 OK', repr(headers)
         assert body == data
Ejemplo n.º 15
0
 def test_multiplexer(self):
     data = binary_data()
     requests = [
         Record(FCGI_BEGIN_REQUEST, pack_begin_request(FCGI_RESPONDER, 0),
                8),
         Record(FCGI_PARAMS, pack_env(REQUEST_METHOD='POST'), 8),
         Record(FCGI_BEGIN_REQUEST, pack_begin_request(FCGI_RESPONDER, 0),
                9),
         Record(FCGI_PARAMS, pack_env(REQUEST_METHOD='POST'), 9),
         Record(FCGI_PARAMS, '', 9),
         Record(FCGI_PARAMS, '', 8),
         Record(FCGI_STDIN, data, 9),
         Record(FCGI_STDIN, data, 8),
         Record(FCGI_STDIN, '', 9),
         Record(FCGI_STDIN, '', 8),
     ]
     for response in self._handle_requests((8, 9), requests):
         assert response.request_status == FCGI_REQUEST_COMPLETE
         assert response.stdout.eof_received
         headers, body = response.parse()
         assert headers.get('Status') == '200 OK', repr(headers)
         assert body == data
Ejemplo n.º 16
0
 def test_filter(self):
     request_id = 2
     request = [
         Record(FCGI_BEGIN_REQUEST,
                pack_begin_request(FCGI_FILTER, 0), request_id),
         Record(FCGI_PARAMS, pack_env(), request_id),
         Record(FCGI_PARAMS, '', request_id),
         Record(FCGI_STDIN, '', request_id),
         Record(FCGI_DATA, '', request_id),
     ]
     response = self._handle_one_request(
         request_id, request, role=FCGI_FILTER)
     assert response.request_status == FCGI_REQUEST_COMPLETE
Ejemplo n.º 17
0
 def test_responder(self):
     request_id = 1
     request = (
         Record(FCGI_BEGIN_REQUEST, pack_begin_request(FCGI_RESPONDER, 0),
                request_id),
         Record(FCGI_PARAMS, pack_env(REQUEST_METHOD='POST', HTTPS='yes'),
                request_id),
         Record(FCGI_PARAMS, '', request_id),
         Record(FCGI_STDIN, binary_data(), request_id),
         Record(FCGI_STDIN, '', request_id),
     )
     response = self._handle_one_request(request_id, request)
     assert response.request_status == FCGI_REQUEST_COMPLETE
Ejemplo n.º 18
0
 def test_filter(self):
     request_id = 2
     request = [
         Record(FCGI_BEGIN_REQUEST, pack_begin_request(FCGI_FILTER, 0),
                request_id),
         Record(FCGI_PARAMS, pack_env(), request_id),
         Record(FCGI_PARAMS, '', request_id),
         Record(FCGI_STDIN, '', request_id),
         Record(FCGI_DATA, '', request_id),
     ]
     response = self._handle_one_request(request_id,
                                         request,
                                         role=FCGI_FILTER)
     assert response.request_status == FCGI_REQUEST_COMPLETE
Ejemplo n.º 19
0
 def test_empty_response(self):
     request_id = 12
     request = [
         Record(FCGI_BEGIN_REQUEST, pack_begin_request(FCGI_RESPONDER, 0),
                request_id),
         Record(FCGI_PARAMS, '', request_id),
         Record(FCGI_STDIN, '', request_id),
     ]
     response = self._handle_one_request(request_id,
                                         request,
                                         app=app(response=''))
     assert response.stdout.eof_received
     headers, body = response.parse()
     assert len(body) == 0, repr(body)
Ejemplo n.º 20
0
    def test_failed_request(self):
        error = AssertionError('Mock application failure SIMULATION')
        request_id = 10
        request = [
            Record(FCGI_BEGIN_REQUEST, pack_begin_request(
                   FCGI_RESPONDER, 0), request_id),
            Record(FCGI_PARAMS, '', request_id),
            Record(FCGI_STDIN, '', request_id),
        ]
        response = self._handle_one_request(request_id, request,
                                            app=app(exception=error))
        assert response.stdout.eof_received
        headers, body = response.parse()
        assert headers.get('Status', '').startswith('500 ')

        request_id = 11
        request = [
            Record(FCGI_BEGIN_REQUEST, pack_begin_request(
                   FCGI_RESPONDER, 0), request_id),
            Record(FCGI_PARAMS, '', request_id),
            Record(FCGI_STDIN, '', request_id),
        ]
        response = self._handle_one_request(request_id, request,
                                            app=app(delay=1, exception=error))