def isHandshakeSuccessful(self, timeout=1.0): """ Wait to receive "handshake successful" event until the end of the timeout. @param timeout: time to wait response in second (default=1s) @type timeout: float @return: an event matching with the template or None otherwise @rtype: templatemessage """ TestAdapterLib.check_timeout(caller=TestAdapterLib.caller(), timeout=timeout) tpl_rsp = TestTemplatesLib.TemplateMessage() if self.cfg['agent-support']: layer_agent = TestTemplatesLib.TemplateLayer('AGENT') layer_agent.addKey(name='name', data=self.cfg['agent']['name']) layer_agent.addKey(name='type', data=self.cfg['agent']['type']) tpl_rsp.addLayer(layer_agent) tpl_rsp.addLayer(AdapterIP.ip(more=AdapterIP.received())) tpl_rsp.addLayer(AdapterTCP.tcp(more=AdapterTCP.received())) if self.cfg['ssl-support']: tpl_rsp.addLayer(AdapterSSL.ssl(more=AdapterSSL.received())) tpl_rsp.addLayer(AdapterHTTP.response()) tpl_rsp.addLayer(templates.ws(more=templates.handshake_ok())) return self.received(expected=tpl_rsp, timeout=timeout)
def hasReceivedPing(self, timeout=1.0): """ Waits to receive "ping" event until the end of the timeout. @param timeout: time to wait response in second (default=1s) @type timeout: float @return: an event matching with the template or None otherwise @rtype: templatemessage """ TestAdapterLib.check_timeout(caller=TestAdapterLib.caller(), timeout=timeout) tpl_rsp = TestTemplatesLib.TemplateMessage() if self.cfg['agent-support']: layer_agent = TestTemplatesLib.TemplateLayer('AGENT') layer_agent.addKey(name='name', data=self.cfg['agent']['name']) layer_agent.addKey(name='type', data=self.cfg['agent']['type']) tpl_rsp.addLayer(layer_agent) tpl_rsp.addLayer(AdapterIP.ip(more=AdapterIP.received())) tpl_rsp.addLayer(AdapterTCP.tcp(more=AdapterTCP.received())) if self.cfg['ssl-support']: tpl_rsp.addLayer(AdapterSSL.ssl(more=AdapterSSL.received())) tpl_rsp.addLayer( templates.ws(opcode=codec.WEBSOCKET_OPCODE_PING, more=templates.received())) return self.received(expected=tpl_rsp, timeout=timeout)
def hasReceivedHttpResponse(self, httpCode="200", httpPhrase="OK", headers=None, timeout=1.0): """ Wait to receive "http response" until the end of the timeout. @param httpCode: http code (default=200) @type httpCode: string @param httpPhrase: http phrase (default=OK) @type httpPhrase: string @param timeout: time to wait in seconds (default=1s) @type timeout: float @return: http response @rtype: template """ TestAdapterLib.check_timeout(caller=TestAdapterLib.caller(), timeout=timeout) tpl = TestTemplatesLib.TemplateMessage() if self.cfg['agent-support']: layer_agent= TestTemplatesLib.TemplateLayer('AGENT') layer_agent.addKey(name='name', data=self.cfg['agent']['name'] ) layer_agent.addKey(name='type', data=self.cfg['agent']['type'] ) tpl.addLayer( layer_agent ) tpl.addLayer( AdapterIP.ip( more=AdapterIP.received() ) ) tpl.addLayer( AdapterTCP.tcp(more=AdapterTCP.received()) ) if self.ADP_HTTP.tcp().cfg['ssl-support']: tpl.addLayer( AdapterSSL.ssl(more=AdapterSSL.received()) ) tpl.addLayer( AdapterHTTP.response( version="HTTP/1.1", code=httpCode, phrase=httpPhrase, headers=headers ) ) return self.hasReceivedResponse(expected=tpl, timeout=timeout)
def getTemplateRequest(self, httpMethod="GET", httpUri="/", httpVersion='HTTP/1.1', httpHeaders={}, httpBody=None): """ """ tpl = TestTemplates.TemplateMessage() if self.cfg['agent-support']: layer_agent = TestTemplates.TemplateLayer('AGENT') layer_agent.addKey(name='name', data=self.cfg['agent']['name']) layer_agent.addKey(name='type', data=self.cfg['agent']['type']) tpl.addLayer(layer_agent) tpl.addLayer(AdapterIP.ip(more=AdapterIP.received())) tpl.addLayer(AdapterTCP.tcp(more=AdapterTCP.received())) if self.ADP_TCP.cfg['ssl-support']: tpl.addLayer(AdapterSSL.ssl(more=AdapterSSL.received())) headers = {} headers.update(httpHeaders) tpl.addLayer( templates.request(method=httpMethod, uri=httpUri, version=httpVersion, headers=headers, body=httpBody)) return tpl
def hasReceivedHttpRequest(self, httpMethod="GET", httpUri="/", httpVersion='HTTP/1.1', timeout=1.0, httpHeaders={}, httpBody=None): """ Wait to receive "http request" until the end of the timeout. @param httpMethod: http method (default=GET) @type httpMethod: string @param httpUri: http phrase (default=OK) @type httpUri: string @param httpVersion: http version (default=HTTP/1.1) @type httpVersion: string @param httpHeaders: expected http headers @type httpHeaders: dict @param httpBody: expected body (default=None) @type httpBody: string/none @param timeout: time to wait in seconds (default=1s) @type timeout: float @return: http response @rtype: template """ TestAdapterLib.check_timeout(caller=TestAdapterLib.caller(), timeout=timeout) tpl = TestTemplates.TemplateMessage() if self.cfg['agent-support']: layer_agent = TestTemplates.TemplateLayer('AGENT') layer_agent.addKey(name='name', data=self.cfg['agent']['name']) layer_agent.addKey(name='type', data=self.cfg['agent']['type']) tpl.addLayer(layer_agent) tpl.addLayer(AdapterIP.ip(more=AdapterIP.received())) tpl.addLayer(AdapterTCP.tcp(more=AdapterTCP.received())) if self.ADP_TCP.cfg['ssl-support']: tpl.addLayer(AdapterSSL.ssl(more=AdapterSSL.received())) headers = {} headers.update(httpHeaders) tpl.addLayer( templates.request(method=httpMethod, uri=httpUri, version=httpVersion, headers=headers, body=httpBody)) return self.hasReceivedRequest(expected=tpl, timeout=timeout)
def hasReceivedRequest(self, expected, timeout=1.0): """ Wait to receive "response" until the end of the timeout. @param expected: response template @type expected: templatemessage/templatelayer @param timeout: time to wait in seconds (default=1s) @type timeout: float @return: http response or none otherwise @rtype: templatemessage/templatelayer/none """ TestAdapterLib.check_timeout(caller=TestAdapterLib.caller(), timeout=timeout) if expected is None: raise Exception( 'has received response: expected template cannot be empty') tpl_expected = expected if isinstance(expected, TestTemplates.TemplateLayer): tpl_expected = TestTemplates.TemplateMessage() if self.cfg['agent-support']: layer_agent = TestTemplates.TemplateLayer('AGENT') layer_agent.addKey(name='name', data=self.cfg['agent']['name']) layer_agent.addKey(name='type', data=self.cfg['agent']['type']) tpl_expected.addLayer(layer_agent) tpl_expected.addLayer(AdapterIP.ip(more=AdapterIP.received())) tpl_expected.addLayer(AdapterTCP.tcp(more=AdapterTCP.received())) if self.ADP_TCP.cfg['ssl-support']: tpl_expected.addLayer( AdapterSSL.ssl(more=AdapterSSL.received())) tpl_expected.addLayer(expected) evt = self.received(expected=tpl_expected, timeout=timeout) if evt is None: return None return evt
def hasReceivedRestResponse(self, httpCode="200", httpPhrase="OK", httpVersion='HTTP/1.1', timeout=1.0, httpHeaders={}, httpBody=None): """ Wait to receive "rest response" until the end of the timeout. @param httpCode: http code (default=200) @type httpCode: string @param httpPhrase: http phrase (default=OK) @type httpPhrase: string @param httpVersion: http version (default=HTTP/1.1) @type httpVersion: string @param httpHeaders: expected http headers @type httpHeaders: dict @param httpBody: expected body (default=None) @type httpBody: string/none @param timeout: time to wait in seconds (default=1s) @type timeout: float @return: http response @rtype: template """ TestAdapter.check_timeout(caller=TestAdapter.caller(), timeout=timeout) tpl = TestTemplates.TemplateMessage() if self.cfg['agent-support']: layer_agent = TestTemplates.TemplateLayer('AGENT') layer_agent.addKey(name='name', data=self.cfg['agent']['name']) layer_agent.addKey(name='type', data=self.cfg['agent']['type']) tpl.addLayer(layer_agent) tpl.addLayer(AdapterIP.ip(more=AdapterIP.received())) tpl.addLayer(AdapterTCP.tcp(more=AdapterTCP.received())) if self.ADP_HTTP.tcp().cfg['ssl-support']: tpl.addLayer(AdapterSSL.ssl(more=AdapterSSL.received())) headers = { 'content-type': TestOperators.Contains(needle='application/json', AND=True, OR=False) } headers.update(httpHeaders) tpl.addLayer( AdapterHTTP.response(version=httpVersion, code=httpCode, phrase=httpPhrase, headers=headers, body=httpBody)) tpl.addLayer(templates.response()) return self.hasReceivedResponse(expected=tpl, timeout=timeout)