當前位置: 首頁>>代碼示例>>Python>>正文


Python Stub.response_body方法代碼示例

本文整理匯總了Python中stubo.model.stub.Stub.response_body方法的典型用法代碼示例。如果您正苦於以下問題:Python Stub.response_body方法的具體用法?Python Stub.response_body怎麽用?Python Stub.response_body使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在stubo.model.stub.Stub的用法示例。


在下文中一共展示了Stub.response_body方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: insert_stub

# 需要導入模塊: from stubo.model.stub import Stub [as 別名]
# 或者: from stubo.model.stub.Stub import response_body [as 別名]
 def insert_stub(self, doc, stateful):
     from stubo.model.stub import Stub
     matchers = doc['stub'].contains_matchers()
     scenario = doc['scenario']
     stubs_cursor = self.get_stubs(scenario)
     if stubs_cursor.count():
         for stub in stubs_cursor:
             the_stub = Stub(stub['stub'], scenario)
             if matchers and matchers == the_stub.contains_matchers():
                 if not stateful and \
                     doc['stub'].response_body() == the_stub.response_body():
                     msg = 'duplicate stub found, not inserting.'
                     log.warn(msg)
                     return msg
                 log.debug('In scenario: {0} found exact match for matchers:'
                   ' {1}. Perform stateful update of stub.'.format(scenario,
                                                                   matchers))
                 response = the_stub.response_body()
                 response.extend(doc['stub'].response_body())
                 the_stub.set_response_body(response)   
                 self.db.scenario_stub.update(
                     {'_id': ObjectId(stub['_id'])},
                     {'$set' : {'stub' : the_stub.payload}})
                 return 'updated with stateful response'
     doc['stub'] = doc['stub'].payload       
     status = self.db.scenario_stub.insert(doc)
     return 'inserted scenario_stub: {0}'.format(status)
開發者ID:erowan,項目名稱:stubo-app,代碼行數:29,代碼來源:db.py

示例2: insert_stub

# 需要導入模塊: from stubo.model.stub import Stub [as 別名]
# 或者: from stubo.model.stub.Stub import response_body [as 別名]
    def insert_stub(self, doc, stateful):
        """
        Insert stub into DB. Performs a check whether this stub already exists in database or not.  If it exists
        and stateful is True - new response is appended to the response list, else - reports that duplicate stub
        found and it will not be inserted.

        :param doc: Stub class with Stub that will be inserted
        :param stateful: <boolean> specify whether stub insertion should be stateful or not
        :return: <string> message with insertion status:
           ignored - if not stateful and stub was already present
           updated - if stateful and stub was already present
           created - if stub was not present in database
        """
        # getting initial values - stub matchers, scenario name
        matchers = doc["stub"].contains_matchers()
        scenario = doc["scenario"]

        matchers_hash = self._create_hash(matchers)
        # check if we have matchers - should be None for REST calls
        if matchers is not None:
            # additional helper value for indexing
            doc["matchers_hash"] = matchers_hash
            matched_stub = self.get_matched_stub(name=scenario, matchers_hash=matchers_hash)
            # checking if stub already exists
            if matched_stub:
                # creating stub object from found document
                the_stub = Stub(matched_stub["stub"], scenario)
                if not stateful and doc["stub"].response_body() == the_stub.response_body():
                    msg = "duplicate stub found, not inserting."
                    log.warn(msg)
                    result = {"status": "ignored", "msg": msg, "key": str(matched_stub["_id"])}
                    return result
                # since stateful is true - updating stub body by extending the list
                log.debug(
                    "In scenario: {0} found exact match for matchers:"
                    " {1}. Perform stateful update of stub.".format(scenario, matchers)
                )
                response = the_stub.response_body()
                response.extend(doc["stub"].response_body())
                the_stub.set_response_body(response)
                # updating Stub body and size, writing to database
                self.db.scenario_stub.update(
                    {"_id": matched_stub["_id"]},
                    {"$set": {"stub": the_stub.payload, "space_used": len(unicode(the_stub.payload))}},
                )
                result = {"status": "updated", "msg": "Updated with stateful response", "key": str(matched_stub["_id"])}
                return result

        # inserting stub into DB
        status = self.db.scenario_stub.insert(self.get_stub_document(doc))

        result = {"status": "created", "msg": "Inserted scenario_stub", "key": str(status)}
        return result
開發者ID:rusenask,項目名稱:mirage,代碼行數:55,代碼來源:db.py

示例3: test_put_stub

# 需要導入模塊: from stubo.model.stub import Stub [as 別名]
# 或者: from stubo.model.stub.Stub import response_body [as 別名]
    def test_put_stub(self):
        self.http_client.fetch(self.get_url('/stubo/api/begin/session?scenario=xslt&session=s1&mode=record'), self.stop)
        response = self.wait()
        self.assertEqual(response.code, 200)
        self.http_client.fetch(self.get_url('/stubo/api/put/module?name=/static/cmds/tests/ext/xslt/mangler.py'),
                               self.stop)
        response = self.wait()
        self.assertEqual(response.code, 200)

        self.http_client.fetch(self.get_url('/stubo/api/put/stub?session=s1&ext_module=mangler'),
                               callback=self.stop,
                               method="POST",
                               body="""||textMatcher||<request><a>ba</a><trans_id>1234</trans_id><b>ba</b><user uuid="xxx">joe</user><dt>2013-11-25</dt></request>||response||<response><a>ba</a><trans_id>1234</trans_id><b>ba</b><user uuid="xxx">joe</user><dt>2013-11-25</dt></response>""")
        response = self.wait()
        self.assertEqual(response.code, 200)
        from stubo.model.db import Scenario
        scenario_db = Scenario(db=self.db)
        stubs = list(scenario_db.get_stubs('localhost:xslt'))
        self.assertEqual(len(stubs), 1)
        from stubo.model.stub import Stub
        stub = Stub(stubs[0]['stub'], 'localhost:xslt')
        self.assertEqual(stub.contains_matchers()[0],
                         u'<request><a>ba</a><trans_id>***</trans_id><b>ba</b><user uuid="***">***</user><dt>***</dt></request>\n')
        self.assertEqual(stub.response_body()[0],
                         u'<response><a>ba</a><trans_id>***</trans_id><b>ba</b><user uuid="***">***</user><dt>***</dt></response>\n')
        from datetime import date
        self.assertEqual(stub.module(), {
            u'system_date': str(date.today()),
            u'recorded_system_date': str(date.today()),
            u'name': u'mangler'})
開發者ID:ocoperations,項目名稱:stubo-app,代碼行數:32,代碼來源:test_ext.py

示例4: insert_stub

# 需要導入模塊: from stubo.model.stub import Stub [as 別名]
# 或者: from stubo.model.stub.Stub import response_body [as 別名]
    def insert_stub(self, doc, stateful):
        """
        Insert stub into DB. Performs a check whether this stub already exists in database or not.  If it exists
        and stateful is True - new response is appended to the response list, else - reports that duplicate stub
        found and it will not be inserted.

        :param doc: Stub class with Stub that will be inserted
        :param stateful: <boolean> specify whether stub insertion should be stateful or not
        :return: <string> message with insertion status
        """
        matchers = doc['stub'].contains_matchers()
        scenario = doc['scenario']
        stubs_cursor = self.get_stubs(scenario)
        if stubs_cursor.count():
            for stub in stubs_cursor:
                the_stub = Stub(stub['stub'], scenario)
                if matchers and matchers == the_stub.contains_matchers():
                    if not stateful and \
                        doc['stub'].response_body() == the_stub.response_body():
                        msg = 'duplicate stub found, not inserting.'
                        log.warn(msg)
                        return msg
                    log.debug('In scenario: {0} found exact match for matchers:'
                      ' {1}. Perform stateful update of stub.'.format(scenario,
                                                                      matchers))
                    response = the_stub.response_body()
                    response.extend(doc['stub'].response_body())
                    the_stub.set_response_body(response)   
                    self.db.scenario_stub.update(
                        {'_id': ObjectId(stub['_id'])},
                        {'$set' : {'stub' : the_stub.payload}})
                    return 'updated with stateful response'
        doc['stub'] = doc['stub'].payload       
        status = self.db.scenario_stub.insert(doc)
        self.db.scenario_stub.create_index([("stub.priority", ASCENDING), ("scenario", ASCENDING)])
        return 'inserted scenario_stub: {0}'.format(status)
開發者ID:JohnFDavenport,項目名稱:stubo-app,代碼行數:38,代碼來源:db.py

示例5: export_stubs_to_commands_format

# 需要導入模塊: from stubo.model.stub import Stub [as 別名]
# 或者: from stubo.model.stub.Stub import response_body [as 別名]
def export_stubs_to_commands_format(handler, scenario_name):
    """
    Exports scenario to .commands file format.
    :param handler:
    :param scenario_name: <string> Scenario name
    :return: :raise exception_response:
    """
    cache = Cache(get_hostname(handler.request))
    scenario_name_key = cache.scenario_key_name(scenario_name)

    # use user arg or epoch time
    session_id = handler.get_argument('session_id', int(time.time()))
    session = u'{0}_{1}'.format(scenario_name, session_id)
    cmds = [
        'delete/stubs?scenario={0}'.format(scenario_name),
        'begin/session?scenario={0}&session={1}&mode=record'.format(
            scenario_name, session)
    ]
    files = []
    scenario = Scenario()
    # get scenario pre stubs for specified scenario
    stubs = list(scenario.get_pre_stubs(scenario_name_key))
    if stubs:
        for i in range(len(stubs)):
            entry = stubs[i]
            stub = Stub(entry['stub'], scenario_name_key)
            # if stub is rest - matcher may be None, checking that
            if stub.contains_matchers() is None:
                cmds.append('# Stub skipped since no matchers were found. Consider using .yaml format for additional '
                            'capabilities')
                # skipping to next stub, this stub is not compatible with .commands format
                continue
            matchers = [('{0}_{1}_{2}.textMatcher'.format(session, i, x), stub.contains_matchers()[x])
                        for x in range(len(stub.contains_matchers()))]
            matchers_str = ",".join(x[0] for x in matchers)
            url_args = stub.args()
            url_args['session'] = session
            module_info = stub.module()
            if module_info:
                # Note: not including put/module in the export, modules are shared
                # by multiple scenarios.
                url_args['ext_module'] = module_info['name']
                url_args['stub_created_date'] = stub.recorded()
                url_args['stubbedSystemDate'] = module_info.get('recorded_system_date')
                url_args['system_date'] = module_info.get('system_date')
            url_args =  urlencode(url_args)
            responses = stub.response_body()
            assert(len(responses) == 1)
            response = responses[0]
            response = ('{0}_{1}.response'.format(session, i), response)
            cmds.append('put/stub?{0},{1},{2}'.format(url_args, matchers_str,
                                                      response[0]))
            files.append(response)
            files.extend(matchers)
    else:
        cmds.append('put/stub?session={0},text=a_dummy_matcher,text=a_dummy_response'.format(session))
    cmds.append('end/session?session={0}'.format(session))

    runnable = asbool(handler.get_argument('runnable', False))
    runnable_info = dict()

    if runnable:
        playback_session = handler.get_argument('playback_session', None)
        if not playback_session:
            raise exception_response(400,
                                     title="'playback_session' argument required with 'runnable")
        runnable_info['playback_session'] = playback_session

        tracker = Tracker()
        last_used = tracker.session_last_used(scenario_name_key,
                                              playback_session, 'playback')
        if not last_used:
            raise exception_response(400,
                                     title="Unable to find playback session")
        runnable_info['last_used'] = dict(remote_ip=last_used['remote_ip'],
                                          start_time=str(last_used['start_time']))
        playback = tracker.get_last_playback(scenario_name, playback_session,
                                             last_used['start_time'])
        playback = list(playback)
        if not playback:
            raise exception_response(400,
                                     title="Unable to find a playback for scenario='{0}', playback_session='{1}'".format(scenario_name, playback_session))

        cmds.append('begin/session?scenario={0}&session={1}&mode=playback'.format(
            scenario_name, session))
        number_of_requests = len(playback)
        runnable_info['number_of_playback_requests'] = number_of_requests
        for nrequest in range(number_of_requests):
            track = playback[nrequest]
            request_text = track.get('request_text')
            if not request_text:
                raise exception_response(400, title='Unable to obtain playback details, was full tracking enabled?')

            request_file_name = '{0}_{1}.request'.format(session, nrequest)
            files.append((request_file_name, request_text))
            stubo_response_text = track['stubo_response']
            if not isinstance(stubo_response_text, basestring):
                stubo_response_text = unicode(stubo_response_text)
            stubo_response_file_name = '{0}_{1}.stubo_response'.format(session, nrequest)
            files.append((stubo_response_file_name, stubo_response_text))
#.........這裏部分代碼省略.........
開發者ID:mithun-kumar,項目名稱:stubo-app,代碼行數:103,代碼來源:export_commands.py

示例6: insert_stub

# 需要導入模塊: from stubo.model.stub import Stub [as 別名]
# 或者: from stubo.model.stub.Stub import response_body [as 別名]
    def insert_stub(self, doc, stateful):
        """
        Insert stub into DB. Performs a check whether this stub already exists in database or not.  If it exists
        and stateful is True - new response is appended to the response list, else - reports that duplicate stub
        found and it will not be inserted.

        :param doc: Stub class with Stub that will be inserted
        :param stateful: <boolean> specify whether stub insertion should be stateful or not
        :return: <string> message with insertion status
        """
        # getting initial values - stub matchers, scenario name
        matchers = doc['stub'].contains_matchers()
        scenario = doc['scenario']

        matchers_hash = self._create_hash(matchers)
        # check if we have matchers - should be None for REST calls
        if matchers is not None:
            # additional helper value for indexing
            doc['matchers_hash'] = matchers_hash
            matched_stub = self.get_matched_stub(name=scenario, matchers_hash=matchers_hash)

            # checking if stub already exists
            if matched_stub:
                # creating stub object from found document
                the_stub = Stub(matched_stub['stub'], scenario)
                if not stateful and doc['stub'].response_body() == the_stub.response_body():
                    msg = 'duplicate stub found, not inserting.'
                    log.warn(msg)
                    return msg
                # since stateful is true - updating stub body by extending the list
                log.debug('In scenario: {0} found exact match for matchers:'
                          ' {1}. Perform stateful update of stub.'.format(scenario, matchers))
                response = the_stub.response_body()
                response.extend(doc['stub'].response_body())
                the_stub.set_response_body(response)
                # updating Stub body and size, writing to database
                self.db.scenario_stub.update(
                    {'_id': matched_stub['_id']},
                    {'$set': {'stub': the_stub.payload,
                              'space_used': len(unicode(the_stub.payload))}})
                return 'updated with stateful response'

        # Stub doesn't exist in DB - preparing new object
        doc['stub'] = doc['stub'].payload

        # additional helper for aggregation framework
        try:
            doc['recorded'] = doc['stub']['recorded']
        except KeyError:
            # during tests "recorded" value is not supplied
            pass
        # calculating stub size
        doc['space_used'] = len(unicode(doc['stub']))

        # inserting stub into DB
        status = self.db.scenario_stub.insert(doc)

        # create indexes
        if matchers_hash:
            self._create_index(key="matchers_hash")
        # creating index for scenario and priority
        self._create_index(key="scenario")
        if 'priority' in doc['stub']:
            # creating index for priority
            self._create_index("stub.priority")

        return 'inserted scenario_stub: {0}'.format(status)
開發者ID:mithun-kumar,項目名稱:stubo-app,代碼行數:69,代碼來源:db.py

示例7: create_session_cache

# 需要導入模塊: from stubo.model.stub import Stub [as 別名]
# 或者: from stubo.model.stub.Stub import response_body [as 別名]
 def create_session_cache(self, scenario_name, session_name, 
                          system_date=None):
     scenario_key = self.scenario_key_name(scenario_name)
     log.debug("create_session_cache: scenario_key={0}, session_name={1}".format(
               scenario_key, session_name))
     session = self.get(scenario_key, session_name)
     if not session:
         # must be using a different session name for playback than record
         session = {
             'session' : session_name,
             'scenario' : scenario_key 
         }
         # add to sessions map
         self.set_raw('{0}:sessions'.format(self.host), session_name, scenario_name) 
                
     session['status'] = 'playback'
     session['system_date'] = system_date or datetime.date.today().strftime(
         '%Y-%m-%d')
     session['last_used'] = datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')
     cache_info = []
     
     # copy mongo scenario stubs to redis cache
     scenario_col = Scenario()   
     stubs_cursor = scenario_col.get_stubs(scenario_key)
     stubs = list(stubs_cursor)
     if not stubs:
         raise exception_response(500,
             title="found no stubs in mongo for {0}".format(scenario_key))
     from stubo.ext.module import Module
     for scenario_stub in stubs:
         stub = Stub(scenario_stub['stub'], scenario_stub['scenario'])
         if stub.module():
             module_name = stub.module()['name']
             # tag this stub with the latest version of the module
             module = Module(self.host)
             version = module.latest_version(module_name)
             if not version:
                 raise exception_response(500,
                     title="module '{0}' not found in cache".format(
                     module.key(module_name)))
             stub.module()['version'] = version
         
         response_ids = []
         response_bodys = stub.response_body()
         # cache each response id -> response (text, status) etc
         for response_text in response_bodys:
             stub.set_response_body(response_text)
             response_id = response_hash(response_text, stub)
             self.set_response(scenario_name, session_name, response_id,
                               stub.response())
             response_ids.append(response_id) 
         
         # replace response text with response hash ids for session cache 
         stub.response().pop('body', None)
         stub.response()['ids'] = response_ids
         delay_policy_name = stub.delay_policy()
         if delay_policy_name:
             # Note: the delay policy is not really cached with the session.
             # The get/response call will just use the name to get the latest
             # delay value from the 'delay_policy' key in redis.
             delay_policy_key = '{0}:delay_policy'.format(self.host)
             delay_policy = self.get(delay_policy_key, delay_policy_name)
             if not delay_policy:
                 log.warn('unable to find delay_policy: {0}'.format(
                          delay_policy_name))
             stub.set_delay_policy(delay_policy)
         #_id = ObjectId(scenario_stub['_id'])
         #stub['recorded'] = str(_id.generation_time.date())
         cache_info.append(stub.payload)
     session['stubs'] = cache_info 
     #log.debug('stubs: {0}'.format(session['stubs']))
     self.set(scenario_key, session_name, session)
     log.debug('created session cache: {0}:{1}'.format(session['scenario'],
                                                       session['session']))
     return session
開發者ID:JohnFDavenport,項目名稱:stubo-app,代碼行數:77,代碼來源:__init__.py


注:本文中的stubo.model.stub.Stub.response_body方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。