Skip to content

Commit

Permalink
Check for Sync-Token before updating the cache (Azure#21600)
Browse files Browse the repository at this point in the history
Some responses, e.g. http.StatusBadRequest, won't return a Sync-Token.
  • Loading branch information
jhendrixMSFT committed Sep 20, 2023
1 parent 9464951 commit adeb6d3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
1 change: 1 addition & 0 deletions sdk/data/azappconfig/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Breaking Changes

### Bugs Fixed
* Check for a `Sync-Token` value before updating the cache.

### Other Changes

Expand Down
9 changes: 6 additions & 3 deletions sdk/data/azappconfig/internal/synctoken/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ func (p *Policy) Do(req *policy.Request) (*http.Response, error) {
return nil, err
}

// update the cache from the response
if err := p.cache.Set(exported.SyncToken(resp.Header.Get(syncTokenHeader))); err != nil {
return nil, &nonRetriableError{err}
// update the cache from the response if available.
// e.g. a 404 will include a Sync-Token but a 400 will not.
if st := resp.Header.Get(syncTokenHeader); st != "" {
if err := p.cache.Set(exported.SyncToken(st)); err != nil {
return nil, &nonRetriableError{err}
}
}

return resp, err
Expand Down
7 changes: 4 additions & 3 deletions sdk/data/azappconfig/internal/synctoken/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (

func TestPolicy(t *testing.T) {
srv, close := mock.NewServer()
srv.AppendResponse()
defer close()

cache := NewCache()
Expand All @@ -36,9 +35,11 @@ func TestPolicy(t *testing.T) {

req, err := runtime.NewRequest(context.Background(), http.MethodGet, srv.URL())
require.NoError(t, err)

srv.AppendResponse()
resp, err := pl.Do(req)
require.Error(t, err) // missing Sync-Token response header
require.Nil(t, resp)
require.NoError(t, err)
require.NotNil(t, resp)

srv.AppendResponse(mock.WithHeader(syncTokenHeader, "id=val"))
resp, err = pl.Do(req)
Expand Down

0 comments on commit adeb6d3

Please sign in to comment.