Skip to content

Commit 2806c5c

Browse files
committed
added isJWKMetadata test
1 parent 5f62fa6 commit 2806c5c

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/jwk-fetcher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ interface JWKMetadata {
1010
keys: Array<JsonWebKeyWithKid>;
1111
}
1212

13-
const isJWKMetadata = (value: any): value is JWKMetadata =>
13+
export const isJWKMetadata = (value: any): value is JWKMetadata =>
1414
isNonNullObject(value) && !!value.keys && isArray(value.keys);
1515

1616
/**

tests/jwk-fetcher.test.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Miniflare } from 'miniflare';
22
import { describe, it, expect, vi } from 'vitest';
33
import type { Fetcher } from '../src/jwk-fetcher';
4-
import { parseMaxAge, UrlKeyFetcher } from '../src/jwk-fetcher';
4+
import { isJWKMetadata, parseMaxAge, UrlKeyFetcher } from '../src/jwk-fetcher';
55
import { WorkersKVStore } from '../src/key-store';
66

77
class HTTPMockFetcher implements Fetcher {
@@ -158,3 +158,38 @@ describe('parseMaxAge', () => {
158158
expect(maxAge).toStrictEqual(want);
159159
});
160160
});
161+
162+
describe('isJWKMetadata', () => {
163+
it('should return true for valid JWKMetadata', () => {
164+
const valid = JSON.parse(validResponseJSON)
165+
expect(isJWKMetadata(valid)).toBe(true);
166+
});
167+
168+
it('should return false for null', () => {
169+
expect(isJWKMetadata(null)).toBe(false);
170+
});
171+
172+
it('should return false for undefined', () => {
173+
expect(isJWKMetadata(undefined)).toBe(false);
174+
});
175+
176+
it('should return false for non-object', () => {
177+
expect(isJWKMetadata('string')).toBe(false);
178+
expect(isJWKMetadata(123)).toBe(false);
179+
expect(isJWKMetadata(true)).toBe(false);
180+
});
181+
182+
it('should return false for object without keys property', () => {
183+
const invalidJWKMetadata = {
184+
notKeys: [],
185+
};
186+
expect(isJWKMetadata(invalidJWKMetadata)).toBe(false);
187+
});
188+
189+
it('should return false for object with non-array keys property', () => {
190+
const invalidJWKMetadata = {
191+
keys: 'notArray',
192+
};
193+
expect(isJWKMetadata(invalidJWKMetadata)).toBe(false);
194+
});
195+
});

0 commit comments

Comments
 (0)