Skip to content

Commit

Permalink
handleHeaderQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
volodymyr-basiuk committed Dec 16, 2024
1 parent 5266277 commit b662f47
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/iden3comm/handlers/discovery-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface DiscoveryProtocolOptions {
packageManager: IPackageManager;
protocols?: Array<ProtocolMessage>;
goalCodes?: Array<string>;
headers?: Array<string>;
}

/**
Expand Down Expand Up @@ -142,6 +143,20 @@ export class DiscoveryProtocolHandler
*/
constructor(private readonly _options: DiscoveryProtocolOptions) {
super();
const headers = [
'id',
'typ',
'type',
'thid',
'body',
'from',
'to',
'created_time',
'expires_time'
];
if (!_options.headers) {
_options.headers = headers;
}
}

/**
Expand Down Expand Up @@ -198,6 +213,9 @@ export class DiscoveryProtocolHandler
case DiscoveryProtocolFeatureType.GoalCode:
result = this.handleGoalCodeQuery();
break;
case DiscoveryProtocolFeatureType.Header:
result = this.handleHeaderQuery();
break;
}

return this.handleMatch(result, query.match);
Expand Down Expand Up @@ -229,6 +247,15 @@ export class DiscoveryProtocolHandler
);
}

private handleHeaderQuery(): DiscoverFeatureDisclosure[] {
return (
this._options.headers?.map((header) => ({
[DiscoverFeatureQueryType.FeatureType]: DiscoveryProtocolFeatureType.Header,
id: header
})) ?? []
);
}

private handleMatch(
disclosures: DiscoverFeatureDisclosure[],
match?: string
Expand Down
23 changes: 23 additions & 0 deletions tests/handlers/discover-protocol.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,29 @@ describe('discovery-protocol', () => {
expect(disclosureIds).to.include('unit.testing.some.goal-code');
});

it('feature-type: header', async () => {
const packageManager: IPackageManager = new PackageManager();
const discoveryProtocolHandler = new DiscoveryProtocolHandler({
packageManager
});

const protocolQueryMessage = createDiscoveryFeatureQueryMessage([
{
[DiscoverFeatureQueryType.FeatureType]: DiscoveryProtocolFeatureType.Header,
match: 'expires_time'
}
]);

const {
body: { disclosures }
} = await discoveryProtocolHandler.handleDiscoveryQuery(protocolQueryMessage);
expect(disclosures.length).to.be.eq(1);
expect(disclosures[0][DiscoverFeatureQueryType.FeatureType]).to.be.eq(
DiscoveryProtocolFeatureType.Header
);
expect(disclosures[0].id).to.be.eq('expires_time');
});

it('feature-type: protocol with protocol version mismatch', async () => {
const packageManager: IPackageManager = new PackageManager();
const discoveryProtocolHandler = new DiscoveryProtocolHandler({
Expand Down

0 comments on commit b662f47

Please sign in to comment.