mirror of
https://gitee.com/johng/gf.git
synced 2024-11-29 18:57:44 +08:00
add http instance function for package gins
This commit is contained in:
parent
be0df90d05
commit
c906990b63
27
frame/gins/gins_httpclient.go
Normal file
27
frame/gins/gins_httpclient.go
Normal file
@ -0,0 +1,27 @@
|
||||
// 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 gins
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
)
|
||||
|
||||
const (
|
||||
frameCoreComponentNameHttpClient = "gf.core.component.httpclient"
|
||||
)
|
||||
|
||||
// HttpClient returns an instance of http client with specified name.
|
||||
func HttpClient(name ...interface{}) *ghttp.Client {
|
||||
var (
|
||||
instanceKey = fmt.Sprintf("%s.%v", frameCoreComponentNameHttpClient, name)
|
||||
)
|
||||
return localInstances.GetOrSetFuncLock(instanceKey, func() interface{} {
|
||||
return ghttp.NewClient()
|
||||
}).(*ghttp.Client)
|
||||
}
|
30
frame/gins/gins_z_unit_httpclient_test.go
Normal file
30
frame/gins/gins_z_unit_httpclient_test.go
Normal file
@ -0,0 +1,30 @@
|
||||
// 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 gins_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/gins"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
)
|
||||
|
||||
func Test_Client(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
c = gins.HttpClient()
|
||||
c1 = gins.HttpClient("c1")
|
||||
c2 = gins.HttpClient("c2")
|
||||
)
|
||||
c.SetAgent("test1")
|
||||
c.SetAgent("test2")
|
||||
t.AssertNE(fmt.Sprintf(`%p`, c), fmt.Sprintf(`%p`, c1))
|
||||
t.AssertNE(fmt.Sprintf(`%p`, c), fmt.Sprintf(`%p`, c2))
|
||||
t.AssertNE(fmt.Sprintf(`%p`, c1), fmt.Sprintf(`%p`, c2))
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue
Block a user