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

feat: Allow fetch functions to pass headers #2004

Merged
merged 6 commits into from
Feb 5, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fetch functions header feature unit tests
  • Loading branch information
kadirchan committed Feb 4, 2025
commit 3b703b17c7bc345d587a0ed46b417b113b3bdc78
182 changes: 180 additions & 2 deletions src/lib/mina/fetch.unit-test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { PrivateKey, TokenId } from 'o1js';
import { createActionsList } from './fetch.js';
import {
createActionsList,
fetchAccount,
fetchActions,
fetchEvents,
setArchiveDefaultHeaders,
setArchiveGraphqlEndpoint,
setGraphqlEndpoint,
setMinaDefaultHeaders,
} from './fetch.js';
import { mockFetchActionsResponse as fetchResponseWithTxInfo } from './fixtures/fetch-actions-response-with-transaction-info.js';
import { mockFetchActionsResponse as fetchResponseNoTxInfo } from './fixtures/fetch-actions-response-without-transaction-info.js';
import { test, describe } from 'node:test';
import { test, describe, beforeEach, afterEach } from 'node:test';
import { removeJsonQuotes } from './graphql.js';
import { expect } from 'expect';

@@ -183,4 +192,173 @@ describe('Fetch', () => {
});
});
});

const minaEndpoint = 'https://mina.dummy/graphql';
const archiveEndpoint = 'https://archive.dummy/graphql';

describe('Testing fetch headers', () => {
let originalFetch: typeof global.fetch;
let lastFetchOptions: any = null;

beforeEach(() => {
originalFetch = global.fetch;
lastFetchOptions = undefined;
global.fetch = ((
input: RequestInfo | URL,
init?: RequestInit
): Promise<Response> => {
lastFetchOptions = init;
let url: string;
if (typeof input === 'string') {
url = input;
} else if (input instanceof URL) {
url = input.toString();
} else {
url = input.url;
}

if (url.includes('archive.dummy')) {
return Promise.resolve({
ok: true,
json: async () => ({
data: {
events: [],
},
}),
} as Response);
} else {
return Promise.resolve({
ok: true,
json: async () => ({
data: {},
}),
} as Response);
}
}) as typeof fetch;

setGraphqlEndpoint(minaEndpoint);
setArchiveGraphqlEndpoint(archiveEndpoint);
});

afterEach(() => {
global.fetch = originalFetch;
});

test('Mina default headers with per request headers', async () => {
setMinaDefaultHeaders({ Authorization: 'Bearer mina-default-token' });
const perRequestHeaders = { 'X-Custom': 'custom-value' };
await fetchAccount(
{ publicKey: PrivateKey.random().toPublicKey().toBase58() },
minaEndpoint,
{
headers: perRequestHeaders,
}
);

expect(lastFetchOptions.headers).toMatchObject({
'Content-Type': 'application/json',
Authorization: 'Bearer mina-default-token',
'X-Custom': 'custom-value',
});
});

test('Per request headers overrides default headers', async () => {
setMinaDefaultHeaders({ Authorization: 'Bearer mina-default-token' });

const perRequestHeaders = {
Authorization: 'Bearer override-token',
'X-Test': 'value',
};
await fetchAccount(
{ publicKey: PrivateKey.random().toPublicKey().toBase58() },
minaEndpoint,
{
headers: perRequestHeaders,
}
);

expect(lastFetchOptions.headers).toMatchObject({
'Content-Type': 'application/json',
Authorization: 'Bearer override-token',
'X-Test': 'value',
});
});

test('Archive default headers with per request headers', async () => {
setArchiveDefaultHeaders({
Authorization: 'Bearer archive-default-token',
});

const perRequestHeaders = { 'X-Another': 'another-value' };
await fetchEvents(
{ publicKey: PrivateKey.random().toPublicKey().toBase58() },
archiveEndpoint,
{},
perRequestHeaders
);

expect(lastFetchOptions.headers).toMatchObject({
'Content-Type': 'application/json',
Authorization: 'Bearer archive-default-token',
'X-Another': 'another-value',
});
});

test('Only default and base headers are used', async () => {
setMinaDefaultHeaders({
'X-Default': 'default-header',
Authorization: 'Bearer mina-default-token',
});
await fetchAccount(
{ publicKey: PrivateKey.random().toPublicKey().toBase58() },
minaEndpoint,
{}
);

expect(lastFetchOptions.headers).toMatchObject({
'Content-Type': 'application/json',
'X-Default': 'default-header',
Authorization: 'Bearer mina-default-token',
});
});

test('Default and per request headers are used for fetchActions', async () => {
setMinaDefaultHeaders({
'X-Default': 'default-header',
});

const perRequestHeaders = {
Authorization: 'Bearer archive-default-token',
};
await fetchActions(
{
publicKey: PrivateKey.random().toPublicKey().toBase58(),
actionStates: {
fromActionState: '',
endActionState: '',
},
},
minaEndpoint,
perRequestHeaders
);

expect(lastFetchOptions.headers).toMatchObject({
'Content-Type': 'application/json',
'X-Default': 'default-header',
Authorization: 'Bearer archive-default-token',
});
});

test('Only base headers are used', async () => {
await fetchAccount(
{ publicKey: PrivateKey.random().toPublicKey().toBase58() },
minaEndpoint,
{}
);

expect(lastFetchOptions.headers).toMatchObject({
'Content-Type': 'application/json',
});
});
});
});
Loading
Oops, something went wrong.