TODO: 解决覆盖率问题

This commit is contained in:
eson 2019-09-04 03:08:28 +08:00
parent cade90cc40
commit 469276cdaa
4 changed files with 55 additions and 25 deletions

View File

@ -54,3 +54,8 @@ func FromHTTPResponse(resp *http.Response) (*Response, error) {
func (gresp *Response) Content() string { func (gresp *Response) Content() string {
return gresp.readContent return gresp.readContent
} }
// GetSrcResponse 获取原生golang http.Response
func (gresp *Response) GetSrcResponse() *http.Response {
return gresp.readResponse
}

View File

@ -1,9 +1,34 @@
package requests package requests
import ( import (
"net/http"
"testing" "testing"
"474420502.top/eson/gjson"
) )
func TestTest(t *testing.T) { func TestTest(t *testing.T) {
var gresp *http.Response
var err error
gresp, err = http.DefaultClient.Get("http://httpbin.org/get")
if err != nil {
t.Error(err)
}
resp, err := FromHTTPResponse(gresp)
if err != nil {
t.Error(err)
}
if gjson.Get(resp.Content(), "headers.Host").String() != "httpbin.org" {
t.Error("headers.Host != httpbin.org ?")
}
if resp.GetSrcResponse().StatusCode != 200 {
t.Error("StatusCode != 200")
}
if len(resp.GetSrcResponse().Header) == 0 {
t.Error("esp.GetSrcResponse().Header == nil")
}
} }

View File

@ -76,7 +76,7 @@ func TestSession_Post(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
ses := NewSession() ses := NewSession()
got, err := ses.Post("http://httpbin.org/post").AutoSetBody(tt.args.params...).Execute() got, err := ses.Post("http://httpbin.org/post").SetBodyAuto(tt.args.params...).Execute()
if err != nil { if err != nil {
t.Errorf("Metchod error = %v", err) t.Errorf("Metchod error = %v", err)
@ -127,7 +127,7 @@ func TestSession_Setparams(t *testing.T) {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
ses := NewSession() ses := NewSession()
got, err := ses.Post("http://httpbin.org/post").AutoSetBody(tt.args.params...).Execute() got, err := ses.Post("http://httpbin.org/post").SetBodyAuto(tt.args.params...).Execute()
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("Metchod error = %v, wantErr %v", err, tt.wantErr) t.Errorf("Metchod error = %v, wantErr %v", err, tt.wantErr)
return return
@ -169,7 +169,7 @@ func TestSession_PostUploadFile(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
ses := NewSession() ses := NewSession()
got, err := ses.Post("http://httpbin.org/post").AutoSetBody(tt.args.params, TypeFormData).Execute() got, err := ses.Post("http://httpbin.org/post").SetBodyAuto(tt.args.params, TypeFormData).Execute()
if err != nil { if err != nil {
t.Errorf("Metchod error = %v", err) t.Errorf("Metchod error = %v", err)
@ -213,7 +213,7 @@ func TestSession_Put(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
ses := NewSession() ses := NewSession()
got, err := ses.Put("http://httpbin.org/put").AutoSetBody(tt.args.params, TypeFormData).Execute() got, err := ses.Put("http://httpbin.org/put").SetBodyAuto(tt.args.params, TypeFormData).Execute()
if err != nil { if err != nil {
t.Errorf("Metchod error = %v", err) t.Errorf("Metchod error = %v", err)
@ -257,7 +257,7 @@ func TestSession_Patch(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
ses := NewSession() ses := NewSession()
got, err := ses.Patch("http://httpbin.org/patch").AutoSetBody(tt.args.params, TypeFormData).Execute() got, err := ses.Patch("http://httpbin.org/patch").SetBodyAuto(tt.args.params, TypeFormData).Execute()
if err != nil { if err != nil {
t.Errorf("Metchod error = %v", err) t.Errorf("Metchod error = %v", err)

View File

@ -173,25 +173,25 @@ func (wf *Workflow) GetURLRawPath() string {
} }
// encodePath path格式每个item都必须以/开头 // encodePath path格式每个item都必须以/开头
func encodePath(path []string) string { // func encodePath(path []string) string {
rawpath := "" // rawpath := ""
for _, p := range path { // for _, p := range path {
if p[0] != '/' { // if p[0] != '/' {
p = "/" + p // p = "/" + p
} // }
rawpath += p // rawpath += p
} // }
return rawpath // return rawpath
} // }
// SetURLPath 设置Path参数 // // SetURLPath 设置Path参数
func (wf *Workflow) SetURLPath(path []string) *Workflow { // func (wf *Workflow) SetURLPath(path []string) *Workflow {
if path == nil { // if path == nil {
return wf // return wf
} // }
wf.ParsedURL.Path = encodePath(path) // wf.ParsedURL.Path = encodePath(path)
return wf // return wf
} // }
// SetURLRawPath 设置Pa晚上参数 // SetURLRawPath 设置Pa晚上参数
func (wf *Workflow) SetURLRawPath(path string) *Workflow { func (wf *Workflow) SetURLRawPath(path string) *Workflow {
@ -210,8 +210,8 @@ func (wf *Workflow) GetBody(body IBody) IBody {
return wf.Body return wf.Body
} }
// AutoSetBody 参数设置 // SetBodyAuto 参数设置
func (wf *Workflow) AutoSetBody(params ...interface{}) *Workflow { func (wf *Workflow) SetBodyAuto(params ...interface{}) *Workflow {
if params != nil { if params != nil {
plen := len(params) plen := len(params)