mirror of
https://github.com/therootcompany/golib.git
synced 2026-11-04 06:55:00 +00:00
feat(iplist): make HTTP client part of Load API
This commit is contained in:
parent
9cbf82fc16
commit
a8cc57c4c9
@ -32,13 +32,9 @@ const (
|
|||||||
// that are themselves HTTP(S) URLs are recursively expanded up to
|
// that are themselves HTTP(S) URLs are recursively expanded up to
|
||||||
// MaxNestedSources levels. URL responses are cached below cacheDir and the
|
// MaxNestedSources levels. URL responses are cached below cacheDir and the
|
||||||
// cache remains available across process restarts.
|
// cache remains available across process restarts.
|
||||||
func Load(ctx context.Context, source, cacheDir string) ([]string, error) {
|
// Load reads a source with an optional HTTP client. A nil client uses the
|
||||||
return LoadWithClient(ctx, source, cacheDir, nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
// LoadWithClient is Load with an optional HTTP client. A nil client uses the
|
|
||||||
// internal client with the package's standard timeout and transport policy.
|
// internal client with the package's standard timeout and transport policy.
|
||||||
func LoadWithClient(ctx context.Context, source, cacheDir string, client *http.Client) ([]string, error) {
|
func Load(ctx context.Context, source, cacheDir string, client *http.Client) ([]string, error) {
|
||||||
if client == nil {
|
if client == nil {
|
||||||
client = https.NewInternalClient()
|
client = https.NewInternalClient()
|
||||||
}
|
}
|
||||||
|
|||||||
@ -28,7 +28,7 @@ func TestLoadWithClient(t *testing.T) {
|
|||||||
called = true
|
called = true
|
||||||
return http.DefaultTransport.RoundTrip(r)
|
return http.DefaultTransport.RoundTrip(r)
|
||||||
})}
|
})}
|
||||||
entries, err := LoadWithClient(context.Background(), server.URL, t.TempDir(), client)
|
entries, err := Load(context.Background(), server.URL, t.TempDir(), client)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -54,7 +54,7 @@ func TestLoadURLAuthCacheAndNested(t *testing.T) {
|
|||||||
defer outer.Close()
|
defer outer.Close()
|
||||||
|
|
||||||
cacheDir := t.TempDir()
|
cacheDir := t.TempDir()
|
||||||
got, err := Load(context.Background(), strings.Replace(outer.URL, "http://", "http://u:p@", 1), cacheDir)
|
got, err := Load(context.Background(), strings.Replace(outer.URL, "http://", "http://u:p@", 1), cacheDir, nil)
|
||||||
if err != nil || !authOK || len(got) != 1 || got[0] != "192.0.2.2" {
|
if err != nil || !authOK || len(got) != 1 || got[0] != "192.0.2.2" {
|
||||||
t.Fatalf("auth=%v entries=%#v err=%v", authOK, got, err)
|
t.Fatalf("auth=%v entries=%#v err=%v", authOK, got, err)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user