Skip to content

Commit

Permalink
fix missing auth headers
Browse files Browse the repository at this point in the history
  • Loading branch information
imolorhe committed Apr 23, 2024
1 parent 0394abc commit b8f0cc1
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { QueryCollectionService } from '../query-collection/query-collection.ser

import { QueryService } from './query.service';
import { GqlService } from '../gql/gql.service';
import { PerWindowState } from 'altair-graphql-core/build/types/state/per-window.interfaces';

describe('QueryService', () => {
let service: QueryService;
Expand All @@ -17,7 +18,14 @@ describe('QueryService', () => {
TestBed.configureTestingModule({
providers: [
MockProvider(NotifyService),
MockProvider(EnvironmentService),
MockProvider(EnvironmentService, {
hydrate(content) {
return `HYDRATED[[${content}]]`;
},
hydrateHeaders(headers) {
return headers;
},
}),
MockProvider(PreRequestService),
MockProvider(QueryCollectionService),
MockProvider(GqlService),
Expand All @@ -34,4 +42,136 @@ describe('QueryService', () => {
it('should be created', () => {
expect(service).toBeTruthy();
});

describe('hydrateAllHydratables', () => {
it('should hydrate all hydratables', () => {
const hydratedContent = service.hydrateAllHydratables({
query: {
url: 'http://localhost:3000/graphql',
query: 'query { hello }',
variables: '{ "name": "world" }',
subscriptionConnectionParams: '{ "name": "world" }',
},
variables: {
variables: '{ "name": "world" }',
},
headers: [
{
key: 'Content-Type',
value: 'application/json',
enabled: true,
},
],
} as unknown as PerWindowState);
expect(hydratedContent).toEqual({
headers: [
{
key: 'Content-Type',
value: 'application/json',
enabled: true,
},
],
query: 'HYDRATED[[query { hello }]]',
subscriptionConnectionParams: 'HYDRATED[[{ "name": "world" }]]',
subscriptionUrl: 'HYDRATED[[undefined]]',
url: 'HYDRATED[[http://localhost:3000/graphql]]',
variables: 'HYDRATED[[{ "name": "world" }]]',
});
});
it('should hydrate with environment', () => {
const hydratedContent = service.hydrateAllHydratables(
{
query: {
url: 'http://localhost:3000/graphql',
query: 'query { hello }',
variables: '{ "name": "world" }',
subscriptionConnectionParams: '{ "name": "world" }',
},
variables: {
variables: '{ "name": "world" }',
},
headers: [
{
key: 'Content-Type',
value: 'application/json',
enabled: true,
},
],
} as unknown as PerWindowState,
{
additionalHeaders: [],
requestScriptLogs: [],
environment: {
x: 1,
},
}
);
expect(hydratedContent).toEqual({
headers: [
{
key: 'Content-Type',
value: 'application/json',
enabled: true,
},
],
query: 'HYDRATED[[query { hello }]]',
subscriptionConnectionParams: 'HYDRATED[[{ "name": "world" }]]',
subscriptionUrl: 'HYDRATED[[undefined]]',
url: 'HYDRATED[[http://localhost:3000/graphql]]',
variables: 'HYDRATED[[{ "name": "world" }]]',
});
});
it('should hydrate with additional headers', () => {
const hydratedContent = service.hydrateAllHydratables(
{
query: {
url: 'http://localhost:3000/graphql',
query: 'query { hello }',
variables: '{ "name": "world" }',
subscriptionConnectionParams: '{ "name": "world" }',
},
variables: {
variables: '{ "name": "world" }',
},
headers: [
{
key: 'Content-Type',
value: 'application/json',
enabled: true,
},
],
} as unknown as PerWindowState,
{
additionalHeaders: [
{
key: 'Authorization',
value: 'Bearer token',
enabled: true,
},
],
requestScriptLogs: [],
environment: {},
}
);
expect(hydratedContent).toEqual({
headers: [
{
key: 'Content-Type',
value: 'application/json',
enabled: true,
},
{
key: 'Authorization',
value: 'Bearer token',
enabled: true,
},
],
query: 'HYDRATED[[query { hello }]]',
subscriptionConnectionParams: 'HYDRATED[[{ "name": "world" }]]',
subscriptionUrl: 'HYDRATED[[undefined]]',
url: 'HYDRATED[[http://localhost:3000/graphql]]',
variables: 'HYDRATED[[{ "name": "world" }]]',
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,11 @@ export class QueryService {
let subscriptionConnectionParams = this.environmentService.hydrate(
window.query.subscriptionConnectionParams
);
let headers = this.environmentService.hydrateHeaders(window.headers);
const combinedHeaders = [
...window.headers,
...(transformResult?.additionalHeaders ?? []),
];
let headers = this.environmentService.hydrateHeaders(combinedHeaders);

if (transformResult?.environment) {
const activeEnvironment = transformResult.environment;
Expand Down Expand Up @@ -308,12 +312,9 @@ export class QueryService {
activeEnvironment,
}
);
headers = this.environmentService.hydrateHeaders(
[...window.headers, ...transformResult.additionalHeaders],
{
activeEnvironment,
}
);
headers = this.environmentService.hydrateHeaders(combinedHeaders, {
activeEnvironment,
});
}

return {
Expand Down

0 comments on commit b8f0cc1

Please sign in to comment.