Skip to content

Commit 4b2ed98

Browse files
authored
Adjust headers to fix backwards compty with php-node (#40)
1 parent f60dbc8 commit 4b2ed98

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/napi.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,11 @@ impl Headers {
262262
/// ```
263263
#[napi]
264264
pub fn get(&self, key: String) -> Option<String> {
265+
// Return the last value for this key (HTTP headers can have multiple values)
265266
self.0
266-
.get(&key)
267+
.get_all(&key)
268+
.iter()
269+
.last()
267270
.and_then(|v| v.to_str().map(|s| s.to_string()).ok())
268271
}
269272

@@ -431,7 +434,7 @@ impl Headers {
431434
/// ```
432435
#[napi(getter)]
433436
pub fn size(&self) -> u32 {
434-
self.0.len() as u32
437+
self.0.keys_len() as u32
435438
}
436439

437440
/// Get an iterator over the header entries.

test/headers.test.mjs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ test('Headers', async t => {
5050
baz: ['buz', 'bux']
5151
})
5252
strictEqual(headers.get('foo'), 'bar', 'should return the value of an existing header')
53-
deepStrictEqual(headers.get('baz'), 'buz', 'should return first value for a multi-value header')
53+
deepStrictEqual(headers.get('baz'), 'bux', 'should return last value for a multi-value header')
5454
strictEqual(headers.get('not-exists'), null, 'should return null for non-existing header')
5555
})
5656

@@ -108,13 +108,13 @@ test('Headers', async t => {
108108
foo: 'bar',
109109
baz: ['buz', 'bux']
110110
})
111-
strictEqual(headers.size, 3, 'should have correct size with multiple headers')
111+
strictEqual(headers.size, 2, 'should count unique header keys')
112112
headers.set('foo', 'new-value')
113-
strictEqual(headers.size, 3, 'should not change size when replacing a header value')
113+
strictEqual(headers.size, 2, 'should not change size when replacing a header value')
114114
headers.add('new-header', 'value')
115-
strictEqual(headers.size, 4, 'should increase size when adding a new header')
115+
strictEqual(headers.size, 3, 'should increase size when adding a new header')
116116
headers.add('baz', 'new-baz-value')
117-
strictEqual(headers.size, 5, 'should increase size when adding a new value to an existing header')
117+
strictEqual(headers.size, 3, 'should not increase size when adding a new value to an existing header')
118118
headers.clear()
119119
strictEqual(headers.size, 0, 'should be zero after clearing headers')
120120
})

0 commit comments

Comments
 (0)