Skip to content

Populate GMPID header #2730

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 1 commit into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/storage/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
#Unreleased
- [Changed] Added an additional header to all network requests that propagates the Firebase App ID.

# 6.1.0
- [Feature] Added the support for List API.

3 changes: 2 additions & 1 deletion packages/storage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"dev": "rollup -c -w",
"test": "run-p test:browser lint",
"test:browser": "karma start --single-run",
"prepare": "yarn build"
"prepare": "yarn build",
"prettier": "prettier --write 'src/**/*.ts' 'test/**/*.ts'"
},
"license": "Apache-2.0",
"dependencies": {
Expand Down
9 changes: 8 additions & 1 deletion packages/storage/src/implementation/authwrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export class AuthWrapper {
private app_: FirebaseApp | null;
private authProvider_: Provider<FirebaseAuthInternalName>;
private bucket_: string | null = null;
private appId_: string | null = null;

private storageRefMaker_: (p1: AuthWrapper, p2: Location) => Reference;
private requestMaker_: requestMaker;
Expand All @@ -67,6 +68,7 @@ export class AuthWrapper {
const options = this.app_.options;
if (type.isDef(options)) {
this.bucket_ = AuthWrapper.extractBucket_(options);
this.appId_ = options.appId ?? null;
}
}
this.authProvider_ = authProvider;
Expand Down Expand Up @@ -138,7 +140,12 @@ export class AuthWrapper {
authToken: string | null
): Request<T> {
if (!this.deleted_) {
const request = this.requestMaker_(requestInfo, authToken, this.pool_);
const request = this.requestMaker_(
requestInfo,
this.appId_,
authToken,
this.pool_
);
this.requestMap_.addRequest(request);
return request;
} else {
Expand Down
8 changes: 8 additions & 0 deletions packages/storage/src/implementation/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,17 +280,25 @@ export function addVersionHeader_(headers: Headers): void {
headers['X-Firebase-Storage-Version'] = 'webjs/' + version;
}

export function addGmpidHeader_(headers: Headers, appId: string | null): void {
if (appId) {
headers['X-Firebase-GMPID'] = appId;
}
}

/**
* @template T
*/
export function makeRequest<T>(
requestInfo: RequestInfo<T>,
appId: string | null,
authToken: string | null,
pool: XhrIoPool
): Request<T> {
const queryPart = UrlUtils.makeQueryString(requestInfo.urlParams);
const url = requestInfo.url + queryPart;
const headers = Object.assign({}, requestInfo.headers);
addGmpidHeader_(headers, appId);
addAuthHeader_(headers, authToken);
addVersionHeader_(headers);
return new NetworkRequest<T>(
Expand Down
1 change: 1 addition & 0 deletions packages/storage/src/implementation/requestmaker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { XhrIoPool } from './xhriopool';

type requestMaker = <T>(
requestInfo: RequestInfo<T>,
appId: string | null,
authToken: string | null,
pool: XhrIoPool
) => Request<T>;
Expand Down
49 changes: 44 additions & 5 deletions packages/storage/test/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe('Firebase Storage > Request', () => {
requestInfo.headers[requestHeader] = requestValue;
requestInfo.successCodes = [200, 234];

return makeRequest(requestInfo, null, makePool(spiedSend))
return makeRequest(requestInfo, null, null, makePool(spiedSend))
.getPromise()
.then(
result => {
Expand Down Expand Up @@ -100,7 +100,7 @@ describe('Firebase Storage > Request', () => {
requestInfo.urlParams[p1] = v1;
requestInfo.urlParams[p2] = v2;
requestInfo.body = 'thisistherequestbody';
return makeRequest(requestInfo, null, makePool(spiedSend))
return makeRequest(requestInfo, null, null, makePool(spiedSend))
.getPromise()
.then(
() => {
Expand Down Expand Up @@ -143,7 +143,7 @@ describe('Firebase Storage > Request', () => {
timeout
);

return makeRequest(requestInfo, null, makePool(newSend))
return makeRequest(requestInfo, null, null, makePool(newSend))
.getPromise()
.then(
() => {
Expand All @@ -165,7 +165,7 @@ describe('Firebase Storage > Request', () => {
handler,
timeout
);
const request = makeRequest(requestInfo, null, makePool(null));
const request = makeRequest(requestInfo, null, null, makePool(null));
const promise = request.getPromise().then(
() => {
assert.fail('Succeeded when handler gave error');
Expand All @@ -192,7 +192,12 @@ describe('Firebase Storage > Request', () => {
handler,
timeout
);
const request = makeRequest(requestInfo, authToken, makePool(spiedSend));
const request = makeRequest(
requestInfo,
/* appId= */ null,
authToken,
makePool(spiedSend)
);
return request.getPromise().then(
() => {
assert.isTrue(spiedSend.calledOnce);
Expand All @@ -208,4 +213,38 @@ describe('Firebase Storage > Request', () => {
}
);
});

it('Sends APP ID along properly', () => {
const appId = 'myFirebaseApp';

function newSend(xhrio: TestingXhrIo): void {
xhrio.simulateResponse(200, '', {});
}
const spiedSend = sinon.spy(newSend);

function handler(): boolean {
return true;
}
const requestInfo = new RequestInfo(
'http://my-url.com/',
'GET',
handler,
timeout
);
const request = makeRequest(requestInfo, appId, null, makePool(spiedSend));
return request.getPromise().then(
() => {
assert.isTrue(spiedSend.calledOnce);
const args: unknown[] = spiedSend.getCall(0).args;
const expectedHeaders: { [key: string]: string } = {
'X-Firebase-GMPID': appId
};
expectedHeaders[versionHeaderName] = versionHeaderValue;
assert.deepEqual(args[4], expectedHeaders);
},
() => {
assert.fail('Request failed unexpectedly');
}
);
});
});