Skip to content
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

fix: correct STORAGE_EMULATOR_HOST handling (#2069, #1314) #2070

Merged
merged 3 commits into from Oct 18, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/storage.ts
Expand Up @@ -665,8 +665,12 @@ export class Storage extends Service {

options = Object.assign({}, options, {apiEndpoint});

// Note: EMULATOR_HOST is an experimental configuration variable. Use apiEndpoint instead.
const baseUrl = EMULATOR_HOST || `${options.apiEndpoint}/storage/v1`;
// Note: EMULATOR_HOST, if present and not overridden, has been applied to
// `options` at this point. Also, this uses string concatenation because the
// endpoint may contain a base path, and any trailing slash on that will
// have been removed, so using the two-arg URL constructor for relative path
// resolution won't work.
const baseUrl = new URL(options.apiEndpoint + '/storage/v1').href;

const config = {
apiEndpoint: options.apiEndpoint!,
Expand Down
22 changes: 17 additions & 5 deletions test/index.ts
Expand Up @@ -437,27 +437,33 @@ describe('Storage', () => {
delete process.env.STORAGE_EMULATOR_HOST;
});

it('should set baseUrl to env var STORAGE_EMULATOR_HOST', () => {
it('should set baseUrl to env var STORAGE_EMULATOR_HOST plus standard path', () => {
const storage = new Storage({
projectId: PROJECT_ID,
});

const calledWith = storage.calledWith_[0];
assert.strictEqual(calledWith.baseUrl, EMULATOR_HOST);
assert.strictEqual(calledWith.baseUrl, EMULATOR_HOST + '/storage/v1');
assert.strictEqual(
calledWith.apiEndpoint,
'https://internal.benchmark.com/path'
);
});

it('should be overriden by apiEndpoint', () => {
it('should be overridden by apiEndpoint', () => {
const storage = new Storage({
projectId: PROJECT_ID,
apiEndpoint: 'https://some.api.com',
});

const calledWith = storage.calledWith_[0];
assert.strictEqual(calledWith.baseUrl, EMULATOR_HOST);
// NOTE: this used to assert partially the opposite of what the test
// says: it checked that baseUrl was _not_ overridden, but apiEndpoint
// was.
assert.strictEqual(
calledWith.baseUrl,
'https://some.api.com/storage/v1'
);
assert.strictEqual(calledWith.apiEndpoint, 'https://some.api.com');
});

Expand All @@ -470,7 +476,13 @@ describe('Storage', () => {
});

const calledWith = storage.calledWith_[0];
assert.strictEqual(calledWith.baseUrl, EMULATOR_HOST);
// NOTE: this used to assert partially the opposite of what the test
// says: it checked that baseUrl was _not_ overridden, but apiEndpoint
// was.
assert.strictEqual(
calledWith.baseUrl,
'https://internal.benchmark.com/path/storage/v1'
);
assert.strictEqual(
calledWith.apiEndpoint,
'https://internal.benchmark.com/path'
Expand Down