Skip to content

Commit

Permalink
Merge pull request #251 from mspindelhirn/fix/twitchtv#250
Browse files Browse the repository at this point in the history
fix context is used in response hook after request
  • Loading branch information
dpolansky authored Jul 24, 2020
2 parents 3c8a4fc + a7f214d commit ae31a3a
Show file tree
Hide file tree
Showing 20 changed files with 491 additions and 453 deletions.
54 changes: 27 additions & 27 deletions clientcompat/internal/clientcompat/clientcompat.twirp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 25 additions & 25 deletions example/service.twirp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions internal/twirptest/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,44 @@ func TestClientWithHooks(t *testing.T) {
}
}

func TestClientContextToHook(t *testing.T) {
h := PickyHatmaker(1)
s := httptest.NewServer(NewHaberdasherServer(h, nil))

type Key uint
const testKey Key = 0
requestCalled := false
responseCalled := false
hooks := &twirp.ClientHooks{
RequestPrepared: func(ctx context.Context, req *http.Request) (context.Context, error) {
requestCalled = true
return context.WithValue(ctx, testKey, "test-value"), nil
},
ResponseReceived: func(ctx context.Context) {
responseCalled = true
ctxVal := ctx.Value(testKey)
if "test-value" != ctxVal {
t.Errorf("context value set in RequestPrepared is not received in ResponseReceived: got %v, want test-value", ctxVal)
}
},
Error: func(ctx context.Context, err twirp.Error) {},
}

protoCli := NewHaberdasherProtobufClient(s.URL, &http.Client{}, twirp.WithClientHooks(hooks))
ctx := context.Background()
_, err := protoCli.MakeHat(ctx, &Size{Inches: 1})
if err != nil {
t.Error("unexpected error %w from MakeHat call", err)
}

if !requestCalled {
t.Error("misssing RequestPrepared hook call")
}
if !responseCalled {
t.Error("missing ResponseReceived hook call")
}
}

func TestClientIntermediaryErrors(t *testing.T) {
testcase := func(body string, code int, expectedErrorCode twirp.ErrorCode, clientMaker func(string, HTTPClient, ...twirp.ClientOption) Haberdasher) func(*testing.T) {
return func(t *testing.T) {
Expand Down
Loading

0 comments on commit ae31a3a

Please sign in to comment.