Verify / verify (push) Canceled after 0s
抽取 Gin、配置、日志、数据库、Redis 与出站 HTTP 基础能力。 新增统一接入指南,将能力清单和相对链接检查纳入 make verify。 对齐远端 module 路径,修正 Redis 读写超时特殊值映射。 验证:make verify、make race-check 均通过。
38 lines
855 B
Go
38 lines
855 B
Go
package httpclient_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
|
|
"git.zhangdeman.cn/github/ginframework/httpclient"
|
|
)
|
|
|
|
func ExampleClient_Send() {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"name":"world"}`))
|
|
}))
|
|
defer server.Close()
|
|
client, err := httpclient.New(httpclient.Config{}, httpclient.Options{})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer client.CloseIdleConnections()
|
|
response, err := client.Send(context.Background(), httpclient.Request{URL: server.URL})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
var (
|
|
output struct {
|
|
Name string `json:"name"`
|
|
}
|
|
)
|
|
if err = response.DecodeJSON(&output); err != nil {
|
|
panic(err)
|
|
}
|
|
fmt.Println(response.StatusCode, output.Name)
|
|
// Output: 200 world
|
|
}
|