Skip to content

Commit fd3f143

Browse files
committed
added non json do
1 parent 8d82933 commit fd3f143

File tree

6 files changed

+204
-14
lines changed

6 files changed

+204
-14
lines changed

coverage.out

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
mode: set
2-
github.com/GolangToolKits/go-http-proxy/goProxy.go:15.62,20.52 5 1
3-
github.com/GolangToolKits/go-http-proxy/goProxy.go:20.52,23.18 3 1
4-
github.com/GolangToolKits/go-http-proxy/goProxy.go:23.18,25.4 1 1
5-
github.com/GolangToolKits/go-http-proxy/goProxy.go:25.9,27.4 1 1
6-
github.com/GolangToolKits/go-http-proxy/goProxy.go:28.8,32.19 4 1
7-
github.com/GolangToolKits/go-http-proxy/goProxy.go:32.19,35.4 2 1
8-
github.com/GolangToolKits/go-http-proxy/goProxy.go:35.9,38.4 2 1
9-
github.com/GolangToolKits/go-http-proxy/goProxy.go:40.2,40.24 1 1
10-
github.com/GolangToolKits/go-http-proxy/goProxy.go:44.31,46.2 1 1
11-
github.com/GolangToolKits/go-http-proxy/mockGoProxy.go:17.66,21.18 4 1
12-
github.com/GolangToolKits/go-http-proxy/mockGoProxy.go:21.18,25.3 3 1
13-
github.com/GolangToolKits/go-http-proxy/mockGoProxy.go:26.2,26.41 1 1
14-
github.com/GolangToolKits/go-http-proxy/mockGoProxy.go:30.35,32.2 1 1
2+
github.com/GolangToolKits/go-http-proxy/goProxy.go:16.62,21.52 5 1
3+
github.com/GolangToolKits/go-http-proxy/goProxy.go:21.52,24.18 3 1
4+
github.com/GolangToolKits/go-http-proxy/goProxy.go:24.18,26.4 1 1
5+
github.com/GolangToolKits/go-http-proxy/goProxy.go:26.9,28.4 1 1
6+
github.com/GolangToolKits/go-http-proxy/goProxy.go:29.8,33.19 4 1
7+
github.com/GolangToolKits/go-http-proxy/goProxy.go:33.19,36.4 2 1
8+
github.com/GolangToolKits/go-http-proxy/goProxy.go:36.9,39.4 2 1
9+
github.com/GolangToolKits/go-http-proxy/goProxy.go:41.2,41.24 1 1
10+
github.com/GolangToolKits/go-http-proxy/goProxy.go:45.68,51.52 6 1
11+
github.com/GolangToolKits/go-http-proxy/goProxy.go:51.52,54.18 3 1
12+
github.com/GolangToolKits/go-http-proxy/goProxy.go:54.18,56.4 1 0
13+
github.com/GolangToolKits/go-http-proxy/goProxy.go:56.9,58.4 1 1
14+
github.com/GolangToolKits/go-http-proxy/goProxy.go:59.8,62.17 3 1
15+
github.com/GolangToolKits/go-http-proxy/goProxy.go:62.17,66.4 3 1
16+
github.com/GolangToolKits/go-http-proxy/goProxy.go:68.2,68.29 1 1
17+
github.com/GolangToolKits/go-http-proxy/goProxy.go:72.31,74.2 1 1
18+
github.com/GolangToolKits/go-http-proxy/mockGoProxy.go:18.66,22.18 4 1
19+
github.com/GolangToolKits/go-http-proxy/mockGoProxy.go:22.18,26.3 3 1
20+
github.com/GolangToolKits/go-http-proxy/mockGoProxy.go:27.2,27.41 1 1
21+
github.com/GolangToolKits/go-http-proxy/mockGoProxy.go:31.72,35.16 4 1
22+
github.com/GolangToolKits/go-http-proxy/mockGoProxy.go:35.16,37.3 1 1
23+
github.com/GolangToolKits/go-http-proxy/mockGoProxy.go:38.2,38.46 1 1
24+
github.com/GolangToolKits/go-http-proxy/mockGoProxy.go:43.35,45.2 1 1

goProxy.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package goproxy
33

44
import (
55
"encoding/json"
6+
"io"
67
"log"
78
"net/http"
89
)
@@ -40,6 +41,33 @@ func (p *GoProxy) Do(req *http.Request, obj any) (bool, int) {
4041
return suc, statusCode
4142
}
4243

44+
// DoNonJSON DoNonJSON
45+
func (p *GoProxy) DoNonJSON(req *http.Request) (bool, int, []byte) {
46+
var suc bool
47+
var statusCode int
48+
var rtn []byte
49+
client := &http.Client{}
50+
resp, err := client.Do(req)
51+
if err != nil || resp.StatusCode != http.StatusOK {
52+
log.Println("go-http-proxy Do err: ", err)
53+
log.Println("resp in fail: ", resp)
54+
if resp != nil {
55+
statusCode = resp.StatusCode
56+
} else {
57+
statusCode = http.StatusNotFound
58+
}
59+
} else {
60+
defer resp.Body.Close()
61+
b, err := io.ReadAll(resp.Body)
62+
if err == nil {
63+
rtn = b
64+
statusCode = resp.StatusCode
65+
suc = true
66+
}
67+
}
68+
return suc, statusCode, rtn
69+
}
70+
4371
// New New proxy
4472
func (p *GoProxy) New() Proxy {
4573
return p

goProxy_test.go

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ func TestGoProxy_Do(t *testing.T) {
5959

6060
r2, _ := http.NewRequest("GET", "http:///", nil)
6161

62-
6362
r4, rErr4 := http.NewRequest("GET", sURL4, nil)
6463
if rErr4 != nil {
6564
fmt.Println("request error: ", rErr4)
@@ -133,3 +132,65 @@ func TestGoProxy_Do(t *testing.T) {
133132
})
134133
}
135134
}
135+
136+
func TestGoProxy_DoNonJSON(t *testing.T) {
137+
138+
var url = "https://media.licdn.com/dms/image/C5603AQGRApW88KjOCA/profile-displayphoto-shrink_100_100/0/1516940037267?e=1691625600&v=beta&t=Ibi46xLe0v7RvwvFcBmhhWWWdr19bQtOJR3ebyrIt-k"
139+
r, rErr := http.NewRequest("GET", url, nil)
140+
if rErr != nil {
141+
fmt.Println("request error: ", rErr)
142+
}
143+
144+
r2, rErr2 := http.NewRequest("GET", "httppp:////", nil)
145+
if rErr2 != nil {
146+
fmt.Println("request error: ", rErr2)
147+
}
148+
149+
type args struct {
150+
req *http.Request
151+
}
152+
tests := []struct {
153+
name string
154+
p *GoProxy
155+
args args
156+
want bool
157+
want1 int
158+
want2 int
159+
}{
160+
// TODO: Add test cases.
161+
{
162+
name: "test 1",
163+
args: args{
164+
req: r,
165+
},
166+
want: true,
167+
want1: 200,
168+
want2: 0,
169+
},
170+
{
171+
name: "test 2",
172+
args: args{
173+
req: r2,
174+
},
175+
want: false,
176+
want1: 404,
177+
want2: 1,
178+
},
179+
}
180+
for _, tt := range tests {
181+
t.Run(tt.name, func(t *testing.T) {
182+
px := &GoProxy{}
183+
p := px.New()
184+
got, got1, got2 := p.DoNonJSON(tt.args.req)
185+
if got != tt.want {
186+
t.Errorf("GoProxy.DoNonJSON() got = %v, want %v", got, tt.want)
187+
}
188+
if got1 != tt.want1 {
189+
t.Errorf("GoProxy.DoNonJSON() got1 = %v, want %v", got1, tt.want1)
190+
}
191+
if len(got2) == tt.want2 {
192+
t.Errorf("GoProxy.DoNonJSON() got2 = %v, want %v", got2, tt.want2)
193+
}
194+
})
195+
}
196+
}

mockGoProxy.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package goproxy
22

33
import (
44
"encoding/json"
5+
"io"
56
"log"
67
"net/http"
78
)
@@ -26,6 +27,18 @@ func (p *MockGoProxy) Do(req *http.Request, obj any) (bool, int) {
2627
return p.MockDoSuccess1, p.MockRespCode
2728
}
2829

30+
// DoNonJSON DoNonJSON
31+
func (p *MockGoProxy) DoNonJSON(req *http.Request) (bool, int, []byte) {
32+
defer p.MockResp.Body.Close()
33+
var rtn []byte
34+
b, err := io.ReadAll(p.MockResp.Body)
35+
if err == nil {
36+
rtn = b
37+
}
38+
return p.MockDoSuccess1, p.MockRespCode, rtn
39+
40+
}
41+
2942
// New New proxy
3043
func (p *MockGoProxy) New() Proxy {
3144
return p

0 commit comments

Comments
 (0)