-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.spec.ts
More file actions
135 lines (112 loc) · 4.79 KB
/
worker.spec.ts
File metadata and controls
135 lines (112 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { exports } from 'cloudflare:workers';
function postJson(path: string, body: unknown): Request {
return new Request(`http://localhost${path}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
});
}
describe('static flag worker', () => {
describe('service endpoints', () => {
it('returns service info at root', async () => {
const response = await exports.default.fetch('http://localhost/');
expect(response.status).toBe(200);
const body = await response.json();
expect(body.status).toBe('ok');
expect(body.service).toBe('flagd-ofrep-js-worker');
expect(body.flagSource).toBe('static');
});
it('returns health check', async () => {
const response = await exports.default.fetch('http://localhost/health');
expect(response.status).toBe(200);
const body = await response.json();
expect(body.status).toBe('ok');
});
it('returns 404 for unknown paths', async () => {
const response = await exports.default.fetch('http://localhost/unknown');
expect(response.status).toBe(404);
});
});
describe('single flag evaluation', () => {
it('evaluates a boolean flag', async () => {
const response = await exports.default.fetch(postJson('/ofrep/v1/evaluate/flags/simple-boolean', {}));
expect(response.status).toBe(200);
const body = await response.json();
expect(body.key).toBe('simple-boolean');
expect(body.value).toBe(false);
expect(body.variant).toBe('off');
expect(body.reason).toBeDefined();
});
it('evaluates a string flag', async () => {
const response = await exports.default.fetch(postJson('/ofrep/v1/evaluate/flags/simple-string', {}));
expect(response.status).toBe(200);
const body = await response.json();
expect(body.key).toBe('simple-string');
expect(body.value).toBe('default-value');
});
it('evaluates with targeting context', async () => {
const response = await exports.default.fetch(
postJson('/ofrep/v1/evaluate/flags/targeted-boolean', {
context: { email: 'user@openfeature.dev' },
}),
);
expect(response.status).toBe(200);
const body = await response.json();
expect(body.value).toBe(true);
expect(body.reason).toBe('TARGETING_MATCH');
});
it('returns 404 for non-existent flag', async () => {
const response = await exports.default.fetch(postJson('/ofrep/v1/evaluate/flags/does-not-exist', {}));
expect(response.status).toBe(404);
const body = await response.json();
expect(body.errorCode).toBe('FLAG_NOT_FOUND');
});
it('defers disabled flags to code defaults', async () => {
const response = await exports.default.fetch(postJson('/ofrep/v1/evaluate/flags/disabled-flag', {}));
expect(response.status).toBe(200);
const body = await response.json();
expect(body.key).toBe('disabled-flag');
expect(body.reason).toBe('DISABLED');
});
});
describe('bulk evaluation', () => {
it('evaluates all flags', async () => {
const response = await exports.default.fetch(postJson('/ofrep/v1/evaluate/flags', {}));
expect(response.status).toBe(200);
const body = await response.json();
expect(body.flags).toBeDefined();
expect(Array.isArray(body.flags)).toBe(true);
expect(body.flags.length).toBeGreaterThan(0);
});
it('includes metadata in bulk response', async () => {
const response = await exports.default.fetch(postJson('/ofrep/v1/evaluate/flags', {}));
const body = await response.json();
expect(body.metadata).toBeDefined();
expect(body.metadata.flagSetId).toBe('js-worker-example');
});
it('passes context to bulk evaluation', async () => {
const response = await exports.default.fetch(
postJson('/ofrep/v1/evaluate/flags', {
context: { email: 'user@openfeature.dev' },
}),
);
const body = await response.json();
const targeted = body.flags.find((f: { key: string }) => f.key === 'targeted-boolean');
expect(targeted).toBeDefined();
expect(targeted.value).toBe(true);
});
});
describe('CORS', () => {
it('handles OPTIONS preflight on OFREP paths', async () => {
const response = await exports.default.fetch(
new Request('http://localhost/ofrep/v1/evaluate/flags/simple-boolean', { method: 'OPTIONS' }),
);
expect(response.status).toBe(204);
expect(response.headers.get('Access-Control-Allow-Origin')).toBe('*');
});
it('includes CORS headers on evaluation responses', async () => {
const response = await exports.default.fetch(postJson('/ofrep/v1/evaluate/flags/simple-boolean', {}));
expect(response.headers.get('Access-Control-Allow-Origin')).toBe('*');
});
});
});