Skip to content

Commit

Permalink
ADD test for includeHttpHeadersWs
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyvdberg committed Mar 16, 2023
1 parent 77338fd commit f0fbc6e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/plugins/replication-graphql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export function replicateGraphQL<RxDocType, CheckpointType>(
const startBefore = graphqlReplicationState.start.bind(graphqlReplicationState);
graphqlReplicationState.start = () => {
if (mustUseSocket) {
const httpHeaders = pull.includeHttpHeadersWs ? mutateableClientState.headers : undefined;
const httpHeaders = pull.includeWsHeaders ? mutateableClientState.headers : undefined;
const wsClient = getGraphQLWebSocket(ensureNotFalsy(url.ws), httpHeaders);

wsClient.on('connected', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/types/plugins/replication-graphql.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ ReplicationPullOptions<RxDocType, CheckpointType>,
streamQueryBuilder?: RxGraphQLReplicationPullStreamQueryBuilder;
dataPath?: string;
responseModifier?: RxGraphQLPullResponseModifier<RxDocType, CheckpointType>;
includeHttpHeadersWs?: boolean;
includeWsHeaders?: boolean;
};

export type RxGraphQLPullResponseModifier<RxDocType, CheckpointType> = (
Expand Down
8 changes: 8 additions & 0 deletions test/helper/graphql-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,14 @@ export async function spawn(
// Set up the WebSocket for handling GraphQL subscriptions
const subServer = useServer(
{
onConnect: (ctx) => {
if (reqHeaderName) { // Only check auth when required header was set
const headers = ctx.connectionParams?.headers as Record<string, string>;
if (headers[reqHeaderName] !== reqHeaderValue) {
return false;
}
}
},
schema,
execute,
subscribe,
Expand Down
35 changes: 35 additions & 0 deletions test/unit/replication-graphql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1486,6 +1486,41 @@ describe('replication-graphql.test.ts', () => {
await server.close();
await c.database.destroy();
});
it('should respect pull.includeWsHeaders', async () => {
const [c, server] = await Promise.all([
humansCollection.createHumanWithTimestamp(0),
SpawnServer.spawn()
]);
server.requireHeader('token', 'Bearer token');
const replicationState = replicateGraphQL({
collection: c,
url: server.url,
pull: {
batchSize,
queryBuilder: pullQueryBuilder,
streamQueryBuilder: pullStreamQueryBuilder,
includeWsHeaders: true,
},
headers: {
token: 'Bearer token'
},
live: true,
deletedField: 'deleted'
});
ensureReplicationHasNoErrors(replicationState);
await replicationState.awaitInSync();

const testDocData = getTestData(1)[0];

// Test mutation with subscription
await server.setDocument(testDocData);
await waitUntil(async () => {
const docs = await c.find().exec();
return docs.length === 1;
});
await server.close();
await c.database.destroy();
});
it('should respect the pull.responseModifier', async () => {
const checkpointIterationModeAmount = 5;
const eventObservationModeAmount = 3;
Expand Down

0 comments on commit f0fbc6e

Please sign in to comment.