def get_sampler_pair(self, request): timeout = self.safe_time(request.priority_option('timeout')) # convert body to string if isinstance(request.body, (dict, list, numeric_types)): if request.get_header('content-type') == 'application/json' or isinstance(request.body, numeric_types): request.body = json.dumps(request.body) elif not simple_body_dict(request.body): LOG.debug('Header "Content-Type: application/json" is required for body: "%s"', request.body) request.body = json.dumps(request.body) use_random_host_ip = request.priority_option('random-source-ip', default=False) host_ips = get_host_ips(filter_loopbacks=True) if use_random_host_ip else [] files = request.upload_files body_file = request.config.get("body-file") has_file_for_body = not (request.body or files) and body_file # method can be put, post, or even variable if has_file_for_body and request.method != "GET": files = [{"path": body_file}] http = JMX._get_http_request(request.url, request.label, request.method, timeout, request.body, request.priority_option('keepalive', default=True), files, request.content_encoding, request.priority_option('follow-redirects', default=True), use_random_host_ip, host_ips) children = etree.Element("hashTree") if request.headers: children.append(JMX._get_header_mgr(request.headers)) children.append(etree.Element("hashTree")) return http, children
def _get_exec(self): exec_str = '' for req in self.scenario.get_requests(): if not isinstance(req, HTTPRequest): msg = "Gatling simulation generator doesn't support '%s' blocks, skipping" self.log.warning(msg, req.NAME) continue if len(exec_str) > 0: exec_str += '.' default_address = self.scenario.get("default-address") if default_address: url = req.url else: url = self.fixed_addr(req.url) exec_str += 'exec(\n' exec_template = self.indent('http("%(req_label)s").%(method)s("%(url)s")\n', level=2) exec_str += exec_template % {'req_label': req.label, 'method': req.method.lower(), 'url': url} for key in req.headers: exec_template = self.indent('.header("%(key)s", "%(val)s")\n', level=3) exec_str += exec_template % {'key': key, 'val': req.headers[key]} # todo: join with the same in get_sampler_pair if isinstance(req.body, (dict, list, numeric_types)): if req.get_header('content-type') == 'application/json' or isinstance(req.body, numeric_types): req.body = json.dumps(req.body) elif not simple_body_dict(req.body): self.log.debug('Header "Content-Type: application/json" is required for body: "%s"', req.body) req.body = json.dumps(req.body) if isinstance(req.body, str): exec_str += self.indent('.body(%(method)s("""%(body)s"""))\n', level=3) exec_str = exec_str % {'method': 'StringBody', 'body': req.body} elif isinstance(req.body, dict): for key in sorted(req.body.keys()): exec_str += self.indent('.formParam("%(key)s", "%(val)s")\n', level=3) exec_str = exec_str % {'key': key, 'val': req.body[key]} elif req.body is not None: self.log.warning("Unknown body type: %s", req.body) exec_str += self.__get_assertions(req.config.get('assert', [])) if not req.priority_option('follow-redirects', default=True): exec_str += self.indent('.disableFollowRedirect\n', level=3) exec_str += self.indent(')', level=1) think_time = int(dehumanize_time(req.get_think_time())) if think_time: exec_str += '.pause(%(think_time)s)' % {'think_time': think_time} return exec_str
def get_sampler_pair(self, request): timeout = self.safe_time(request.priority_option('timeout')) # convert body to string if isinstance(request.body, (dict, list, numeric_types)): if request.get_header( 'content-type') == 'application/json' or isinstance( request.body, numeric_types): request.body = json.dumps(request.body) elif not simple_body_dict(request.body): LOG.debug( 'Header "Content-Type: application/json" is required for body: "%s"', request.body) request.body = json.dumps(request.body) use_random_host_ip = request.priority_option('random-source-ip', default=False) host_ips = get_host_ips( filter_loopbacks=True) if use_random_host_ip else [] files = request.upload_files body_file = request.config.get("body-file") has_file_for_body = not (request.body or files) and body_file # method can be put, post, or even variable if has_file_for_body and request.method != "GET": files = [{"path": body_file}] http = JMX._get_http_request( request.url, request.label, request.method, timeout, request.body, request.priority_option('keepalive', default=True), files, request.content_encoding, request.priority_option('follow-redirects', default=True), use_random_host_ip, host_ips) children = etree.Element("hashTree") if request.headers: children.append(JMX._get_header_mgr(request.headers)) children.append(etree.Element("hashTree")) return http, children