Skip to content

Commit

Permalink
更新单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
lixizan committed Dec 8, 2022
1 parent 7f24adf commit 05bb6a2
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
@@ -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))
Expand Down Expand Up @@ -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)
}

0 comments on commit 05bb6a2

Please sign in to comment.