feat: introduce support for custom HTTP headers (#16599)
* feat: introduce support for custom http headers * feat: add fetch middleware
This commit is contained in:
@ -88,6 +88,49 @@ describe('Connection', () => {
|
||||
});
|
||||
}
|
||||
|
||||
if (mockServer) {
|
||||
it('should pass HTTP headers to RPC', async () => {
|
||||
const headers = {
|
||||
Authorization: 'Bearer 123',
|
||||
};
|
||||
|
||||
let connection = new Connection(url, {
|
||||
httpHeaders: headers,
|
||||
});
|
||||
|
||||
await mockRpcResponse({
|
||||
method: 'getVersion',
|
||||
params: [],
|
||||
value: {'solana-core': '0.20.4'},
|
||||
withHeaders: headers,
|
||||
});
|
||||
|
||||
expect(await connection.getVersion()).to.be.not.null;
|
||||
});
|
||||
|
||||
it('should allow middleware to augment request', async () => {
|
||||
let connection = new Connection(url, {
|
||||
fetchMiddleware: (url, options, fetch) => {
|
||||
options.headers = Object.assign(options.headers, {
|
||||
Authorization: 'Bearer 123',
|
||||
});
|
||||
fetch(url, options);
|
||||
},
|
||||
});
|
||||
|
||||
await mockRpcResponse({
|
||||
method: 'getVersion',
|
||||
params: [],
|
||||
value: {'solana-core': '0.20.4'},
|
||||
withHeaders: {
|
||||
Authorization: 'Bearer 123',
|
||||
},
|
||||
});
|
||||
|
||||
expect(await connection.getVersion()).to.be.not.null;
|
||||
});
|
||||
}
|
||||
|
||||
it('get account info - not found', async () => {
|
||||
const account = new Account();
|
||||
|
||||
|
@ -5,7 +5,7 @@ import * as mockttp from 'mockttp';
|
||||
|
||||
import {mockRpcMessage} from './rpc-websockets';
|
||||
import {Account, Connection, PublicKey, Transaction} from '../../src';
|
||||
import type {Commitment, RpcParams} from '../../src/connection';
|
||||
import type {Commitment, HttpHeaders, RpcParams} from '../../src/connection';
|
||||
|
||||
export const mockServer: mockttp.Mockttp | undefined =
|
||||
process.env.TEST_LIVE === undefined ? mockttp.getLocal() : undefined;
|
||||
@ -64,12 +64,14 @@ export const mockRpcResponse = async ({
|
||||
value,
|
||||
error,
|
||||
withContext,
|
||||
withHeaders,
|
||||
}: {
|
||||
method: string;
|
||||
params: Array<any>;
|
||||
value?: any;
|
||||
error?: any;
|
||||
withContext?: boolean;
|
||||
withHeaders?: HttpHeaders;
|
||||
}) => {
|
||||
if (!mockServer) return;
|
||||
|
||||
@ -90,6 +92,7 @@ export const mockRpcResponse = async ({
|
||||
method,
|
||||
params,
|
||||
})
|
||||
.withHeaders(withHeaders || {})
|
||||
.thenReply(
|
||||
200,
|
||||
JSON.stringify({
|
||||
|
Reference in New Issue
Block a user