From 05bb6a2415a9e03e8adab98d06ce820dba7d63aa Mon Sep 17 00:00:00 2001 From: lixizan Date: Thu, 8 Dec 2022 17:09:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=8D=95=E5=85=83=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client_test.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/client_test.go b/client_test.go index 53d5158..df90828 100644 --- a/client_test.go +++ b/client_test.go @@ -1,12 +1,15 @@ package hasaki import ( + "context" "github.com/stretchr/testify/assert" "net/http" "testing" "time" ) +const testURL = "https://api.github.com" + func TestWithTimeout(t *testing.T) { var value = 5 * time.Second c, _ := NewClient(WithTimeout(value)) @@ -49,6 +52,26 @@ func TestWithTransport(t *testing.T) { func TestNewClient(t *testing.T) { cli, _ := NewClient() - resp := cli.Get("https://api.github.com/").Send(nil) + resp := cli.Get(testURL).Send(nil) assert.NoError(t, resp.Err()) } + +func TestWithBefore(t *testing.T) { + cli, _ := NewClient(WithBefore(func(ctx context.Context, request *http.Request) (context.Context, error) { + ctx = context.WithValue(ctx, "k", "v") + return ctx, nil + })) + result := cli.Get(testURL).Send(nil) + val := result.Context().Value("k") + assert.Equal(t, "v", val) +} + +func TestWithAfter(t *testing.T) { + cli, _ := NewClient(WithAfter(func(ctx context.Context, response *http.Response) (context.Context, error) { + ctx = context.WithValue(ctx, "k", "v") + return ctx, nil + })) + result := cli.Get(testURL).Send(nil) + val := result.Context().Value("k") + assert.Equal(t, "v", val) +}