-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhelpers.js
More file actions
160 lines (137 loc) · 3.82 KB
/
helpers.js
File metadata and controls
160 lines (137 loc) · 3.82 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* Test helpers for JavaScript Solid Server
*/
import { createServer } from '../src/server.js';
import fs from 'fs-extra';
import path from 'path';
const TEST_DATA_DIR = './data';
let server = null;
let baseUrl = null;
// Store tokens for pods by name
const podTokens = new Map();
/**
* Start a test server on a random available port
* @param {object} options - Server options
* @param {boolean} options.conneg - Enable content negotiation (default false)
* @returns {Promise<{server: object, baseUrl: string}>}
*/
export async function startTestServer(options = {}) {
// Clean up any existing test data
await fs.emptyDir(TEST_DATA_DIR);
server = createServer({ logger: false, ...options });
// Use port 0 to let OS assign available port
await server.listen({ port: 0, host: '127.0.0.1' });
const address = server.server.address();
baseUrl = `http://127.0.0.1:${address.port}`;
return { server, baseUrl };
}
/**
* Stop the test server
*/
export async function stopTestServer() {
if (server) {
await server.close();
server = null;
}
// Clean up test data
await fs.emptyDir(TEST_DATA_DIR);
// Clear tokens
podTokens.clear();
}
/**
* Get the base URL
*/
export function getBaseUrl() {
return baseUrl;
}
/**
* Create a pod for testing
* @param {string} name - Pod name
* @returns {Promise<{webId: string, podUri: string, token: string}>}
*/
export async function createTestPod(name) {
const res = await fetch(`${baseUrl}/.pods`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name })
});
if (!res.ok) {
throw new Error(`Failed to create pod: ${res.status}`);
}
const result = await res.json();
// Store the token for this pod
if (result.token) {
podTokens.set(name, result.token);
}
return result;
}
/**
* Get token for a pod
* @param {string} name - Pod name
* @returns {string|null}
*/
export function getPodToken(name) {
return podTokens.get(name) || null;
}
/**
* Make a request to the test server
* @param {string} path - URL path
* @param {object} options - fetch options (can include `auth: 'podname'` for authenticated requests)
* @returns {Promise<Response>}
*/
export async function request(urlPath, options = {}) {
const url = urlPath.startsWith('http') ? urlPath : `${baseUrl}${urlPath}`;
// Handle authentication
const { auth, ...fetchOptions } = options;
if (auth) {
const token = podTokens.get(auth);
if (token) {
fetchOptions.headers = {
...fetchOptions.headers,
'Authorization': `Bearer ${token}`
};
}
}
return fetch(url, fetchOptions);
}
/**
* Assert response status
*/
export function assertStatus(res, expected, message = '') {
if (res.status !== expected) {
throw new Error(`Expected status ${expected}, got ${res.status}. ${message}`);
}
}
/**
* Assert response header exists
*/
export function assertHeader(res, header, expected = undefined) {
const value = res.headers.get(header);
if (value === null) {
throw new Error(`Expected header ${header} to exist`);
}
if (expected !== undefined && value !== expected) {
throw new Error(`Expected header ${header} to be "${expected}", got "${value}"`);
}
return value;
}
/**
* Assert response header contains value
*/
export function assertHeaderContains(res, header, substring) {
const value = res.headers.get(header);
if (value === null || !value.includes(substring)) {
throw new Error(`Expected header ${header} to contain "${substring}", got "${value}"`);
}
return value;
}
/**
* Parse JSON-LD from HTML (extracts from script tag)
*/
export function extractJsonLdFromHtml(html) {
const match = html.match(/<script type="application\/ld\+json">([\s\S]*?)<\/script>/);
if (!match) {
throw new Error('No JSON-LD found in HTML');
}
return JSON.parse(match[1]);
}