Skip to content

Commit

Permalink
fix body 为空
Browse files Browse the repository at this point in the history
  • Loading branch information
link1st committed Jun 9, 2023
1 parent 0f09374 commit e26a27c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 45 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/link1st/go-stress-testing

go 1.14
go 1.16

require (
github.com/golang/protobuf v1.4.2
Expand Down
4 changes: 2 additions & 2 deletions model/curl_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package model
import (
"encoding/json"
"errors"
"io/ioutil"
"io"
"os"
"strings"

Expand Down Expand Up @@ -50,7 +50,7 @@ func ParseTheFile(path string) (curl *CURL, err error) {
defer func() {
_ = file.Close()
}()
dataBytes, err := ioutil.ReadAll(file)
dataBytes, err := io.ReadAll(file)
if err != nil {
err = errors.New("读取文件失败:" + err.Error())
return
Expand Down
2 changes: 1 addition & 1 deletion model/request_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type Verify interface {
}

// VerifyHTTP http 验证
type VerifyHTTP func(request *Request, response *http.Response) (code int, isSucceed bool)
type VerifyHTTP func(request *Request, response *http.Response, body []byte) (code int, isSucceed bool)

// VerifyWebSocket webSocket 验证
type VerifyWebSocket func(request *Request, seq string, msg []byte) (code int, isSucceed bool)
Expand Down
28 changes: 17 additions & 11 deletions server/golink/http_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"sync"

Expand Down Expand Up @@ -65,6 +64,7 @@ func send(chanID uint64, request *model.Request) (bool, int, uint64, int64) {
// startTime = time.Now()
isSucceed = false
errCode = model.HTTPOk
body []byte
contentLength = int64(0)
err error
resp *http.Response
Expand All @@ -73,23 +73,26 @@ func send(chanID uint64, request *model.Request) (bool, int, uint64, int64) {
newRequest := getRequest(request)

resp, requestTime, err = client.HTTPRequest(chanID, newRequest)

if err != nil {
errCode = model.RequestErr // 请求错误
} else {
// 此处原方式获取的数据长度可能是 -1,换成如下方式获取可获取到正确的长度
contentLength, err = getBodyLength(resp)
body, err = getBody(resp)
if err != nil {
contentLength = resp.ContentLength
errCode = model.ParseError
} else {
contentLength = int64(len(body))
// 验证请求是否成功
errCode, isSucceed = newRequest.GetVerifyHTTP()(newRequest, resp, body)
}
// 验证请求是否成功
errCode, isSucceed = newRequest.GetVerifyHTTP()(newRequest, resp)
}
return isSucceed, errCode, requestTime, contentLength
}

// getBodyLength 获取响应数据长度
func getBodyLength(response *http.Response) (length int64, err error) {
// getBody 获取响应数据
func getBody(response *http.Response) (body []byte, err error) {
defer func() {
_ = response.Body.Close()
}()
var reader io.ReadCloser
switch response.Header.Get("Content-Encoding") {
case "gzip":
Expand All @@ -100,6 +103,9 @@ func getBodyLength(response *http.Response) (length int64, err error) {
default:
reader = response.Body
}
body, err := ioutil.ReadAll(reader)
return int64(len(body)), err
body, err = io.ReadAll(reader)
defer func() {
_ = response.Body.Close()
}()
return body, err
}
43 changes: 13 additions & 30 deletions server/verify/http_verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"

"github.com/link1st/go-stress-testing/model"
Expand All @@ -25,26 +24,21 @@ func getZipData(response *http.Response) (body []byte, err error) {
default:
reader = response.Body
}
body, err = ioutil.ReadAll(reader)
response.Body = ioutil.NopCloser(bytes.NewReader(body))
body, err = io.ReadAll(reader)
response.Body = io.NopCloser(bytes.NewReader(body))
return
}

// HTTPStatusCode 通过 HTTP 状态码判断是否请求成功
func HTTPStatusCode(request *model.Request, response *http.Response) (code int, isSucceed bool) {
defer func() {
_ = response.Body.Close()
}()
func HTTPStatusCode(request *model.Request, response *http.Response, body []byte) (code int, isSucceed bool) {
code = response.StatusCode
if code == request.Code {
isSucceed = true
}
// 开启调试模式
if request.GetDebug() {
body, err := getZipData(response)
fmt.Printf("请求结果 httpCode:%d body:%s err:%v \n", response.StatusCode, string(body), err)
fmt.Printf("请求结果 httpCode:%d body:%s \n", response.StatusCode, string(body))
}
io.Copy(ioutil.Discard, response.Body)
return
}

Expand All @@ -60,35 +54,24 @@ type ResponseJSON struct {
// HTTPJson 通过返回的Body 判断
// 返回示例: {"code":200,"msg":"Success","data":{}}
// code 默认将http code作为返回码,http code 为200时 取body中的返回code
func HTTPJson(request *model.Request, response *http.Response) (code int, isSucceed bool) {
defer func() {
_ = response.Body.Close()
}()
func HTTPJson(request *model.Request, response *http.Response, body []byte) (code int, isSucceed bool) {
code = response.StatusCode
if code == http.StatusOK {
body, err := getZipData(response)
if err != nil {
responseJSON := &ResponseJSON{}
if err := json.Unmarshal(body, responseJSON); err != nil {
code = model.ParseError
fmt.Printf("请求结果 ioutil.ReadAll err:%v", err)
fmt.Printf("请求结果 json.Unmarshal err:%v", err)
} else {
responseJSON := &ResponseJSON{}
err = json.Unmarshal(body, responseJSON)
if err != nil {
code = model.ParseError
fmt.Printf("请求结果 json.Unmarshal err:%v", err)
} else {
code = responseJSON.Code
// body 中code返回200为返回数据成功
if responseJSON.Code == request.Code {
isSucceed = true
}
code = responseJSON.Code
// body 中code返回200为返回数据成功
if responseJSON.Code == request.Code {
isSucceed = true
}
}
// 开启调试模式
if request.GetDebug() {
fmt.Printf("请求结果 httpCode:%d body:%s err:%v \n", response.StatusCode, string(body), err)
fmt.Printf("请求结果 httpCode:%d body:%s \n", response.StatusCode, string(body))
}
}
io.Copy(ioutil.Discard, response.Body)
return
}

0 comments on commit e26a27c

Please sign in to comment.