add stream test, Repeat the problem

This commit is contained in:
bxd
2023-11-14 10:18:43 +08:00
parent fb8716ce4a
commit 8919e527de
4 changed files with 59 additions and 14 deletions

4
go.mod
View File

@@ -11,9 +11,9 @@ require (
github.com/gospider007/net v0.0.0-20231028084010-313c148cf0a1
github.com/gospider007/re v0.0.0-20231024115818-adfd03636256
github.com/gospider007/tools v0.0.0-20231110011734-c2a4739ea0a0
github.com/gospider007/websocket v0.0.0-20231104023155-0d1a787293d3
github.com/gospider007/websocket v0.0.0-20231114021646-5bfda75c28a6
github.com/refraction-networking/utls v1.5.4
golang.org/x/exp v0.0.0-20231108232855-2478ac86f678
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa
golang.org/x/net v0.18.0
)

4
go.sum
View File

@@ -63,6 +63,8 @@ github.com/gospider007/websocket v0.0.0-20231102021107-d34612e770bd h1:6kmXEifLg
github.com/gospider007/websocket v0.0.0-20231102021107-d34612e770bd/go.mod h1:OSPnflgh+c8tIqpkUCpX21uBiJIGikhURuEctoRZM/E=
github.com/gospider007/websocket v0.0.0-20231104023155-0d1a787293d3 h1:cjfmIiX1+ZcEfMy5hikIX37mzX/mnAuJ5T0EUb0wy0I=
github.com/gospider007/websocket v0.0.0-20231104023155-0d1a787293d3/go.mod h1:OSPnflgh+c8tIqpkUCpX21uBiJIGikhURuEctoRZM/E=
github.com/gospider007/websocket v0.0.0-20231114021646-5bfda75c28a6 h1:NBNR8ido7velygbUQo1gApOAN3WyLTc8KCYzaliPfTs=
github.com/gospider007/websocket v0.0.0-20231114021646-5bfda75c28a6/go.mod h1:a716MYobAt21tc8vP7qK+Hr2NgHKxhyQ4QtX1gql/hg=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
@@ -138,6 +140,8 @@ golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
golang.org/x/exp v0.0.0-20231108232855-2478ac86f678 h1:mchzmB1XO2pMaKFRqk/+MV3mgGG96aqaPXaMifQU47w=
golang.org/x/exp v0.0.0-20231108232855-2478ac86f678/go.mod h1:zk2irFbV9DP96SEBUUAy67IdHUaZuSnrz1n472HUCLE=
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa h1:FRnLl4eNAQl8hwxVVC17teOw8kdjVDVAiFMtgUdTSRQ=
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa/go.mod h1:zk2irFbV9DP96SEBUUAy67IdHUaZuSnrz1n472HUCLE=
golang.org/x/image v0.13.0 h1:3cge/F/QTkNLauhf2QoE9zp+7sr+ZcL4HnoZmdwg9sg=
golang.org/x/image v0.13.0/go.mod h1:6mmbMOeV28HuMTgA6OSRkdXKYw/t5W9Uwn2Yv1r3Yxk=
golang.org/x/image v0.14.0 h1:tNgSxAFe3jC4uYqvZdTr84SZoM1KfwdC9SKIFrLjFn4=

View File

@@ -34,6 +34,7 @@ type Response struct {
filePath string
bar bool
isNewConn bool
isClosed bool
}
type SseClient struct {
@@ -288,16 +289,6 @@ func (obj *Response) ReadBody() error {
return nil
}
// safe close conn
func (obj *Response) CloseConn() {
obj.response.Body.(interface{ CloseConn() }).CloseConn()
}
// force close conn
func (obj *Response) ForceCloseConn() {
obj.response.Body.(interface{ ForceCloseConn() }).ForceCloseConn()
}
// conn proxy
func (obj *Response) Proxy() string {
return obj.response.Body.(interface{ Proxy() string }).Proxy()
@@ -325,18 +316,43 @@ func (obj *Response) H2Ja3() string {
// close body
func (obj *Response) CloseBody() error {
if obj.cnl != nil {
defer obj.cnl()
if obj.isClosed {
return nil
}
defer func() {
if obj.cnl != nil {
obj.cnl()
}
obj.isClosed = true
}()
if obj.webSocket != nil {
obj.webSocket.Close()
}
if obj.response != nil && obj.response.Body != nil {
if err := tools.CopyWitchContext(obj.ctx, io.Discard, obj.response.Body, false); err != nil {
obj.CloseConn()
return err
} else {
return obj.response.Body.Close()
}
}
return nil
}
// safe close conn
func (obj *Response) CloseConn() {
if obj.isClosed {
return
}
obj.response.Body.(interface{ CloseConn() }).CloseConn()
obj.isClosed = true
}
// force close conn
func (obj *Response) ForceCloseConn() {
if obj.isClosed {
return
}
obj.response.Body.(interface{ ForceCloseConn() }).ForceCloseConn()
obj.isClosed = true
}

25
test/stream_test.go Normal file
View File

@@ -0,0 +1,25 @@
package main
import (
"testing"
"github.com/gospider007/requests"
)
func TestStream(t *testing.T) {
resp, err := requests.Get(nil, "https://httpbin.org/anything", requests.RequestOption{
Stream: true,
})
if err != nil {
t.Fatal(err)
}
if resp.IsStream() {
if resp.StatusCode() != 200 {
t.Fatal("resp.StatusCode()!= 200")
}
resp.CloseBody()
resp.CloseBody()
} else {
t.Fatal("resp.IsStream() is false")
}
}