-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathetag-test.js
More file actions
40 lines (31 loc) · 1.23 KB
/
etag-test.js
File metadata and controls
40 lines (31 loc) · 1.23 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
'use strict';
const assert = require('node:assert/strict');
const fs = require('node:fs');
const etag = require('etag');
// Generate etag from a string
const tag1 = etag('hello world');
assert.equal(typeof tag1, 'string');
assert.ok(tag1.startsWith('"'), 'strong etag should start with quote');
assert.ok(tag1.endsWith('"'), 'strong etag should end with quote');
// Generate etag from a Buffer
const tag2 = etag(Buffer.from('hello world'));
assert.equal(typeof tag2, 'string');
assert.ok(tag2.startsWith('"'));
assert.ok(tag2.endsWith('"'));
// Same content should produce the same etag
assert.equal(tag1, tag2);
// Different content should produce different etags
const tag3 = etag('different content');
assert.notEqual(tag1, tag3);
// Generate a weak etag
const weakTag = etag('hello world', { weak: true });
assert.equal(typeof weakTag, 'string');
assert.ok(weakTag.startsWith('W/"'), 'weak etag should start with W/"');
assert.ok(weakTag.endsWith('"'));
// Generate etag from fs.Stats
const stat = fs.statSync(__filename);
const statTag = etag(stat);
assert.equal(typeof statTag, 'string');
assert.ok(statTag.startsWith('"') || statTag.startsWith('W/"'), 'stat etag should be quoted');
assert.ok(statTag.endsWith('"'));
console.log('etag-test:ok');