1
1
package qhttp
2
2
3
+ //https://github.com/imroc/req
4
+
3
5
import (
4
6
"fmt"
7
+ "github.com/imroc/req/v3"
8
+ "log"
5
9
"testing"
10
+ "time"
6
11
)
7
12
8
13
const MD = "https://raw.githubusercontent.com/luban-agi/Awesome-AIGC-Tutorials/main/README.md"
@@ -16,3 +21,124 @@ func TestHttpClient_Get(t *testing.T) {
16
21
fmt .Println (statusCode , md )
17
22
fmt .Println (statusCode1 , md_zh )
18
23
}
24
+
25
+ func TestReqMode (t * testing.T ) {
26
+ req .DevMode ()
27
+ req .EnableAutoDecode ()
28
+ req .EnableForceHTTP1 ()
29
+ var result = req .MustGet ("https://httpbin.org/uuid" )
30
+ print (result )
31
+ }
32
+
33
+ type ErrorMessage struct {
34
+ Message string `json:"message"`
35
+ }
36
+
37
+ func (msg * ErrorMessage ) Error () string {
38
+ return fmt .Sprintf ("API Error: %s" , msg .Message )
39
+ }
40
+
41
+ type UserInfo struct {
42
+ Name string `json:"name"`
43
+ Blog string `json:"blog"`
44
+ }
45
+
46
+ func TestAdvancedUsage (t * testing.T ) {
47
+ client := req .C ().
48
+ SetUserAgent ("my-custom-client" ). // Chainable client settings.
49
+ SetTimeout (5 * time .Second ).
50
+ OnAfterResponse (func (client * req.Client , resp * req.Response ) error {
51
+ if resp .Err != nil { // There is an underlying error, e.g. network error or unmarshal error.
52
+ return nil
53
+ }
54
+ if errMsg , ok := resp .ErrorResult ().(* ErrorMessage ); ok {
55
+ resp .Err = errMsg // Convert api error into go error
56
+ return nil
57
+ }
58
+ if ! resp .IsSuccessState () {
59
+ // Neither a success response nor a error response, record details to help troubleshooting
60
+ resp .Err = fmt .Errorf ("bad status: %s\n raw content:\n %s" , resp .Status , resp .Dump ())
61
+ }
62
+ return nil
63
+ })
64
+
65
+ var userInfo UserInfo
66
+ var errMsg ErrorMessage
67
+ resp , err := client .R ().
68
+ SetHeader ("Accept" , "application/vnd.github.v3+json" ). // Chainable request settings.
69
+ SetPathParam ("username" , "imroc" ). // Replace path variable in url.
70
+ SetSuccessResult (& userInfo ). // Unmarshal response body into userInfo automatically if status code is between 200 and 299.
71
+ SetErrorResult (& errMsg ). // Unmarshal response body into errMsg automatically if status code >= 400.
72
+ EnableDump (). // Enable dump at request level, only print dump content if there is an error or some unknown situation occurs to help troubleshoot.
73
+ Get ("https://api.github.com/users/{username}" )
74
+
75
+ if err != nil { // Error handling.
76
+ log .Println ("error:" , err )
77
+ log .Println ("raw content:" )
78
+ log .Println (resp .Dump ()) // Record raw content when error occurs.
79
+ return
80
+ }
81
+
82
+ if resp .IsErrorState () { // Status code >= 400.
83
+ fmt .Println (errMsg .Message ) // Record error message returned.
84
+ return
85
+ }
86
+
87
+ if resp .IsSuccessState () { // Status code is between 200 and 299.
88
+ fmt .Printf ("%s (%s)\n " , userInfo .Name , userInfo .Blog )
89
+ return
90
+ }
91
+
92
+ // Unknown status code.
93
+ log .Println ("unknown status" , resp .Status )
94
+ log .Println ("raw content:" )
95
+ log .Println (resp .Dump ()) // Record raw content when server returned unknown status code.
96
+ }
97
+
98
+ type Repo struct {
99
+ Name string `json:"name"`
100
+ Url string `json:"url"`
101
+ }
102
+
103
+ type Result struct {
104
+ Data string `json:"data"`
105
+ }
106
+
107
+ func TestSimplePost (t * testing.T ) {
108
+ client := req .C ().DevMode ()
109
+ var result Result
110
+
111
+ resp , err := client .R ().
112
+ SetBody (& Repo {Name : "req" , Url : "https://github.com/imroc/req" }).
113
+ SetSuccessResult (& result ).
114
+ Post ("https://httpbin.org/post" )
115
+ if err != nil {
116
+ log .Fatal (err )
117
+ }
118
+
119
+ if ! resp .IsSuccessState () {
120
+ fmt .Println ("bad response status:" , resp .Status )
121
+ return
122
+ }
123
+ fmt .Println ("++++++++++++++++++++++++++++++++++++++++++++++++" )
124
+ fmt .Println ("data:" , result .Data )
125
+ fmt .Println ("++++++++++++++++++++++++++++++++++++++++++++++++" )
126
+ }
127
+
128
+ type APIResponse struct {
129
+ Origin string `json:"origin"`
130
+ Url string `json:"url"`
131
+ }
132
+
133
+ func TestApiStyleReq (t * testing.T ) {
134
+ var resp APIResponse
135
+ c := req .C ().SetBaseURL ("https://httpbin.org/post" )
136
+ err := c .Post ().
137
+ SetBody ("hello" ).
138
+ Do ().
139
+ Into (& resp )
140
+ if err != nil {
141
+ panic (err )
142
+ }
143
+ fmt .Println ("My IP is" , resp .Origin )
144
+ }
0 commit comments