mirror of
https://gitee.com/johng/gf.git
synced 2024-12-02 20:28:17 +08:00
402 lines
9.8 KiB
Go
402 lines
9.8 KiB
Go
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
|
//
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
// If a copy of the MIT was not distributed with this file,
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
package gclient_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
)
|
|
|
|
func init() {
|
|
// Default server for client.
|
|
p := 8999
|
|
s := g.Server(p)
|
|
// HTTP method handlers.
|
|
s.Group("/", func(group *ghttp.RouterGroup) {
|
|
group.GET("/", func(r *ghttp.Request) {
|
|
r.Response.Writef(
|
|
"GET: query: %d, %s",
|
|
r.GetQuery("id").Int(),
|
|
r.GetQuery("name").String(),
|
|
)
|
|
})
|
|
group.PUT("/", func(r *ghttp.Request) {
|
|
r.Response.Writef(
|
|
"PUT: form: %d, %s",
|
|
r.GetForm("id").Int(),
|
|
r.GetForm("name").String(),
|
|
)
|
|
})
|
|
group.POST("/", func(r *ghttp.Request) {
|
|
r.Response.Writef(
|
|
"POST: form: %d, %s",
|
|
r.GetForm("id").Int(),
|
|
r.GetForm("name").String(),
|
|
)
|
|
})
|
|
group.DELETE("/", func(r *ghttp.Request) {
|
|
r.Response.Writef(
|
|
"DELETE: form: %d, %s",
|
|
r.GetForm("id").Int(),
|
|
r.GetForm("name").String(),
|
|
)
|
|
})
|
|
group.HEAD("/", func(r *ghttp.Request) {
|
|
r.Response.Write("head")
|
|
})
|
|
group.OPTIONS("/", func(r *ghttp.Request) {
|
|
r.Response.Write("options")
|
|
})
|
|
})
|
|
// Client chaining operations handlers.
|
|
s.Group("/", func(group *ghttp.RouterGroup) {
|
|
group.ALL("/header", func(r *ghttp.Request) {
|
|
r.Response.Writef(
|
|
"Span-Id: %s, Trace-Id: %s",
|
|
r.Header.Get("Span-Id"),
|
|
r.Header.Get("Trace-Id"),
|
|
)
|
|
})
|
|
group.ALL("/cookie", func(r *ghttp.Request) {
|
|
r.Response.Writef(
|
|
"SessionId: %s",
|
|
r.Cookie.Get("SessionId"),
|
|
)
|
|
})
|
|
group.ALL("/json", func(r *ghttp.Request) {
|
|
r.Response.Writef(
|
|
"Content-Type: %s, id: %d",
|
|
r.Header.Get("Content-Type"),
|
|
r.Get("id").Int(),
|
|
)
|
|
})
|
|
})
|
|
// Other testing handlers.
|
|
s.Group("/var", func(group *ghttp.RouterGroup) {
|
|
group.ALL("/json", func(r *ghttp.Request) {
|
|
r.Response.Write(`{"id":1,"name":"john"}`)
|
|
})
|
|
group.ALL("/jsons", func(r *ghttp.Request) {
|
|
r.Response.Write(`[{"id":1,"name":"john"}, {"id":2,"name":"smith"}]`)
|
|
})
|
|
})
|
|
s.SetAccessLogEnabled(false)
|
|
s.SetDumpRouterMap(false)
|
|
s.SetPort(p)
|
|
err := s.Start()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
time.Sleep(time.Millisecond * 500)
|
|
}
|
|
|
|
func ExampleClient_Header() {
|
|
var (
|
|
url = "http://127.0.0.1:8999/header"
|
|
header = g.MapStrStr{
|
|
"Span-Id": "0.1",
|
|
"Trace-Id": "123456789",
|
|
}
|
|
)
|
|
content := g.Client().Header(header).PostContent(ctx, url, g.Map{
|
|
"id": 10000,
|
|
"name": "john",
|
|
})
|
|
fmt.Println(content)
|
|
|
|
// Output:
|
|
// Span-Id: 0.1, Trace-Id: 123456789
|
|
}
|
|
|
|
func ExampleClient_HeaderRaw() {
|
|
var (
|
|
url = "http://127.0.0.1:8999/header"
|
|
headerRaw = `
|
|
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3950.0 Safari/537.36
|
|
Span-Id: 0.1
|
|
Trace-Id: 123456789
|
|
`
|
|
)
|
|
content := g.Client().HeaderRaw(headerRaw).PostContent(ctx, url, g.Map{
|
|
"id": 10000,
|
|
"name": "john",
|
|
})
|
|
fmt.Println(content)
|
|
|
|
// Output:
|
|
// Span-Id: 0.1, Trace-Id: 123456789
|
|
}
|
|
|
|
func ExampleClient_Cookie() {
|
|
var (
|
|
url = "http://127.0.0.1:8999/cookie"
|
|
cookie = g.MapStrStr{
|
|
"SessionId": "123",
|
|
}
|
|
)
|
|
content := g.Client().Cookie(cookie).PostContent(ctx, url, g.Map{
|
|
"id": 10000,
|
|
"name": "john",
|
|
})
|
|
fmt.Println(content)
|
|
|
|
// Output:
|
|
// SessionId: 123
|
|
}
|
|
|
|
func ExampleClient_ContentJson() {
|
|
var (
|
|
url = "http://127.0.0.1:8999/json"
|
|
jsonStr = `{"id":10000,"name":"john"}`
|
|
jsonMap = g.Map{
|
|
"id": 10000,
|
|
"name": "john",
|
|
}
|
|
)
|
|
// Post using JSON string.
|
|
fmt.Println(g.Client().ContentJson().PostContent(ctx, url, jsonStr))
|
|
// Post using JSON map.
|
|
fmt.Println(g.Client().ContentJson().PostContent(ctx, url, jsonMap))
|
|
|
|
// Output:
|
|
// Content-Type: application/json, id: 10000
|
|
// Content-Type: application/json, id: 10000
|
|
}
|
|
|
|
func ExampleClient_Post() {
|
|
url := "http://127.0.0.1:8999"
|
|
// Send with string parameter in request body.
|
|
r1, err := g.Client().Post(ctx, url, "id=10000&name=john")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer r1.Close()
|
|
fmt.Println(r1.ReadAllString())
|
|
|
|
// Send with map parameter.
|
|
r2, err := g.Client().Post(ctx, url, g.Map{
|
|
"id": 10000,
|
|
"name": "john",
|
|
})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer r2.Close()
|
|
fmt.Println(r2.ReadAllString())
|
|
|
|
// Output:
|
|
// POST: form: 10000, john
|
|
// POST: form: 10000, john
|
|
}
|
|
|
|
func ExampleClient_PostBytes() {
|
|
url := "http://127.0.0.1:8999"
|
|
fmt.Println(string(g.Client().PostBytes(ctx, url, g.Map{
|
|
"id": 10000,
|
|
"name": "john",
|
|
})))
|
|
|
|
// Output:
|
|
// POST: form: 10000, john
|
|
}
|
|
|
|
func ExampleClient_PostContent() {
|
|
url := "http://127.0.0.1:8999"
|
|
fmt.Println(g.Client().PostContent(ctx, url, g.Map{
|
|
"id": 10000,
|
|
"name": "john",
|
|
}))
|
|
|
|
// Output:
|
|
// POST: form: 10000, john
|
|
}
|
|
|
|
func ExampleClient_PostVar() {
|
|
type User struct {
|
|
Id int
|
|
Name string
|
|
}
|
|
var (
|
|
users []User
|
|
url = "http://127.0.0.1:8999/var/jsons"
|
|
)
|
|
err := g.Client().PostVar(ctx, url).Scan(&users)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
fmt.Println(users)
|
|
|
|
// Output:
|
|
// [{1 john} {2 smith}]
|
|
}
|
|
|
|
func ExampleClient_Get() {
|
|
var (
|
|
ctx = context.Background()
|
|
url = "http://127.0.0.1:8999"
|
|
)
|
|
|
|
// Send with string parameter along with URL.
|
|
r1, err := g.Client().Get(ctx, url+"?id=10000&name=john")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer r1.Close()
|
|
fmt.Println(r1.ReadAllString())
|
|
|
|
// Send with string parameter in request body.
|
|
r2, err := g.Client().Get(ctx, url, "id=10000&name=john")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer r2.Close()
|
|
fmt.Println(r2.ReadAllString())
|
|
|
|
// Send with map parameter.
|
|
r3, err := g.Client().Get(ctx, url, g.Map{
|
|
"id": 10000,
|
|
"name": "john",
|
|
})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer r3.Close()
|
|
fmt.Println(r3.ReadAllString())
|
|
|
|
// Output:
|
|
// GET: query: 10000, john
|
|
// GET: query: 10000, john
|
|
// GET: query: 10000, john
|
|
}
|
|
|
|
func ExampleClient_GetBytes() {
|
|
var (
|
|
ctx = context.Background()
|
|
url = "http://127.0.0.1:8999"
|
|
)
|
|
fmt.Println(string(g.Client().GetBytes(ctx, url, g.Map{
|
|
"id": 10000,
|
|
"name": "john",
|
|
})))
|
|
|
|
// Output:
|
|
// GET: query: 10000, john
|
|
}
|
|
|
|
func ExampleClient_GetContent() {
|
|
url := "http://127.0.0.1:8999"
|
|
fmt.Println(g.Client().GetContent(ctx, url, g.Map{
|
|
"id": 10000,
|
|
"name": "john",
|
|
}))
|
|
|
|
// Output:
|
|
// GET: query: 10000, john
|
|
}
|
|
|
|
func ExampleClient_GetVar() {
|
|
type User struct {
|
|
Id int
|
|
Name string
|
|
}
|
|
var (
|
|
user *User
|
|
ctx = context.Background()
|
|
url = "http://127.0.0.1:8999/var/json"
|
|
)
|
|
err := g.Client().GetVar(ctx, url).Scan(&user)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
fmt.Println(user)
|
|
|
|
// Output:
|
|
// &{1 john}
|
|
}
|
|
|
|
// ExampleClient_SetProxy a example for `gclient.Client.SetProxy` method.
|
|
// please prepare two proxy server before running this example.
|
|
// http proxy server listening on `127.0.0.1:1081`
|
|
// socks5 proxy server listening on `127.0.0.1:1080`
|
|
func ExampleClient_SetProxy() {
|
|
// connect to a http proxy server
|
|
client := g.Client()
|
|
client.SetProxy("http://127.0.0.1:1081")
|
|
client.SetTimeout(5 * time.Second) // it's suggested to set http client timeout
|
|
response, err := client.Get(ctx, "https://api.ip.sb/ip")
|
|
if err != nil {
|
|
// err is not nil when your proxy server is down.
|
|
// eg. Get "https://api.ip.sb/ip": proxyconnect tcp: dial tcp 127.0.0.1:1087: connect: connection refused
|
|
fmt.Println(err)
|
|
}
|
|
response.RawDump()
|
|
// connect to a http proxy server which needs auth
|
|
client.SetProxy("http://user:password:127.0.0.1:1081")
|
|
client.SetTimeout(5 * time.Second) // it's suggested to set http client timeout
|
|
response, err = client.Get(ctx, "https://api.ip.sb/ip")
|
|
if err != nil {
|
|
// err is not nil when your proxy server is down.
|
|
// eg. Get "https://api.ip.sb/ip": proxyconnect tcp: dial tcp 127.0.0.1:1087: connect: connection refused
|
|
fmt.Println(err)
|
|
}
|
|
response.RawDump()
|
|
|
|
// connect to a socks5 proxy server
|
|
client.SetProxy("socks5://127.0.0.1:1080")
|
|
client.SetTimeout(5 * time.Second) // it's suggested to set http client timeout
|
|
response, err = client.Get(ctx, "https://api.ip.sb/ip")
|
|
if err != nil {
|
|
// err is not nil when your proxy server is down.
|
|
// eg. Get "https://api.ip.sb/ip": socks connect tcp 127.0.0.1:1087->api.ip.sb:443: dial tcp 127.0.0.1:1087: connect: connection refused
|
|
fmt.Println(err)
|
|
}
|
|
fmt.Println(response.RawResponse())
|
|
|
|
// connect to a socks5 proxy server which needs auth
|
|
client.SetProxy("socks5://user:password@127.0.0.1:1080")
|
|
client.SetTimeout(5 * time.Second) // it's suggested to set http client timeout
|
|
response, err = client.Get(ctx, "https://api.ip.sb/ip")
|
|
if err != nil {
|
|
// err is not nil when your proxy server is down.
|
|
// eg. Get "https://api.ip.sb/ip": socks connect tcp 127.0.0.1:1087->api.ip.sb:443: dial tcp 127.0.0.1:1087: connect: connection refused
|
|
fmt.Println(err)
|
|
}
|
|
fmt.Println(response.RawResponse())
|
|
}
|
|
|
|
// ExampleClientChain_Proxy a chain version of example for `gclient.Client.Proxy` method.
|
|
// please prepare two proxy server before running this example.
|
|
// http proxy server listening on `127.0.0.1:1081`
|
|
// socks5 proxy server listening on `127.0.0.1:1080`
|
|
// for more details, please refer to ExampleClient_SetProxy
|
|
func ExampleClient_Proxy() {
|
|
var (
|
|
ctx = context.Background()
|
|
)
|
|
client := g.Client()
|
|
response, err := client.Proxy("http://127.0.0.1:1081").Get(ctx, "https://api.ip.sb/ip")
|
|
if err != nil {
|
|
// err is not nil when your proxy server is down.
|
|
// eg. Get "https://api.ip.sb/ip": proxyconnect tcp: dial tcp 127.0.0.1:1087: connect: connection refused
|
|
fmt.Println(err)
|
|
}
|
|
fmt.Println(response.RawResponse())
|
|
|
|
client2 := g.Client()
|
|
response, err = client2.Proxy("socks5://127.0.0.1:1080").Get(ctx, "https://api.ip.sb/ip")
|
|
if err != nil {
|
|
// err is not nil when your proxy server is down.
|
|
// eg. Get "https://api.ip.sb/ip": socks connect tcp 127.0.0.1:1087->api.ip.sb:443: dial tcp 127.0.0.1:1087: connect: connection refused
|
|
fmt.Println(err)
|
|
}
|
|
fmt.Println(response.RawResponse())
|
|
}
|