-
Notifications
You must be signed in to change notification settings - Fork 962
Added ability to retry unauthorized requests #8351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e8729b0
Added ability to retry unauthorized requests
maneesht e0a9211
Undid comment change
maneesht fa933a3
Removed emulator.json file
maneesht b39b8cc
Removed todo
maneesht f8bad0b
Merge remote-tracking branch 'public/dataconnect' into mtewani/allow-…
maneesht 20ff671
Update API reports
maneesht File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# @firebase/data-connect | ||
## UNRELEASED | ||
* Updated reporting to use @firebase/data-connect instead of @firebase/connect | ||
* Added functionality to retry queries and mutations if the server responds with UNAUTHENTICATED. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { FirebaseAuthTokenData } from '@firebase/auth-interop-types'; | ||
import { | ||
AuthTokenListener, | ||
AuthTokenProvider, | ||
DataConnectOptions, | ||
FirebaseAuthProvider | ||
} from '../../src'; | ||
import { RESTTransport } from '../../src/network/transport/rest'; | ||
import { initializeFetch } from '../../src/network/fetch'; | ||
import { expect } from 'chai'; | ||
import * as chai from 'chai'; | ||
import chaiAsPromised from 'chai-as-promised'; | ||
import * as sinon from 'sinon'; | ||
chai.use(chaiAsPromised); | ||
const options: DataConnectOptions = { | ||
connector: 'c', | ||
location: 'l', | ||
projectId: 'p', | ||
service: 's' | ||
}; | ||
const INITIAL_TOKEN = 'initial token'; | ||
class FakeAuthProvider implements AuthTokenProvider { | ||
private token: string | null = INITIAL_TOKEN; | ||
addTokenChangeListener(listener: AuthTokenListener): void {} | ||
getToken(forceRefresh: boolean): Promise<FirebaseAuthTokenData | null> { | ||
if (!forceRefresh) { | ||
return Promise.resolve({ accessToken: this.token! }); | ||
} | ||
return Promise.resolve({ accessToken: 'testToken' }); | ||
} | ||
setToken(_token: string | null) { | ||
this.token = _token; | ||
} | ||
} | ||
const json = { | ||
message: 'unauthorized' | ||
}; | ||
|
||
const fakeFetchImpl = sinon.stub().returns( | ||
Promise.resolve({ | ||
json: () => { | ||
return Promise.resolve(json); | ||
}, | ||
status: 401 | ||
} as Response) | ||
); | ||
describe('Queries', () => { | ||
afterEach(() => { | ||
fakeFetchImpl.resetHistory(); | ||
}); | ||
it('[QUERY] should retry auth whenever the fetcher returns with unauthorized', async () => { | ||
initializeFetch(fakeFetchImpl); | ||
const authProvider = new FakeAuthProvider(); | ||
const rt = new RESTTransport(options, undefined, authProvider); | ||
await expect( | ||
rt.invokeQuery('test', null) | ||
).to.eventually.be.rejectedWith(JSON.stringify(json)); | ||
expect(fakeFetchImpl.callCount).to.eq(2); | ||
}); | ||
it('[MUTATION] should retry auth whenever the fetcher returns with unauthorized', async () => { | ||
initializeFetch(fakeFetchImpl); | ||
const authProvider = new FakeAuthProvider(); | ||
const rt = new RESTTransport(options, undefined, authProvider); | ||
await expect( | ||
rt.invokeMutation('test', null) | ||
).to.eventually.be.rejectedWith(JSON.stringify(json)); | ||
expect(fakeFetchImpl.callCount).to.eq(2); | ||
}); | ||
it("should not retry auth whenever the fetcher returns with unauthorized and the token doesn't change", async () => { | ||
initializeFetch(fakeFetchImpl); | ||
const authProvider = new FakeAuthProvider(); | ||
const rt = new RESTTransport(options, undefined, authProvider); | ||
rt._setLastToken('initial token'); | ||
await expect( | ||
rt.invokeQuery('test', null) as Promise<any> | ||
).to.eventually.be.rejectedWith(JSON.stringify(json)); | ||
expect(fakeFetchImpl.callCount).to.eq(1); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just realized I forgot to check for a changed token in Android! I'll go ahead and implement that.