mirror of
https://github.com/duke-git/lancet.git
synced 2026-03-01 00:35:28 +08:00
Compare commits
2 Commits
2a796adf85
...
6853d627f4
| Author | SHA1 | Date | |
|---|---|---|---|
| 6853d627f4 | |||
| e461acdb72 |
@@ -108,6 +108,7 @@ type HttpClientConfig struct {
|
|||||||
HandshakeTimeout time.Duration
|
HandshakeTimeout time.Duration
|
||||||
ResponseTimeout time.Duration
|
ResponseTimeout time.Duration
|
||||||
Verbose bool
|
Verbose bool
|
||||||
|
Proxy *url.URL
|
||||||
}
|
}
|
||||||
|
|
||||||
// defaultHttpClientConfig defalut client config.
|
// defaultHttpClientConfig defalut client config.
|
||||||
@@ -163,6 +164,11 @@ func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient {
|
|||||||
client.TLS = config.TLSConfig
|
client.TLS = config.TLSConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if config.Proxy != nil {
|
||||||
|
transport := client.Client.Transport.(*http.Transport)
|
||||||
|
transport.Proxy = http.ProxyURL(config.Proxy)
|
||||||
|
}
|
||||||
|
|
||||||
return client
|
return client
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/duke-git/lancet/v2/internal"
|
"github.com/duke-git/lancet/v2/internal"
|
||||||
)
|
)
|
||||||
@@ -361,3 +362,23 @@ func TestSendRequestWithFilePath(t *testing.T) {
|
|||||||
t.Fatalf("expected %d, got %d", http.StatusOK, resp.StatusCode)
|
t.Fatalf("expected %d, got %d", http.StatusOK, resp.StatusCode)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestProxy(t *testing.T) {
|
||||||
|
config := &HttpClientConfig{
|
||||||
|
HandshakeTimeout: 20 * time.Second,
|
||||||
|
ResponseTimeout: 40 * time.Second,
|
||||||
|
// Use the proxy ip to add it here
|
||||||
|
//Proxy: &url.URL{
|
||||||
|
// Scheme: "http",
|
||||||
|
// Host: "46.17.63.166:18888",
|
||||||
|
//},
|
||||||
|
}
|
||||||
|
httpClient := NewHttpClientWithConfig(config)
|
||||||
|
resp, err := httpClient.Get("https://www.ipplus360.com/getLocation")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
t.Fatalf("expected %d, got %d", http.StatusOK, resp.StatusCode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+32
-11
@@ -50,12 +50,23 @@ func ContainBy[T any](slice []T, predicate func(item T) bool) bool {
|
|||||||
// ContainSubSlice check if the slice contain a given subslice or not.
|
// ContainSubSlice check if the slice contain a given subslice or not.
|
||||||
// Play: https://go.dev/play/p/bcuQ3UT6Sev
|
// Play: https://go.dev/play/p/bcuQ3UT6Sev
|
||||||
func ContainSubSlice[T comparable](slice, subSlice []T) bool {
|
func ContainSubSlice[T comparable](slice, subSlice []T) bool {
|
||||||
for _, v := range subSlice {
|
if len(subSlice) == 0 {
|
||||||
if !Contain(slice, v) {
|
return true
|
||||||
|
}
|
||||||
|
if len(slice) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
elementMap := make(map[T]struct{}, len(slice))
|
||||||
|
for _, item := range slice {
|
||||||
|
elementMap[item] = struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, item := range subSlice {
|
||||||
|
if _, ok := elementMap[item]; !ok {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,35 +92,41 @@ func Chunk[T any](slice []T, size int) [][]T {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compact creates an slice with all falsey values removed. The values false, nil, 0, and "" are falsey.
|
// Compact creates a slice with all falsey values removed. The values false, nil, 0, and "" are falsey.
|
||||||
// Play: https://go.dev/play/p/pO5AnxEr3TK
|
// Play: https://go.dev/play/p/pO5AnxEr3TK
|
||||||
func Compact[T comparable](slice []T) []T {
|
func Compact[T comparable](slice []T) []T {
|
||||||
var zero T
|
var zero T
|
||||||
|
|
||||||
result := []T{}
|
result := make([]T, 0, len(slice))
|
||||||
|
|
||||||
for _, v := range slice {
|
for _, v := range slice {
|
||||||
if v != zero {
|
if v != zero {
|
||||||
result = append(result, v)
|
result = append(result, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return result[:len(result):len(result)]
|
||||||
return result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Concat creates a new slice concatenating slice with any additional slices.
|
// Concat creates a new slice concatenating slice with any additional slices.
|
||||||
// Play: https://go.dev/play/p/gPt-q7zr5mk
|
// Play: https://go.dev/play/p/gPt-q7zr5mk
|
||||||
func Concat[T any](slice []T, slices ...[]T) []T {
|
func Concat[T any](slice []T, slices ...[]T) []T {
|
||||||
result := append([]T{}, slice...)
|
totalLen := len(slice)
|
||||||
|
|
||||||
for _, v := range slices {
|
for _, v := range slices {
|
||||||
result = append(result, v...)
|
totalLen += len(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
result := make([]T, 0, totalLen)
|
||||||
|
|
||||||
|
result = append(result, slice...)
|
||||||
|
for _, s := range slices {
|
||||||
|
result = append(result, s...)
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Difference creates an slice of whose element in slice but not in comparedSlice.
|
// Difference creates a slice of whose element in slice but not in comparedSlice.
|
||||||
// Play: https://go.dev/play/p/VXvadzLzhDa
|
// Play: https://go.dev/play/p/VXvadzLzhDa
|
||||||
func Difference[T comparable](slice, comparedSlice []T) []T {
|
func Difference[T comparable](slice, comparedSlice []T) []T {
|
||||||
result := []T{}
|
result := []T{}
|
||||||
@@ -826,7 +843,11 @@ func UnionBy[T any, V comparable](predicate func(item T) V, slices ...[]T) []T {
|
|||||||
// Merge all given slices into one slice.
|
// Merge all given slices into one slice.
|
||||||
// Play: https://go.dev/play/p/lbjFp784r9N
|
// Play: https://go.dev/play/p/lbjFp784r9N
|
||||||
func Merge[T any](slices ...[]T) []T {
|
func Merge[T any](slices ...[]T) []T {
|
||||||
result := make([]T, 0)
|
totalLen := 0
|
||||||
|
for _, v := range slices {
|
||||||
|
totalLen += len(v)
|
||||||
|
}
|
||||||
|
result := make([]T, 0, totalLen)
|
||||||
|
|
||||||
for _, v := range slices {
|
for _, v := range slices {
|
||||||
result = append(result, v...)
|
result = append(result, v...)
|
||||||
|
|||||||
+14
-2
@@ -2,12 +2,11 @@ package slice
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/internal"
|
||||||
"math"
|
"math"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/duke-git/lancet/v2/internal"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestContain(t *testing.T) {
|
func TestContain(t *testing.T) {
|
||||||
@@ -109,6 +108,19 @@ func TestConcat(t *testing.T) {
|
|||||||
assert.Equal([]int{1, 2, 3, 4, 5}, Concat([]int{1, 2, 3}, []int{4}, []int{5}))
|
assert.Equal([]int{1, 2, 3, 4, 5}, Concat([]int{1, 2, 3}, []int{4}, []int{5}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BenchmarkConcat(b *testing.B) {
|
||||||
|
slice1 := []int{1, 2, 3}
|
||||||
|
slice2 := []int{4, 5, 6}
|
||||||
|
slice3 := []int{7, 8, 9}
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
result := Concat(slice1, slice2, slice3)
|
||||||
|
if len(result) == 0 {
|
||||||
|
b.Fatal("unexpected empty result")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestEqual(t *testing.T) {
|
func TestEqual(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user