提交 52ff43ff 编写于 作者: aaronchen2k2k's avatar aaronchen2k2k

update http methed to return an err object

上级 f209f008
......@@ -37,7 +37,10 @@ func ExecCase(ch chan int, sendOutputMsg, sendExecMsg func(info, isRunning strin
func ExecModule(ch chan int, sendOutputMsg, sendExecMsg func(info, isRunning string, wsMsg websocket.Message), req serverDomain.WsReq, msg websocket.Message) (
report commDomain.ZtfReport, pathMaxWidth int, err error) {
cases := zentaoUtils.GetCasesByModule(stringUtils.ParseInt(req.ProductId), stringUtils.ParseInt(req.ModuleId), req.ProjectPath)
cases, err := zentaoUtils.GetCasesByModule(stringUtils.ParseInt(req.ProductId), stringUtils.ParseInt(req.ModuleId), req.ProjectPath)
if err != nil {
return
}
if req.Seq != "" {
cases = analysisUtils.FilterCaseByResult(cases, req)
......@@ -49,7 +52,7 @@ func ExecModule(ch chan int, sendOutputMsg, sendExecMsg func(info, isRunning str
func ExecSuite(ch chan int, sendOutputMsg, sendExecMsg func(info, isRunning string, wsMsg websocket.Message), req serverDomain.WsReq, msg websocket.Message) (
report commDomain.ZtfReport, pathMaxWidth int, err error) {
cases := zentaoUtils.GetCasesBySuite(stringUtils.ParseInt(req.ProductId), stringUtils.ParseInt(req.SuiteId), req.ProjectPath)
cases, err := zentaoUtils.GetCasesBySuite(stringUtils.ParseInt(req.ProductId), stringUtils.ParseInt(req.SuiteId), req.ProjectPath)
if req.Seq != "" {
cases = analysisUtils.FilterCaseByResult(cases, req)
......@@ -61,7 +64,10 @@ func ExecSuite(ch chan int, sendOutputMsg, sendExecMsg func(info, isRunning stri
func ExecTask(ch chan int, sendOutputMsg, sendExecMsg func(info, isRunning string, wsMsg websocket.Message), req serverDomain.WsReq, msg websocket.Message) (
report commDomain.ZtfReport, pathMaxWidth int, err error) {
cases := zentaoUtils.GetCasesByTask(stringUtils.ParseInt(req.ProductId), stringUtils.ParseInt(req.TaskId), req.ProjectPath)
cases, err := zentaoUtils.GetCasesByTask(stringUtils.ParseInt(req.ProductId), stringUtils.ParseInt(req.TaskId), req.ProjectPath)
if err != nil {
return
}
if req.Seq != "" {
cases = analysisUtils.FilterCaseByResult(cases, req)
......
......@@ -42,20 +42,20 @@ func CommitBug(ztfBug commDomain.ZtfBug, projectPath string) (err error) {
bug := commDomain.ZentaoBug{}
copier.Copy(&bug, ztfBug)
ret, ok := httpUtils.Post(url, bug, true)
_, err = httpUtils.Post(url, bug, true)
msg := ""
if ok {
if err == nil {
msg = i118Utils.Sprintf("success_to_report_bug", ztfBug.Case)
} else {
msg = color.RedString(string(ret))
msg = color.RedString(err.Error())
}
if commConsts.ComeFrom == "cmd" {
msgView, _ := commConsts.Cui.View("reportBugMsg")
msgView.Clear()
if ok {
if err == nil {
color.New(color.FgGreen).Fprintf(msgView, msg)
commConsts.Cui.DeleteView("submitInput")
......@@ -140,8 +140,8 @@ func GetBugFiledOptions(req commDomain.FuncResult, projectPath string) (
// field options
config := configUtils.LoadByProjectPath(projectPath)
ok := Login(config)
if !ok {
err = Login(config)
if err != nil {
return
}
......@@ -154,8 +154,8 @@ func GetBugFiledOptions(req commDomain.FuncResult, projectPath string) (
}
url := config.Url + GenApiUri("bug", "ajaxGetBugFieldOptions", params)
bytes, ok := httpUtils.Get(url)
if ok {
bytes, err := httpUtils.Get(url)
if err == nil {
jsonData := &simplejson.Json{}
jsonData, err = simplejson.NewJson(bytes)
......
......@@ -18,10 +18,10 @@ import (
"strings"
)
func GetCasesByModule(productId int, moduleId int, projectPath string) (cases []string) {
func GetCasesByModule(productId int, moduleId int, projectPath string) (cases []string, err error) {
config := configUtils.LoadByProjectPath(projectPath)
ok := Login(config)
if !ok {
err = Login(config)
if err != nil {
return
}
......@@ -39,10 +39,10 @@ func GetCasesByModule(productId int, moduleId int, projectPath string) (cases []
return
}
func GetCasesBySuite(productId int, suiteId int, projectPath string) (cases []string) {
func GetCasesBySuite(productId int, suiteId int, projectPath string) (cases []string, err error) {
config := configUtils.LoadByProjectPath(projectPath)
ok := Login(config)
if !ok {
err = Login(config)
if err != nil {
return
}
......@@ -60,10 +60,10 @@ func GetCasesBySuite(productId int, suiteId int, projectPath string) (cases []st
return
}
func GetCasesByTask(productId int, taskId int, projectPath string) (cases []string) {
func GetCasesByTask(productId int, taskId int, projectPath string) (cases []string, err error) {
config := configUtils.LoadByProjectPath(projectPath)
ok := Login(config)
if !ok {
err = Login(config)
if err != nil {
return
}
......@@ -93,9 +93,9 @@ func ListCaseByProduct(baseUrl string, productId int) []commDomain.ZtfCase {
}
url := baseUrl + GenApiUri("testcase", "browse", params)
dataStr, ok := httpUtils.Get(url)
dataStr, err := httpUtils.Get(url)
if ok {
if err == nil {
var product commDomain.ZtfProduct
json.Unmarshal(dataStr, &product)
......@@ -127,9 +127,9 @@ func ListCaseByModule(baseUrl string, productId, moduleId int) []commDomain.ZtfC
}
url := baseUrl + GenApiUri("testcase", "browse", params)
bytes, ok := httpUtils.Get(url)
bytes, err := httpUtils.Get(url)
if ok {
if err == nil {
var module commDomain.ZtfModule
json.Unmarshal(bytes, &module)
......@@ -161,9 +161,9 @@ func ListCaseBySuite(baseUrl string, productId, suiteId int) []commDomain.ZtfCas
}
url := baseUrl + GenApiUri("testsuite", "view", params)
bytes, ok := httpUtils.Get(url)
bytes, err := httpUtils.Get(url)
if ok {
if err == nil {
var suite commDomain.ZtfSuite
json.Unmarshal(bytes, &suite)
......@@ -196,9 +196,9 @@ func ListCaseByTask(baseUrl string, productId, taskId int) []commDomain.ZtfCase
}
url := baseUrl + GenApiUri("testtask", "cases", params)
bytes, ok := httpUtils.Get(url)
bytes, err := httpUtils.Get(url)
if ok {
if err == nil {
var task commDomain.ZtfTask
json.Unmarshal(bytes, &task)
......@@ -246,9 +246,9 @@ func GetCaseById(baseUrl string, caseId string) commDomain.ZtfCase {
}
url := baseUrl + GenApiUri("testcase", "view", params)
bytes, ok := httpUtils.Get(url)
bytes, err := httpUtils.Get(url)
if ok {
if err == nil {
var csw commDomain.ZtfCaseWrapper
json.Unmarshal(bytes, &csw)
......@@ -260,11 +260,11 @@ func GetCaseById(baseUrl string, caseId string) commDomain.ZtfCase {
}
func CommitCase(caseId int, title string,
stepMap maps.Map, stepTypeMap maps.Map, expectMap maps.Map, projectPath string) {
stepMap maps.Map, stepTypeMap maps.Map, expectMap maps.Map, projectPath string) (err error) {
config := configUtils.LoadByProjectPath(projectPath)
ok := Login(config)
if !ok {
err = Login(config)
if err != nil {
return
}
......@@ -283,7 +283,10 @@ func CommitCase(caseId int, title string,
"stepType": commonUtils.LinkedMapToMap(stepTypeMap),
"expects": commonUtils.LinkedMapToMap(expectMap)}
json, _ := json.Marshal(requestObj)
json, err := json.Marshal(requestObj)
if err != nil {
return
}
if commConsts.Verbose {
logUtils.Infof(string(json))
......@@ -296,11 +299,13 @@ func CommitCase(caseId int, title string,
}
if yes {
_, ok = httpUtils.Post(url, requestObj, true)
if ok {
_, err = httpUtils.Post(url, requestObj, true)
if err == nil {
logUtils.Infof(i118Utils.Sprintf("success_to_commit_case", caseId) + "\n")
}
}
return
}
func fieldMapToListOrderByInt(mp map[string]interface{}) []commDomain.BugOption {
......
package zentaoUtils
import (
"errors"
commConsts "github.com/aaronchen2k/deeptest/internal/comm/consts"
commDomain "github.com/aaronchen2k/deeptest/internal/comm/domain"
configUtils "github.com/aaronchen2k/deeptest/internal/comm/helper/config"
......@@ -29,13 +30,14 @@ func CommitResult(report commDomain.ZtfReport, productId, taskId string, project
Login(config)
url := config.Url + GenApiUri("ci", "commitResult", "")
ret, ok := httpUtils.Post(url, report, false)
ret, err := httpUtils.Post(url, report, false)
msg := ""
if ok {
if err == nil {
msg = color.GreenString(i118Utils.Sprintf("success_to_submit_test_result"))
} else {
msg = color.RedString(string(ret))
err = errors.New(string(ret))
}
logUtils.Info(msg)
......
......@@ -16,11 +16,11 @@ import (
"strings"
)
func Login(config commDomain.ProjectConf) bool {
ok := GetConfig(config.Url)
if !ok {
func Login(config commDomain.ProjectConf) (err error) {
err = GetConfig(config.Url)
if err != nil {
logUtils.Infof(i118Utils.Sprintf("fail_to_login"))
return false
return
}
uri := ""
......@@ -36,12 +36,12 @@ func Login(config commDomain.ProjectConf) bool {
params["password"] = config.Password
var bodyBytes []byte
bodyBytes, ok = httpUtils.PostStr(url, params)
if !ok || (ok && strings.Index(string(bodyBytes), "title") > 0) { // use PostObject to login again for new system
_, ok = httpUtils.Post(url, params, true)
bodyBytes, err = httpUtils.PostStr(url, params)
if err != nil || (err == nil && strings.Index(string(bodyBytes), "title") > 0) { // use PostObject to login again for new system
_, err = httpUtils.Post(url, params, true)
}
if ok {
if err == nil {
if commConsts.Verbose {
logUtils.Info(i118Utils.Sprintf("success_to_login"))
}
......@@ -49,18 +49,18 @@ func Login(config commDomain.ProjectConf) bool {
logUtils.Errorf(i118Utils.Sprintf("fail_to_login"))
}
return ok
return
}
func GetConfig(baseUrl string) bool {
func GetConfig(baseUrl string) (err error) {
//if commConsts.RequestType != "" {
// return true
//}
url := baseUrl + "?mode=getconfig"
bytes, ok := httpUtils.Get(url)
if !ok {
return false
bytes, err := httpUtils.Get(url)
if err != nil {
return
}
json, _ := simplejson.NewJson(bytes)
......@@ -79,12 +79,12 @@ func GetConfig(baseUrl string) bool {
uri = "index.php?m=user&f=login&t=json"
}
url = baseUrl + uri
bytes, ok = httpUtils.Get(url)
if !ok {
return false
bytes, err = httpUtils.Get(url)
if err != nil {
return
}
return true
return
}
func ListLang() (langs []serverDomain.ZentaoLang, err error) {
......@@ -102,7 +102,10 @@ func ListProduct(projectPath string) (products []serverDomain.ZentaoProduct, err
return
}
Login(config)
err = Login(config)
if err != nil {
return
}
// $productID = 0, $branch = 0, $browseType = '', $param = 0, $storyType = 'story',
// $orderBy = '', $recTotal = 0, $recPerPage = 20, $pageID = 1, $projectID = 0)
......@@ -114,15 +117,15 @@ func ListProduct(projectPath string) (products []serverDomain.ZentaoProduct, err
}
url := config.Url + GenApiUri("product", "browse", params)
bytes, ok := httpUtils.Get(url)
bytes, err := httpUtils.Get(url)
if !ok {
if err != nil {
err = errors.New("请检查项目配置")
return
}
jsn, _ := simplejson.NewJson(bytes)
productMap, _ := jsn.Get("products").Map()
productMap, err := jsn.Get("products").Map()
for key, val := range productMap {
id, _ := strconv.Atoi(key)
......@@ -134,9 +137,12 @@ func ListProduct(projectPath string) (products []serverDomain.ZentaoProduct, err
func ListModuleByProduct(productId int, projectPath string) (modules []serverDomain.ZentaoModule, err error) {
config := configUtils.LoadByProjectPath(projectPath)
Login(config)
// tree-browse-1-story.html#app=product
err = Login(config)
if err != nil {
return
}
// tree-browse-1-story.html#app=product
params := ""
if commConsts.RequestType == commConsts.PathInfo {
params = fmt.Sprintf("%d-story", productId)
......@@ -147,9 +153,8 @@ func ListModuleByProduct(productId int, projectPath string) (modules []serverDom
url := config.Url + GenApiUri("tree", "browse", params)
url += "#app=product"
bytes, ok := httpUtils.Get(url)
if !ok {
err = errors.New("tree-browse-story fail")
bytes, err := httpUtils.Get(url)
if err != nil {
return
}
......@@ -166,7 +171,10 @@ func ListModuleByProduct(productId int, projectPath string) (modules []serverDom
func ListModuleForCase(productId int, projectPath string) (modules []serverDomain.ZentaoModule, err error) {
config := configUtils.LoadByProjectPath(projectPath)
Login(config)
err = Login(config)
if err != nil {
return
}
// tree-browse-1-case-0-0-qa.html
params := ""
......@@ -179,9 +187,8 @@ func ListModuleForCase(productId int, projectPath string) (modules []serverDomai
url := config.Url + GenApiUri("tree", "browse", params)
url += "#app=product"
bytes, ok := httpUtils.Get(url)
if !ok {
err = errors.New("tree-browse-story fail")
bytes, err := httpUtils.Get(url)
if err != nil {
return
}
......@@ -218,7 +225,10 @@ func GenModuleData(mp map[string]interface{}, modules *[]serverDomain.ZentaoModu
func ListSuiteByProduct(productId int, projectPath string) (suites []serverDomain.ZentaoSuite, err error) {
config := configUtils.LoadByProjectPath(projectPath)
Login(config)
err = Login(config)
if err != nil {
return
}
// $productID = 0, $orderBy = 'id_asc', $recTotal = 0, $recPerPage = 20, $pageID = 1
params := ""
......@@ -230,8 +240,8 @@ func ListSuiteByProduct(productId int, projectPath string) (suites []serverDomai
url := config.Url + GenApiUri("testsuite", "browse", params)
bytes, ok := httpUtils.Get(url)
if !ok {
bytes, err := httpUtils.Get(url)
if err != nil {
err = errors.New("testsuite-browse fail")
return
}
......@@ -253,7 +263,10 @@ func ListSuiteByProduct(productId int, projectPath string) (suites []serverDomai
func ListTaskByProduct(productId int, projectPath string) (tasks []serverDomain.ZentaoTask, err error) {
config := configUtils.LoadByProjectPath(projectPath)
Login(config)
err = Login(config)
if err != nil {
return
}
// $productID = 0, $branch = '', $type = 'local,totalStatus', $orderBy = 'id_asc', $recTotal = 0, $recPerPage = 20, $pageID = 1, $beginTime = 0, $endTime = 0)
params := ""
......@@ -264,9 +277,9 @@ func ListTaskByProduct(productId int, projectPath string) (tasks []serverDomain.
}
url := config.Url + GenApiUri("testtask", "browse", params)
bytes, ok := httpUtils.Get(url)
bytes, err := httpUtils.Get(url)
if !ok {
if err != nil {
err = errors.New("testsuite-browse fail")
return
}
......
......@@ -2,6 +2,7 @@ package httpUtils
import (
"encoding/json"
"errors"
"fmt"
commConsts "github.com/aaronchen2k/deeptest/internal/comm/consts"
i118Utils "github.com/aaronchen2k/deeptest/internal/pkg/lib/i118"
......@@ -15,7 +16,7 @@ import (
"strings"
)
func Get(url string) (ret []byte, ok bool) {
func Get(url string) (ret []byte, err error) {
if strings.Index(url, "mode=getconfig") < 0 {
url = AddToken(url)
}
......@@ -25,17 +26,15 @@ func Get(url string) (ret []byte, ok bool) {
client := &http.Client{}
req, reqErr := http.NewRequest("GET", url, nil)
if reqErr != nil {
logUtils.Error(reqErr.Error())
ok = false
req, err := http.NewRequest("GET", url, nil)
if err != nil {
logUtils.Error(err.Error())
return
}
resp, respErr := client.Do(req)
if respErr != nil {
logUtils.Error(respErr.Error())
ok = false
resp, err := client.Do(req)
if err != nil {
logUtils.Error(err.Error())
return
}
......@@ -47,8 +46,8 @@ func Get(url string) (ret []byte, ok bool) {
defer resp.Body.Close()
var zentaoResp serverDomain.ZentaoResp
jsonErr := json.Unmarshal(bodyBytes, &zentaoResp)
if jsonErr != nil {
err = json.Unmarshal(bodyBytes, &zentaoResp)
if err != nil {
if strings.Index(string(bodyBytes), "<html>") > -1 {
if commConsts.Verbose {
logUtils.Errorf(i118Utils.Sprintf("request_response") + " HTML - " + gohtml.FormatWithLineNo(string(bodyBytes)))
......@@ -56,7 +55,7 @@ func Get(url string) (ret []byte, ok bool) {
return
} else {
if commConsts.Verbose {
logUtils.Infof(jsonErr.Error())
logUtils.Infof(err.Error())
}
return
}
......@@ -67,16 +66,17 @@ func Get(url string) (ret []byte, ok bool) {
status := zentaoResp.Status
if status == "" { // 非嵌套结构
ret = bodyBytes
ok = true
} else { // 嵌套结构
ret = []byte(zentaoResp.Data)
ok = status == "success"
if status != "success" {
err = errors.New(zentaoResp.Data)
}
}
return
}
func Post(url string, data interface{}, useFormFormat bool) (ret []byte, ok bool) {
func Post(url string, data interface{}, useFormFormat bool) (ret []byte, err error) {
url = AddToken(url)
if commConsts.Verbose {
......@@ -103,24 +103,24 @@ func Post(url string, data interface{}, useFormFormat bool) (ret []byte, ok bool
logUtils.Infof(dataStr)
}
req, reqErr := http.NewRequest("POST", url, strings.NewReader(dataStr))
if reqErr != nil {
logUtils.Error(reqErr.Error())
req, err := http.NewRequest("POST", url, strings.NewReader(dataStr))
if err != nil {
logUtils.Error(err.Error())
return
}
//req.Header.Set("Content-Type", "application/json")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, respErr := client.Do(req)
if respErr != nil {
logUtils.Error(respErr.Error())
resp, err := client.Do(req)
if err != nil {
logUtils.Error(err.Error())
return
}
bodyBytes, ioErr := ioutil.ReadAll(resp.Body)
if ioErr != nil {
logUtils.Error(ioErr.Error())
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
logUtils.Error(err.Error())
return
}
......@@ -131,12 +131,12 @@ func Post(url string, data interface{}, useFormFormat bool) (ret []byte, ok bool
defer resp.Body.Close()
ret, ok = GetRespErr(bodyBytes, url)
ret, err = GetRespErr(bodyBytes, url)
return
}
func PostStr(url string, params map[string]string) (ret []byte, ok bool) {
func PostStr(url string, params map[string]string) (ret []byte, err error) {
url = AddToken(url)
if commConsts.Verbose {
......@@ -159,24 +159,22 @@ func PostStr(url string, params map[string]string) (ret []byte, ok bool) {
logUtils.Infof(paramStr)
}
req, reqErr := http.NewRequest("POST", url, strings.NewReader(paramStr))
if reqErr != nil {
req, err := http.NewRequest("POST", url, strings.NewReader(paramStr))
if err != nil {
if commConsts.Verbose {
logUtils.Infof(reqErr.Error())
logUtils.Infof(err.Error())
}
ok = false
return
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("cookie", commConsts.SessionVar+"="+commConsts.SessionId)
resp, respErr := client.Do(req)
if respErr != nil {
resp, err := client.Do(req)
if err != nil {
if commConsts.Verbose {
logUtils.Infof(respErr.Error())
logUtils.Infof(err.Error())
}
ok = false
return
}
......@@ -235,7 +233,7 @@ func AddToken(url string) (ret string) {
return
}
func GetRespErr(bytes []byte, url string) (ret []byte, ok bool) {
func GetRespErr(bytes []byte, url string) (ret []byte, err error) {
ret = bytes
if len(bytes) == 0 {
......@@ -243,14 +241,13 @@ func GetRespErr(bytes []byte, url string) (ret []byte, ok bool) {
}
var zentaoResp serverDomain.ZentaoResp
jsonErr := json.Unmarshal(bytes, &zentaoResp)
if jsonErr != nil {
err = json.Unmarshal(bytes, &zentaoResp)
if err != nil {
if commConsts.Verbose {
if strings.Index(url, "login") < 0 { // jsonErr caused by login request return a html
logUtils.Infof(jsonErr.Error())
logUtils.Infof(err.Error())
}
}
ok = false
return
}
......@@ -258,18 +255,18 @@ func GetRespErr(bytes []byte, url string) (ret []byte, ok bool) {
status := zentaoResp.Status
if status != "" {
ret = []byte(zentaoResp.Data)
ok = status == "success"
if status == "success" {
err = errors.New(zentaoResp.Data)
}
return
}
// 非嵌套结构,map[result:success]
var respData = serverDomain.ZentaoRespData{}
err := json.Unmarshal(bytes, &respData)
err = json.Unmarshal(bytes, &respData)
if err == nil && (respData.Result != "" && respData.Result != "success") {
ok = false
} else {
ok = true
err = errors.New(string(bytes))
}
return
......
......@@ -18,8 +18,8 @@ func (s *TestCaseService) LoadTestCases(productId, moduleId, suiteId, taskId int
config := configUtils.LoadByProjectPath(projectPath)
ok := zentaoUtils.Login(config)
if !ok {
err := zentaoUtils.Login(config)
if err != nil {
loginFail = true
return
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册