Skip to content

Commit

Permalink
fix: revert fetcher API for backwards-compatibility (#760)
Browse files Browse the repository at this point in the history
  • Loading branch information
alnr authored Feb 2, 2024
1 parent 0cdec1a commit a6f7cce
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
14 changes: 12 additions & 2 deletions fetcher/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,23 @@ func NewFetcher(opts ...Modifier) *Fetcher {
}

// Fetch fetches the file contents from the source.
func (f *Fetcher) Fetch(source string) ([]byte, error) {
func (f *Fetcher) Fetch(source string) (*bytes.Buffer, error) {
return f.FetchContext(context.Background(), source)
}

// FetchContext fetches the file contents from the source and allows to pass a
// context that is used for HTTP requests.
func (f *Fetcher) FetchContext(ctx context.Context, source string) ([]byte, error) {
func (f *Fetcher) FetchContext(ctx context.Context, source string) (*bytes.Buffer, error) {
b, err := f.FetchBytes(ctx, source)
if err != nil {
return nil, err
}
return bytes.NewBuffer(b), nil
}

// FetchBytes fetches the file contents from the source and allows to pass a
// context that is used for HTTP requests.
func (f *Fetcher) FetchBytes(ctx context.Context, source string) ([]byte, error) {
switch s := stringsx.SwitchPrefix(source); {
case s.HasPrefix("http://"), s.HasPrefix("https://"):
return f.fetchRemote(ctx, source)
Expand Down
6 changes: 3 additions & 3 deletions fetcher/fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestFetcher(t *testing.T) {
t.Run(fmt.Sprintf("config=%d/case=%d", fc, k), func(t *testing.T) {
actual, err := fetcher.Fetch(tc.source)
require.NoError(t, err)
assert.JSONEq(t, tc.expect, string(actual))
assert.JSONEq(t, tc.expect, actual.String())
})
}
}
Expand Down Expand Up @@ -115,7 +115,7 @@ func TestFetcher(t *testing.T) {

res, err := f.Fetch(srv.URL)
require.NoError(t, err)
require.Equal(t, "toodaloo", string(res))
require.Equal(t, "toodaloo", res.String())

require.EqualValues(t, 1, atomic.LoadInt32(&hits))

Expand All @@ -124,7 +124,7 @@ func TestFetcher(t *testing.T) {
for i := 0; i < 100; i++ {
res2, err := f.Fetch(srv.URL)
require.NoError(t, err)
require.Equal(t, "toodaloo", string(res2))
require.Equal(t, "toodaloo", res2.String())
if &res == &res2 {
t.Fatalf("cache should not return the same pointer")
}
Expand Down
3 changes: 1 addition & 2 deletions jwksx/fetcher_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package jwksx

import (
"bytes"
"context"
"crypto/sha256"
"time"
Expand Down Expand Up @@ -157,7 +156,7 @@ func (f *FetcherNext) fetch(ctx context.Context, location string, opts *fetcherN
return nil, err
}

set, err := jwk.ParseReader(bytes.NewBuffer(result))
set, err := jwk.ParseReader(result)
if err != nil {
return nil, errors.WithStack(herodot.ErrBadRequest.WithReason("failed to parse JWK set").WithWrap(err))
}
Expand Down

0 comments on commit a6f7cce

Please sign in to comment.