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: Selectively add headers to fetch call #1391

Merged
merged 6 commits into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
14 changes: 10 additions & 4 deletions src/app/services/actions/query-action-creator-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { IAction } from '../../../types/action';
import { ContentType } from '../../../types/enums';
import { IQuery } from '../../../types/query-runner';
import { IRequestOptions } from '../../../types/request';
import { IStatus } from '../../../types/status';
import { encodeHashCharacters } from '../../utils/query-url-sanitization';
import { authProvider, GraphClient } from '../graph-client';
import { DEFAULT_USER_SCOPES } from '../graph-constants';
Expand All @@ -29,13 +30,13 @@ export async function anonymousRequest(
query: IQuery,
getState: Function
) {
const { proxyUrl, queryRunnerStatus } = getState();
const { graphUrl, options } = createAnonymousRequest(query, proxyUrl, queryRunnerStatus);
dispatch(queryRunningStatus(true));
const { proxyUrl } = getState();
const { graphUrl, options } = createAnonymousRequest(query, proxyUrl);
return fetch(graphUrl, options);
}

export function createAnonymousRequest(query: IQuery, proxyUrl: string) {
export function createAnonymousRequest(query: IQuery, proxyUrl: string, queryRunnerStatus: IStatus) {
const escapedUrl = encodeURIComponent(query.sampleUrl);
const graphUrl = `${proxyUrl}?url=${escapedUrl}`;
const sampleHeaders: any = {};
Expand All @@ -47,13 +48,18 @@ export function createAnonymousRequest(query: IQuery, proxyUrl: string) {
}

const authToken = '{token:https://graph.microsoft.com/}';
const headers = {
let headers = {
Authorization: `Bearer ${authToken}`,
'Content-Type': 'application/json',
SdkVersion: 'GraphExplorer/4.0',
...sampleHeaders
};

if (queryRunnerStatus && !queryRunnerStatus.ok) {
const updatedHeaders = { ...headers, 'cache-control': 'no-cache', pragma: 'no-cache' }
headers = updatedHeaders;
}

const options: IRequestOptions = {
method: query.selectedVerb,
headers
Expand Down
12 changes: 11 additions & 1 deletion src/tests/utils/proxy-url.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fetch from 'isomorphic-fetch';
import { isValidHttpsUrl } from '../../app/utils/external-link-validation';
import { createAnonymousRequest } from '../../app/services/actions/query-action-creator-util';
import { IQuery } from '../../types/query-runner';
import { IStatus } from '../../types/status';

describe('Sandbox api fetch should', () => {

Expand All @@ -24,7 +25,16 @@ describe('Sandbox api fetch should', () => {
sampleBody: ''
}

const { graphUrl, options } = createAnonymousRequest(query, proxyUrl);
const queryRunnerStatus: IStatus = {
messageType: 0,
ok: false,
status: 400,
statusText: '',
duration: 100,
hint: ''
}

const { graphUrl, options } = createAnonymousRequest(query, proxyUrl, queryRunnerStatus);
const response = await fetch(graphUrl, options);
expect(response.ok).toBe(true);
});
Expand Down