Skip to content

Commit

Permalink
Set environment headers enabled to true.
Browse files Browse the repository at this point in the history
  • Loading branch information
imolorhe committed Jun 18, 2020
1 parent 0f10760 commit b437d38
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ out/
src/app/utils/settings.schema.json
src/app/utils/validate_settings_schema.js
extension-builds/
coverage

dist
build
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,220 @@
import { TestBed } from '@angular/core/testing';

import { empty as observableEmpty } from 'rxjs';
import * as fromRoot from '../../reducers';

import { EnvironmentService } from './environment.service';
import { Store } from '@ngrx/store';
import { Mock } from 'ts-mocks';
import { Subscription } from 'rxjs';

let mockStore: Mock<Store<fromRoot.State>>;

const createEnvironmentState = ({
base = {},
subEnvironments = [],
activeIndex = undefined
}: { base?: any, subEnvironments?: any[], activeIndex?: number } = {}) => {
return {
base: {
variablesJson: JSON.stringify(base)
},
subEnvironments: subEnvironments.map((env, i) => ({
id: `${i}`,
variablesJson: JSON.stringify(env),
})),
activeSubEnvironment: `${activeIndex}`
}
};

const createStoreSubscribeFn = (environments: any) => {
return (fn: any) => {
fn({ environments });
return new Subscription();
}
};

describe('EnvironmentService', () => {
beforeEach(() => TestBed.configureTestingModule({
providers: [
EnvironmentService,
{ provide: Store, useValue: {
subscribe: () => {},
select: () => [],
map: () => observableEmpty(),
dispatch: () => {}
} }
]
}));
beforeEach(() => {
const environments = createEnvironmentState({
base: {
baseUrl: 'https://example.api'
}
})
mockStore = new Mock<Store<fromRoot.State>>({
subscribe: createStoreSubscribeFn(environments),
});
TestBed.configureTestingModule({
providers: [
EnvironmentService,
{
provide: Store,
useFactory: () => mockStore.Object,
},
]
})
});

it('should be created', () => {
const service: EnvironmentService = TestBed.get(EnvironmentService);
expect(service).toBeTruthy();
});

describe('.getActiveEnvironment()', () => {
it('should return base environment', () => {
const service: EnvironmentService = TestBed.get(EnvironmentService);
const activeEnvironment = service.getActiveEnvironment();
expect(activeEnvironment).toEqual({
baseUrl: 'https://example.api'
});
});

it('should merge base and active sub environment', () => {
const environments = createEnvironmentState({
base: {
baseUrl: 'https://example.api',
var1: 'base value'
},
subEnvironments: [
{
baseUrl: 'https://environment-1.api',
var2: 'environment1 value',
},
{
baseUrl: 'https://environment-2.api',
var3: 'environment2 value',
}
],
activeIndex: 0
});
mockStore.extend({ subscribe: createStoreSubscribeFn(environments) })
const service: EnvironmentService = TestBed.get(EnvironmentService);
const activeEnvironment = service.getActiveEnvironment();
expect(activeEnvironment).toEqual({
baseUrl: 'https://environment-1.api',
var1: 'base value',
var2: 'environment1 value',
});
});
});

describe('.hydrate()', () => {
it('should returns empty if content is empty string', () => {
const service: EnvironmentService = TestBed.get(EnvironmentService);
const hydratedContent = service.hydrate('');
expect(hydratedContent).toBe('');
});
it('should hydrate content with active environment', () => {
const service: EnvironmentService = TestBed.get(EnvironmentService);
const hydratedContent = service.hydrate('current URL is {{baseUrl}}!');
expect(hydratedContent).toBe('current URL is https://example.api!');
});

it('should hydrate content with provided environment', () => {
const service: EnvironmentService = TestBed.get(EnvironmentService);
const hydratedContent = service.hydrate('current URL is {{baseUrl}}!', {
activeEnvironment: { baseUrl: 'https://provided.url' }
});
expect(hydratedContent).toBe('current URL is https://provided.url!');
});
});

describe('.hydrateHeaders()', () => {
it('should hydrate headers with active environment', () => {
const service: EnvironmentService = TestBed.get(EnvironmentService);
const hydratedContent = service.hydrateHeaders([
{
key: 'x-api-url',
value: '{{baseUrl}}',
enabled: true
}
]);
expect(hydratedContent).toEqual([
{
key: 'x-api-url',
value: 'https://example.api',
enabled: true
}
]);
});

it('should hydrate headers with provided environment', () => {
const service: EnvironmentService = TestBed.get(EnvironmentService);
const hydratedContent = service.hydrateHeaders([
{
key: 'x-api-url',
value: '{{baseUrl}}',
enabled: true
}
], {
activeEnvironment: { baseUrl: 'https://provided.url' }
});
expect(hydratedContent).toEqual([
{
key: 'x-api-url',
value: 'https://provided.url',
enabled: true
}
]);
});

it('should merge headers with headers payload in active environment', () => {
const service: EnvironmentService = TestBed.get(EnvironmentService);
const hydratedContent = service.hydrateHeaders([
{
key: 'x-api-url',
value: '{{baseUrl}}',
enabled: true
}
], {
activeEnvironment: {
baseUrl: 'https://provided.url',
headers: {
'x-global-token': 'global'
}
}
});
expect(hydratedContent).toEqual([
{
key: 'x-global-token',
value: 'global',
enabled: true
},
{
key: 'x-api-url',
value: 'https://provided.url',
enabled: true
},
]);
});

it('should merge headers and hydrate headers payload in active environment', () => {
const service: EnvironmentService = TestBed.get(EnvironmentService);
const hydratedContent = service.hydrateHeaders([
{
key: 'x-api-url',
value: '{{baseUrl}}',
enabled: true
}
], {
activeEnvironment: {
baseUrl: 'https://provided.url',
globalValue: 'global value',
headers: {
'x-global-token': '{{globalValue}}'
}
}
});
expect(hydratedContent).toEqual([
{
key: 'x-global-token',
value: 'global value',
enabled: true
},
{
key: 'x-api-url',
value: 'https://provided.url',
enabled: true
},
]);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as fromHeaders from '../../reducers/headers/headers';
import { IDictionary } from 'app/interfaces/shared';

interface HydrateEnvironmentOptions {
activeEnvironment?: IDictionary<string>;
activeEnvironment?: IDictionary<any>;
}

@Injectable({
Expand Down Expand Up @@ -86,8 +86,9 @@ export class EnvironmentService {
if (environmentHeadersMap) {
const environmentHeaders = Object.keys(environmentHeadersMap).map(key => {
return {
key,
value: environmentHeadersMap[key],
key: this.hydrate(key, options),
value: this.hydrate(environmentHeadersMap[key], options),
enabled: true,
}
});

Expand Down
4 changes: 2 additions & 2 deletions packages/altair-app/src/app/services/gql/gql.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('GqlService', () => {
expect(service).toBeTruthy();
}));

describe('.getSelectedOperationData', () => {
describe('.getSelectedOperationData()', () => {
it(
'should return selectedOperation as null for single queries, without requesting user selection',
inject([GqlService], (service: GqlService) => {
Expand Down Expand Up @@ -104,7 +104,7 @@ describe('GqlService', () => {
});
}));
});
describe('.sendRequest', () => {
describe('.sendRequest()', () => {
it(
'should call HttpClient with expected parameters',
inject([GqlService], (service: GqlService) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/altair-electron/src/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ const createWindow = () => {
requestHeaders = {};

headers.forEach(header => {
if (headersToSet.includes(header.key) && header.key && header.value) {
if (headersToSet.includes(header.key) && header.key && header.value && header.enabled) {
requestHeaders[header.key] = header.value;
}
});
Expand Down

0 comments on commit b437d38

Please sign in to comment.