Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/napi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,11 @@ impl Headers {
/// ```
#[napi]
pub fn get(&self, key: String) -> Option<String> {
// Return the last value for this key (HTTP headers can have multiple values)
self.0
.get(&key)
.get_all(&key)
.iter()
.last()
.and_then(|v| v.to_str().map(|s| s.to_string()).ok())
}

Expand Down Expand Up @@ -431,7 +434,7 @@ impl Headers {
/// ```
#[napi(getter)]
pub fn size(&self) -> u32 {
self.0.len() as u32
self.0.keys_len() as u32
}

/// Get an iterator over the header entries.
Expand Down
10 changes: 5 additions & 5 deletions test/headers.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ test('Headers', async t => {
baz: ['buz', 'bux']
})
strictEqual(headers.get('foo'), 'bar', 'should return the value of an existing header')
deepStrictEqual(headers.get('baz'), 'buz', 'should return first value for a multi-value header')
deepStrictEqual(headers.get('baz'), 'bux', 'should return last value for a multi-value header')
strictEqual(headers.get('not-exists'), null, 'should return null for non-existing header')
})

Expand Down Expand Up @@ -108,13 +108,13 @@ test('Headers', async t => {
foo: 'bar',
baz: ['buz', 'bux']
})
strictEqual(headers.size, 3, 'should have correct size with multiple headers')
strictEqual(headers.size, 2, 'should count unique header keys')
headers.set('foo', 'new-value')
strictEqual(headers.size, 3, 'should not change size when replacing a header value')
strictEqual(headers.size, 2, 'should not change size when replacing a header value')
headers.add('new-header', 'value')
strictEqual(headers.size, 4, 'should increase size when adding a new header')
strictEqual(headers.size, 3, 'should increase size when adding a new header')
headers.add('baz', 'new-baz-value')
strictEqual(headers.size, 5, 'should increase size when adding a new value to an existing header')
strictEqual(headers.size, 3, 'should not increase size when adding a new value to an existing header')
headers.clear()
strictEqual(headers.size, 0, 'should be zero after clearing headers')
})
Expand Down