Beispiel #1
0
    def itest_(self):
        # print(run_name)
        print(run_work_lis)
        # print(id)
        print("运行的任务:%s" % run_work_lis)
        task1 = models.Req_list_data.objects.values().filter(
            id__in=run_work_lis)

        print("task : %s" % task1)

        for task_i in task1:
            print(task_i)
            # t = central_control().control_inter_http()
            t = interface_http().req_requests(task_i["req"],
                                              task_i["url"],
                                              params=task_i["params"],
                                              headers=task_i["headers"])

            autoin_assert_lists = models.autoin_asserts.objects.values(
                "right_contrast_int", "assert_name", "left_contrast",
                "right_contrast").filter(Req_list_data_id=task_i["id"])
            print("autoin_assert_lists :%s" % autoin_assert_lists)
            # getattr(self, "assertEqual")(t.json()["headers"], {'Accept-Encoding': 'identity', 'Content-Length': '0', 'Host': 'httpbin.org'})
            print("T : %s" % (t.json()["headers"]))
            if autoin_assert_lists.count() != 0:
                print("进入断言")
                for autoin_assert_list in autoin_assert_lists:
                    print(" autoin_assert_list : %s" % autoin_assert_list)
                    if autoin_assert_list["right_contrast_int"] == '':
                        getattr(self, autoin_assert_list["assert_name"])(
                            getattr(t, autoin_assert_list["left_contrast"]),
                            autoin_assert_list["right_contrast"])

                    elif autoin_assert_list["right_contrast_int"] == 'int':
                        print("int 判断")
                        getattr(self, autoin_assert_list["assert_name"])(
                            getattr(t, autoin_assert_list["left_contrast"]),
                            int(autoin_assert_list["right_contrast"]))

                    elif autoin_assert_list["right_contrast_int"] == 'json':
                        print("json 判断")
                        dat_json1 = json.loads(
                            autoin_assert_list["right_contrast"])
                        print(type(dat_json1))
                        # print(type(t.json()["headers"]))
                        # print(dat_json1)
                        # 解决 获取字符串json() 不认识的问题 方法eval
                        getattr(self, autoin_assert_list["assert_name"])(
                            eval("t." + autoin_assert_list["left_contrast"]),
                            dat_json1)

                    elif autoin_assert_list["right_contrast_int"] == 'str':
                        getattr(self, autoin_assert_list["assert_name"])(
                            getattr(t, autoin_assert_list["left_contrast"]),
                            str(autoin_assert_list["right_contrast"]))
                    else:
                        getattr(self, autoin_assert_list["assert_name"])(
                            eval("t." + autoin_assert_list["left_contrast"]),
                            autoin_assert_list["right_contrast"])
Beispiel #2
0
    def test_interfac(self, name, da_par):

        print("da_par :%s" % da_par)
        task_i = models.Req_list_data.objects.values().filter(id=da_par)[0]
        print(task_i)
        t = interface_http().req_requests(task_i["req"],
                                          task_i["url"],
                                          params=task_i["params"],
                                          headers=task_i["headers"])

        autoin_assert_lists = models.autoin_asserts.objects.values().filter(
            Req_list_data_id=task_i["id"])
        print("autoin_assert_lists :%s" % autoin_assert_lists)
        # getattr(self, "assertEqual")(t.json()["headers"], {'Accept-Encoding': 'identity', 'Content-Length': '0', 'Host': 'httpbin.org'})
        print("T : %s" % (t.json()["headers"]))
        if autoin_assert_lists.count() != 0:
            print("进入断言")
            for autoin_assert_list in autoin_assert_lists:
                print(" autoin_assert_list : %s" % autoin_assert_list)
                if autoin_assert_list["right_contrast_int"] == '':
                    getattr(self, autoin_assert_list["assert_name"])(
                        getattr(t, autoin_assert_list["left_contrast"]),
                        autoin_assert_list["right_contrast"])

                elif autoin_assert_list["right_contrast_int"] == 'int':
                    print("int 判断")
                    getattr(self, autoin_assert_list["assert_name"])(
                        getattr(t, autoin_assert_list["left_contrast"]),
                        int(autoin_assert_list["right_contrast"]))

                elif autoin_assert_list["right_contrast_int"] == 'json':
                    # getattr(self, "assertEqual")(t.json()["headers"],
                    #                              {'Accept-Encoding': 'identity', 'Content-Length': '0',
                    #                               'Host': 'httpbin.org'})
                    print("json 判断")
                    dat_json1 = json.loads(
                        autoin_assert_list["right_contrast"])
                    print(type(dat_json1))
                    # print(type(t.json()["headers"]))
                    # print(dat_json1)
                    # 解决 获取字符串json() 不认识的问题 方法eval
                    getattr(self, autoin_assert_list["assert_name"])(
                        eval("t." + autoin_assert_list["left_contrast"]),
                        dat_json1)

                elif autoin_assert_list["right_contrast_int"] == 'str':
                    getattr(self, autoin_assert_list["assert_name"])(
                        getattr(t, autoin_assert_list["left_contrast"]),
                        str(autoin_assert_list["right_contrast"]))
                else:
                    getattr(self, autoin_assert_list["assert_name"])(
                        eval("t." + autoin_assert_list["left_contrast"]),
                        autoin_assert_list["right_contrast"])
Beispiel #3
0
def runlist_project(request):
    Ver_cook = Ver_cook_vr(request)
    print("进入请求")
    if Ver_cook:
        try:
            if request.method == "POST":
                dat = json.loads(request.body)
                print("dat:%s" %dat)
                print(dat)
                print(dat["id"])
                print(dat["urid"])
                if dat["urid"]:
                    get_list = models.Req_list_data.objects.values("id","data_list_id__id", "name",
                                                                      "data_list_id__task_id__pro_name_model",
                                                                      "data_list_id__task_id__id",
                                                                      "data_list_id__text", "data_list_id__agreement",
                                                                      "req", "url", "url_address", "params", "headers",
                                                                      "body", "way_body","file").filter(
                        data_list_id__task_id__task_projects_id=dat["id"], data_list_id__task_id=dat["urid"])
                else:
                    get_list = models.Req_list_data.objects.values("id","data_list_id__id", "name",
                                                                      "data_list_id__task_id__pro_name_model",
                                                                      "data_list_id__task_id__id",
                                                                      "data_list_id__text", "data_list_id__agreement",
                                                                      "req", "url", "url_address", "params", "headers",
                                                                      "body", "way_body","file").filter(
                        data_list_id__task_id__task_projects_id=dat["id"])
                print("get_list是:")
                print(list(get_list))
                print(type(list(get_list)))
                get_list = list(get_list)
                print(get_list)


                assert_list_data = []

                #获取时间
                req_name = time.strftime("%Y-%m-%d_%H%M%S%MS", time.localtime())
                for req_data in get_list:
                    print(req_data)

                    assert_data = models.autoin_asserts.objects.values("assert_name",
                                                         "left_contrast",
                                                         "right_contrast",
                                                         "right_contrast_int").filter(Req_list_data_id_id=req_data["id"])

                    # 处理断言数据
                    for ass in assert_data:
                        dat_list = [ass["assert_name"],ass["left_contrast"],ass["right_contrast"],ass["right_contrast_int"]]
                        assert_list_data.append(dat_list)

                    # print("assert_list_data:")
                    # print(assert_list_data)
                    #
                    # print(type(req_data))
                    # print(req_data["name"])
                    req = req_data["req"]
                    req_url = req_data["url"]+req_data["url_address"]
                    params = req_data["params"]
                    body = req_data["body"]
                    # headers = json.loads(req_data["headers"])

                    headers = json.loads(str(req_data["headers"]).replace('\'','"'))
                    # print(type(headers))
                    print("1")
                    print(headers)
                    file = req_data["file"]


                    print(req_url)
                    # headers=json.loads(headers)
                    req_htm, get_result = interface_http().req_requests(req, url=req_url,params =params,data=body,headers=headers,
                                                                        assertE=assert_list_data
                                                                        )

                    desk_models.work_run_request_history.objects.create(
                        req_id_id=req_data["id"],
                        req_name = req_data["name"],
                        req_url = req_url,  #url
                        req_param = params,  # 记录请求param
                        req_body = body,  # 记录请求body
                        req_headers = headers,  # 记录请求headers
                        req_assert = assert_list_data,  # 记录断言
                        req_html = req_htm.content,  # 记录请求返回结果
                        req_assert_to = get_result,  # 记录断言结果
                        execute_sign = req_name,  # 执行标记--执行时的名字
                        run_work_name = Ver_cook["user_id"],  # 执行人登录名Ver_cook["user_id"], Ver_cook["user_name"]
                        run_work_fullname = Ver_cook["user_name"], # 执行人名字
                        task_state = 1,  # 任务状态,是否执行成功。0默认未执行,1执行成功,-1执行失败
                        task_module = req_data["data_list_id__id"], # 模块
                        task_module_id = dat["id"],
                        task_sort = req_data["data_list_id__task_id__pro_name_model"], #分类
                        task_sort_id = req_data["data_list_id__task_id__id"],

                    )



                    print("req_htm是:")
                    # print(req_htm)

                ret = {"success": "成功","req_name":req_name}
        except BaseException as ex:
            print("进入错误")
            ret = {"fail": "%s" % ex}
            print(ret)
        return HttpResponse(json.dumps(ret))
Beispiel #4
0
def add_interface_add(request):

    Ver_cook = Ver_cook_vr(request)


    if request.method == "POST":
        dat = json.loads(request.body)
        print(dat)




        para_url = dat["par_url"]
        print("para_url :%s" % para_url)
        para_url = para_url.rstrip("#")  # 去除后尾# 号
        print("para_url :%s" % para_url)
        prara_list = para_url.split("&")
        para_url_ = {}
        for lis in prara_list:
            li = lis.split("=")
            para_url_[li[0]] = li[1]
        print("para_url_ : %s" % para_url_)

        add_req_name = dat["add_req_name"]
        if add_req_name:
            pass
        else:
            return  HttpResponse(json.dumps({"fail": "请输入接口名称"}))

        add_req_texts = dat["add_req_texts"]
        # add_req_of = dat["add_req_of"]

        # 请求方法
        method = dat["method"]
        url = dat["url"]
        url2 = dat["url2"]

        params = dat["data"]["Params"]
        print("Params")
        # for i in params
        print(params)
        # print(params.keys())
        # if len(params) != 0:
        #     pass
        # else:
        #     params = ""

        header = dat["data"]["tab_request_head"]
        if len(header) != 0:
            pass
        else:
            header = {}
        print("header : %s" % header)
        # form_data = dat["data"]["tab_request_head"]
        # print(dat["data"])

        add_interface_id = dat["add_interface_id"]
        # 判断body是哪个
        way_body = ''
        get_files = ""

        #用于判断是否有数据
        req_id = dat["add_id"]

        if "application_x_w" in dat["data"]["form_data"].keys():
            boy = dat["data"]["form_data"]["application_x_w"]
            way_body = 'form'

            if "Content-Type" in header.keys():
                print("header中有Content-Type")

            else:
                # header["Content-Type"] = "application/x-www-form-urlencoded"
                print("header中没有Content-Type")
        elif 'form_data' in dat["data"]["form_data"].keys():
            way_body = 'form_data'
            boy = dat["data"]["form_data"]["form_data"]
            print("boy : %s" % boy)
        elif 'raw_json' in dat["data"]["form_data"].keys():
            way_body = 'raw_json'
            boy = dat["data"]["form_data"]["raw_json"]

        elif 'files' in dat["data"].keys():
            way_body = 'file'
            # 文件files
            # print(dat["files"])

            get_files = dat["data"]["files"]
            boy = dat["data"]["files"]
            print("get_files:")
            print(get_files)
        else:
            boy = ""
        # print()

        # print("boy是:%s" %boy)

        # if len(boy) != 0 and (len(boy) == 1 and '' not in boy.keys()):
        if boy:
            pass
        else:
            boy = ""
        print(" 验证: %s" % boy)



        # 断言
        tab_req_assert = dat["tab_req_assert"]
        # print("tab_req_assert: %s" %tab_req_assert)


        succ = "添加成功"
        try:
            # 哪个项目
            print("哪个项目")
            # of_big_model_id = para_url_["project"]
            # of_big_model = pro_name = desk_models.task_projects.objects.values("pro_name").get(id=of_big_model_id)[
            #     "pro_name"]

            # 哪个模块
            # print("哪个模块")
            # of_big_on_small_model_id = para_url_["small"].rstrip("#")
            # of_big_on_small_model = \
            #     desk_models.task_project_model.objects.values("pro_name_model").get(id=of_big_on_small_model_id,
            #                                                                         task_projects_id=of_big_model_id)[
            #         "pro_name_model"]

            # 协议
            print("协议")
            agreement = desk_models.task_project_name.objects.values("agreement").get(id=dat["add_interface_id"])
            print(agreement)




            #
            req_htm=""
            ret = {}
            if dat["id"] == 1:
                print("----1-----")
                #查询数据是否存在
                try:
                    get_req_id = models.Req_list_data.objects.get(data_list_id=req_id)
                except:
                    get_req_id=""

                print("get_req_id:%s" %get_req_id)
                if get_req_id:
                    models.Req_list_data.objects.filter(data_list_id_id=req_id).update(
                        name=add_req_name, req=method, url=url,
                        url_address=url2, params=params,
                        headers=header, body=boy, describe=add_req_texts,
                        file=get_files,
                        agreement=agreement["agreement"],
                        way_body=way_body
                    )
                    ret_id = models.Req_list_data.objects.values('id').filter(data_list_id_id=req_id)[0]["id"]
                else:
                    retd = models.Req_list_data.objects.create(name=add_req_name, req=method, url=url,
                                                               url_address=url2, params=params,
                                                               headers=header, body=boy, describe=add_req_texts,
                                                               file = get_files,
                                                               data_list_id_id=dat["add_interface_id"],
                                                               agreement=agreement["agreement"],
                                                               way_body=way_body
                                                               )
                    ret_id = models.Req_list_data.objects.values('id').filter(data_list_id_id=dat["add_interface_id"]).order_by("-id")[0]["id"]


                print("retd: %s" % ret_id)


                # if way_body =="form_data":
                #     for boy_dat in boy:
                #         print(boy_dat)
                #         models.req_body.objects.create(
                #             req_body_id_id=ret_id,
                #             keys=boy_dat["key"],
                #             text_file=boy_dat["file_text"],
                #             values=boy_dat["values"],
                #             describe=''
                #         )


                # 添加断言
                num = 1  # 判断断言右侧是否还需要填写值
                # if tab_req_assert:
                print("tab_req_assert : %s" %tab_req_assert)
                if tab_req_assert:
                    models.autoin_asserts.objects.filter(Req_list_data_id=ret_id).delete()
                    for data_ass in tab_req_assert:

                        # if data_ass[0] != "请选择" or (data_ass[1] != "" and data_ass[2] != ""):
                        ass_dat = models.autoin_asserts.objects.create(
                            Req_list_data_id_id=ret_id,
                            assert_name=data_ass[0],
                            lef_NO_num=1,
                            left_contrast=data_ass[1],
                            right_NO_num=num,
                            right_contrast=data_ass[2],
                            right_contrast_int=data_ass[3]
                        )
                    print("添加断言")

            elif dat["id"] ==0:
                print("-----2-----")
                #发起请求
                print("修改 body :%s " % boy)
                print("修改 header :%s " % header)

                req_url = url+url2

                # params = list_values_tojson(params)
                # boy = list_values_tojson(boy)
                # header = list_values_tojson(header)
                # params = list_values_tojson(params)

                print(req_url)
                print(params)
                print(boy)
                print(header)
                # print(get_file)


                ret_id = add_interface_id
                succ = "请求成功"

                # 文件 get_files
                get_file = []
                print(get_files)
                print("123456")
                print(type(get_files))
                if get_files:
                    for file in get_files.keys():
                        print(file)
                        get_file.append(("files", (file, open(os.path.join(BASE_DIR, file_save_path,file), "rb"))))
                else:
                    get_file = ""
                print("get_file :%s" % get_file)

                req_htm,get_result = interface_http().req_requests(method, url, params=params, data=boy, headers=header, files=get_file, assertE=tab_req_assert)
                print("get_result:")
                print(get_result)

                # 调用http 请求
                print("ret_id : %s" % ret_id)
                #[['assertEqual', 'status_code', '200', 'int'], ['assertEqual', 'status_code', '200', 'int']]
                #断言返回结果
                if tab_req_assert:
                    # runht = bfr(unittest.makeSuite(Testinterfexec, "test_interfac"))
                    print("进入断言")


                # 断言访问 调试注释
                # run_word().run_reqerst_http(ret_id)

                # get_req_data = run_word().run_reqerst_http(ret_id)
                # print(" get_req_data : %s" % get_req_data)
                # print("req_htm: %s" %req_htm.content)

                if add_interface_id == "":
                    print("add_interface_id 为空")
                    print(add_interface_id)
                    ret = {"fail": "请选择接口类型"}
                else:
                    print("-1-1-1-1-")
                    # req_htm = interface_http().req_requests(method, url, params=params, data=boy, headers=header,
                    #                                         files=get_file)

                    print("0")
                    # print(req_htm.text)
                    # print(req_htm.content)
                    # print(req_htm.headers)
                    print("1")
                    if req_htm.headers:
                        print("2")
                        header_data = req_htm.headers

                        print(header_data)
                    else:
                        header_data = " "



                    # 返回列表中加入断言访问情况
                    # 断言访问 调试注释

                    # if get_req_data:
                    #     print("-1-1-")
                    #     ret["get_req_data"] = get_req_data

                    if req_htm.headers and 'gbk' in req_htm.headers["Content-Type"]:
                        ret = {"body": req_htm.content.decode('gbk'),
                               "headers":str(req_htm.headers),
                               "get_result":get_result
                               }  # .decode('utf-8'),"headers":req_htm.headers
                    else:
                        ret = {"body": req_htm.content.decode('utf-8'),
                               "headers":str(req_htm.headers),
                               "get_result": get_result,
                               }
                    # print(ret)

                    # data = req_htm.content.decode('utf-8')
                    # print(req_htm.json().decode('utf-8'))

                # pass
                # print("检测")

            else:
                print("------3----")
                # pass
            # print(retd)
            # print("url: %s" %url)
            # if req_htm.headers and 'gbk' in req_htm.headers["Content-Type"]:
            #     ret = {"success": succ, "body": req_htm.content.decode('gbk'),
            #            "data_id": ret_id}  # .decode('utf-8'),"headers":req_htm.headers
            #
            # else:  # 'utf-8' in req_htm.headers["Content-Type"]:
            ret["success"] = succ


        except BaseException as ex:
            print("进入错误")
            print(ex)
            ret = {"fail": "%s" % ex}

        # print(boy)
        # success

        print("ret:")
        # print(ret)

    return HttpResponse(json.dumps(ret))  # json.dumps(ret)
Beispiel #5
0
    def itest_(self):
        # print(run_name)
        print("run_name :%s" % run_name)
        # print(id)
        print("运行的任务:%s" % run_work_lis)
        task1 = models.Req_list_data.objects.values().filter(
            id__in=run_work_lis)

        print("task : %s" % task1)

        for task_i in task1:
            print(task_i)
            get_files = task_i.file
            get_file = []

            if get_files:
                for file in get_files.keys():
                    get_file.append(
                        ("name",
                         (file,
                          open(os.path.join(BASE_DIR, file_save_path, file),
                               "rb"))))
            else:
                get_file = ""
            way_body = task_i.way_body
            body_s = {}
            if "form_data" in way_body:

                req_bods = models.req_body.objects.values().filter(
                    req_body_id_id=task_i.id)

                for bodys in req_bods:
                    body_s[bodys.keys] = bodys.values

            elif "application_x_w" in way_body:
                body_s = task_i.body
            else:
                pass

            # t = central_control().control_inter_http()
            t = interface_http().req_requests(task_i["req"],
                                              task_i["url"],
                                              params=task_i["params"],
                                              headers=task_i["headers"],
                                              data=body_s,
                                              files=get_file)

            autoin_assert_lists = models.autoin_asserts.objects.values(
                "right_contrast_int", "assert_name", "left_contrast",
                "right_contrast").filter(Req_list_data_id=task_i["id"])
            print("autoin_assert_lists :%s" % autoin_assert_lists)
            # getattr(self, "assertEqual")(t.json()["headers"], {'Accept-Encoding': 'identity', 'Content-Length': '0', 'Host': 'httpbin.org'})
            # print("T : %s" % (t.json()["headers"]))
            if autoin_assert_lists.count() != 0:
                print("进入断言")
                for autoin_assert_list in autoin_assert_lists:
                    print(" autoin_assert_list : %s" % autoin_assert_list)
                    if autoin_assert_list["right_contrast_int"] == '':
                        getattr(self, autoin_assert_list["assert_name"])(
                            getattr(t, autoin_assert_list["left_contrast"]),
                            autoin_assert_list["right_contrast"])

                    elif autoin_assert_list["right_contrast_int"] == 'int':
                        print("int 判断")
                        getattr(self, autoin_assert_list["assert_name"])(
                            getattr(t, autoin_assert_list["left_contrast"]),
                            int(autoin_assert_list["right_contrast"]))

                    elif autoin_assert_list["right_contrast_int"] == 'json':
                        print("json 判断")
                        dat_json1 = json.loads(
                            autoin_assert_list["right_contrast"])
                        print(type(dat_json1))
                        # print(type(t.json()["headers"]))
                        # print(dat_json1)
                        # 解决 获取字符串json() 不认识的问题 方法eval
                        getattr(self, autoin_assert_list["assert_name"])(
                            eval("t." + autoin_assert_list["left_contrast"]),
                            dat_json1)

                    elif autoin_assert_list["right_contrast_int"] == 'str':
                        getattr(self, autoin_assert_list["assert_name"])(
                            getattr(t, autoin_assert_list["left_contrast"]),
                            str(autoin_assert_list["right_contrast"]))
                    else:
                        getattr(self, autoin_assert_list["assert_name"])(
                            eval("t." + autoin_assert_list["left_contrast"]),
                            autoin_assert_list["right_contrast"])
Beispiel #6
0
def add_interface_add(request):
    if request.method == "POST":
        dat = json.loads(request.body)
        print(dat)

        para_url = dat["par_url"]
        print("para_url :%s" % para_url)
        para_url = para_url.rstrip("#")  # 去除后尾# 号
        print("para_url :%s" % para_url)
        prara_list = para_url.split("&")
        para_url_ = {}
        for lis in prara_list:
            li = lis.split("=")
            para_url_[li[0]] = li[1]
        print("para_url_ : %s" % para_url_)

        add_req_name = dat["add_req_name"]
        # add_id = dat["add_id"]
        if "ecdi" in para_url_.keys():
            add_id = para_url_["ecdi"]
        else:
            add_id = ""

        print("add_id : %s" % add_id)
        add_req_texts = dat["add_req_texts"]
        add_req_of = dat["add_req_of"]

        # 请求方法
        method = dat["method"]
        url = dat["url"]

        params = dat["data"]["Params"]
        # print(params.keys())
        if len(params) != 0:
            pass
        else:
            params = ""

        header = dat["data"]["tab_request_head"]
        if len(header) != 0:
            pass
        else:
            header = {}
        print("header : %s" % header)
        # form_data = dat["data"]["tab_request_head"]
        # print(dat["data"])
        # 判断body是哪个
        way_body = ''
        if "application_x_w" in dat["data"]["form_data"].keys():
            boy = dat["data"]["form_data"]["application_x_w"]
            way_body = 'application_x_w'

            if "Content-Type" in header.keys():
                print("header中有Content-Type")

            else:
                header["Content-Type"] = "application/x-www-form-urlencoded"
                print("header中没有Content-Type")
        elif 'form_data' in dat["data"]["form_data"].keys():
            way_body = 'form_data'
            boy = dat["data"]["form_data"]["form_data"]
            print("boy : %s" % boy)
        else:
            boy = ""
        # print()

        # print("boy是:%s" %boy)

        # if len(boy) != 0 and (len(boy) == 1 and '' not in boy.keys()):
        if boy:
            pass
        else:
            boy = ""
        print(" 验证: %s" % boy)

        #文件files
        # print(dat["files"])
        get_files = dat["data"]["files"]

        print(get_files)

        # 断言
        tab_req_assert = dat["tab_req_assert"]
        # print("tab_req_assert: %s" %tab_req_assert)

        try:
            # 哪个项目
            print("哪个项目")
            of_big_model_id = para_url_["project"]
            of_big_model = pro_name = desk_models.task_projects.objects.values(
                "pro_name").get(id=of_big_model_id)["pro_name"]

            # 哪个模块
            print("哪个模块")
            of_big_on_small_model_id = para_url_["small"].rstrip("#")
            of_big_on_small_model = \
                desk_models.task_project_model.objects.values("pro_name_model").get(id=of_big_on_small_model_id,
                                                                                    task_projects_id=of_big_model_id)[
                    "pro_name_model"]

            # 协议
            print("协议")
            agreement = "http"

            # print("发起访问")

            #
            if add_id == "" and add_req_of != "":
                print("----1-----")
                retd = models.Req_list_data.objects.create(
                    name=add_req_name,
                    req=method,
                    url=url,
                    params=params,
                    headers=header,
                    body=boy,
                    describe=add_req_texts,
                    file=get_files,
                    data_list_name=add_req_of,
                    of_big_model_id=of_big_model_id,
                    of_big_model=of_big_model,
                    of_big_on_small_model_id=of_big_on_small_model_id,
                    of_big_on_small_model=of_big_on_small_model,
                    agreement=agreement,
                    way_body=way_body)
                ret_id = models.Req_list_data.objects.values('id').filter(
                    url=url).order_by("-id")[0]["id"]

                succ = "添加成功"
                print("retd: %s" % ret_id)

                if way_body == "form_data":
                    for boy_dat in boy:
                        print(boy_dat)
                        models.req_body.objects.create(
                            req_body_id_id=ret_id,
                            keys=boy_dat["key"],
                            text_file=boy_dat["file_text"],
                            values=boy_dat["values"],
                            describe='')

                # 添加断言
                num = 1  # 判断断言右侧是否还需要填写值
                # if tab_req_assert:
                print("tab_req_assert : %s" % tab_req_assert)
                for data_ass in tab_req_assert:
                    # if data_ass[0] != "请选择" or (data_ass[1] != "" and data_ass[2] != ""):
                    ass_dat = models.autoin_asserts.objects.create(
                        Req_list_data_id_id=ret_id,
                        assert_name=data_ass[0],
                        lef_NO_num=1,
                        left_contrast=data_ass[1],
                        right_NO_num=num,
                        right_contrast=data_ass[2],
                        right_contrast_int=data_ass[3])
                    print("添加断言")

            elif add_id and add_req_of != "":
                print("-----2-----")
                print("修改 body :%s " % boy)
                print("修改 header :%s " % header)
                retd = models.Req_list_data.objects.filter(id=add_id).update(
                    name=add_req_name,
                    req=method,
                    url=url,
                    params=params,
                    headers=header,
                    body=boy,
                    file=get_files,
                    describe=add_req_texts,
                    data_list_name=add_req_of,
                    way_body=way_body)

                ret_id = add_id
                succ = "修改成功"
                if way_body == "form_data":
                    # 删除原来的body
                    body_data = models.req_body.objects.values().filter(
                        req_body_id=add_id)
                    print("body_data: %s" % body_data)

                    if body_data.count() != 0:
                        print("进入判断")
                        models.req_body.objects.filter(
                            req_body_id=add_id).delete()
                    for boy_dat in boy:
                        print(boy_dat)
                        models.req_body.objects.create(
                            req_body_id_id=ret_id,
                            keys=boy_dat["key"],
                            text_file=boy_dat["file_text"],
                            values=boy_dat["values"],
                            describe='')

                # 删除原来添加的断言
                data = models.autoin_asserts.objects.values().filter(
                    Req_list_data_id_id=add_id)
                print("data: %s" % data)
                print(data == [])
                print(data.count() == '')
                print(data.count() == 0)
                if data:
                    print("进入判断1")
                    print(add_id)
                    delete_ass = models.autoin_asserts.objects.filter(
                        Req_list_data_id_id=add_id).delete()
                    # print("删除之前关联的断言:%s" % delete_ass.count())

                # 添加新的断言
                num = 1  # 判断断言右侧是否还需要填写值

                # print(tab_req_assert)
                for data_ass in tab_req_assert:
                    # print("进入添加断言")
                    # if data_ass[0] != "请选择" and (data_ass[1] != "" and data_ass[2] != ""):
                    ass_dat = models.autoin_asserts.objects.create(
                        Req_list_data_id_id=ret_id,
                        assert_name=data_ass[0],
                        lef_NO_num=1,
                        left_contrast=data_ass[1],
                        right_NO_num=num,
                        right_contrast=data_ass[2],
                        right_contrast_int=data_ass[3])

                    print("添加新断言")

                # pass
                # print("检测")

            else:
                print("------3----")
                # pass
            # print(retd)
            # print("url: %s" %url)

            req_bodys = models.req_body.objects.values().filter(
                req_body_id_id=ret_id)
            boy = {}
            for req_bod in req_bodys:
                if req_bod["text_file"] == 'file':
                    pass
                else:
                    boy[req_bod["keys"]] = req_bod["values"]

            #文件 get_files
            get_file = []
            print(get_files)
            print("123456")
            print(type(get_files))
            if get_files:
                for file in get_files.keys():
                    get_file.append(
                        ("files",
                         (file,
                          open(os.path.join(BASE_DIR, file_save_path, file),
                               "rb"))))
            else:
                get_file = ""
            print("get_file :%s" % get_file)

            # req_htm = interface_http().req_requests(method, url, params=params, data=boy, headers=header, files=get_file)
            # 调用http 请求
            print("ret_id : %s" % ret_id)

            # 断言访问 调试注释
            # run_word().run_reqerst_http(ret_id)
            get_req_data = run_word().run_reqerst_http(ret_id)
            print(" get_req_data : %s" % get_req_data)
            # print("req_htm: %s" %req_htm.content)

            if add_req_of == "":
                print("add_req_of 为空")
                ret = {"fail": "请选择接口类型"}
            else:
                print("-1-1-1-1-")
                req_htm = interface_http().req_requests(method,
                                                        url,
                                                        params=params,
                                                        data=boy,
                                                        headers=header,
                                                        files=get_file)

                print("0")
                print(req_htm.text)
                # print(req_htm.content)
                # print(req_htm.headers)
                print("1")
                if req_htm.headers:
                    print("2")
                    header_data = req_htm.headers

                    print(header_data)
                else:
                    header_data = " "

                if req_htm.headers and 'gbk' in req_htm.headers["Content-Type"]:
                    ret = {
                        "success": succ,
                        "body": req_htm.content.decode('gbk'),
                        "headers": "%s" % header_data,
                        "data_id": ret_id
                    }  # .decode('utf-8'),"headers":req_htm.headers

                else:  # 'utf-8' in req_htm.headers["Content-Type"]:
                    ret = {
                        "success": succ,
                        "body": req_htm.content.decode('utf-8'),
                        "headers": "%s" % header_data,
                        "data_id": ret_id
                    }

                # 返回列表中加入断言访问情况
                # 断言访问 调试注释

                if get_req_data:
                    print("-1-1-")
                    ret["get_req_data"] = get_req_data

            # print(ret)

            # data = req_htm.content.decode('utf-8')
            # print(req_htm.json().decode('utf-8'))

        except BaseException as ex:
            print("进入错误")
            print(ex)
            ret = {"fail": "%s" % ex}

        # print(boy)
        # success

    return HttpResponse(json.dumps(ret))  # json.dumps(ret)
Beispiel #7
0
    def test_interfac(self):

        # Testinterfexec.__class__.__name__ = 'q123'
        print("interfase_run_listid:  %s" % interfase_run_listid)
        task_i = models.Req_list_data.objects.values().filter(
            id__in=interfase_run_listid)[0]
        print("task_i :%s" % task_i)
        # task_i = models.Req_list_data.objects.values().filter(id=task1)[0]
        # print(task_i)
        t = interface_http().req_requests(task_i["req"],
                                          task_i["url"],
                                          params=task_i["params"],
                                          headers=task_i["headers"])

        autoin_assert_lists = models.autoin_asserts.objects.values().filter(
            Req_list_data_id=task_i["id"])
        print("autoin_assert_lists :%s" % autoin_assert_lists)
        # getattr(self, "assertEqual")(t.json()["headers"], {'Accept-Encoding': 'identity', 'Content-Length': '0', 'Host': 'httpbin.org'})
        # print("T : %s" % (t.json()["headers"]))
        if autoin_assert_lists.count() != 0:
            print("进入断言")
            for autoin_assert_list in autoin_assert_lists:
                print(" autoin_assert_list : %s" % autoin_assert_list)
                if autoin_assert_list["right_contrast_int"] == '':
                    fp = getattr(self, autoin_assert_list["assert_name"])(
                        getattr(t, autoin_assert_list["left_contrast"]),
                        autoin_assert_list["right_contrast"])

                elif autoin_assert_list["right_contrast_int"] == 'int':
                    print("int 判断")
                    print(getattr(t, autoin_assert_list["left_contrast"]))
                    print(int(autoin_assert_list["right_contrast"]))
                    fp = getattr(self, autoin_assert_list["assert_name"])(
                        getattr(t, autoin_assert_list["left_contrast"]),
                        int(autoin_assert_list["right_contrast"]))

                elif autoin_assert_list["right_contrast_int"] == 'json':
                    # getattr(self, "assertEqual")(t.json()["headers"],
                    #                              {'Accept-Encoding': 'identity', 'Content-Length': '0',
                    #                               'Host': 'httpbin.org'})
                    print("json 判断")
                    dat_json1 = json.loads(
                        autoin_assert_list["right_contrast"])
                    print(type(dat_json1))
                    # print(type(t.json()["headers"]))
                    # print(dat_json1)
                    # 解决 获取字符串json() 不认识的问题 方法eval
                    fp = getattr(self, autoin_assert_list["assert_name"])(
                        eval("t." + autoin_assert_list["left_contrast"]),
                        dat_json1)

                elif autoin_assert_list["right_contrast_int"] == 'str':
                    fp = getattr(self, autoin_assert_list["assert_name"])(
                        getattr(t, autoin_assert_list["left_contrast"]),
                        str(autoin_assert_list["right_contrast"]))
                else:
                    fp = getattr(self, autoin_assert_list["assert_name"])(
                        eval("t." + autoin_assert_list["left_contrast"]),
                        autoin_assert_list["right_contrast"])
                print("fp: %s" % unittest.TextTestRunner.failfast)
                print("fp: %s" % unittest.failures)