Skip to content

Commit

Permalink
Fix case insensitive relation type comparison for has and get
Browse files Browse the repository at this point in the history
  • Loading branch information
csarven committed Feb 21, 2024
1 parent dee86ee commit 80ff775
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
6 changes: 4 additions & 2 deletions lib/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,12 @@ class Link {
get( attr, value ) {

attr = attr.toLowerCase()
value = value.toLowerCase()

var links = []

for( var i = 0; i < this.refs.length; i++ ) {
if( this.refs[ i ][ attr ] === value ) {
if( this.refs[ i ][ attr ].toLowerCase() === value ) {
links.push( this.refs[ i ] )
}
}
Expand Down Expand Up @@ -138,9 +139,10 @@ class Link {
has( attr, value ) {

attr = attr.toLowerCase()
value = value.toLowerCase()

for( var i = 0; i < this.refs.length; i++ ) {
if( this.refs[ i ][ attr ] === value ) {
if( this.refs[ i ][ attr ].toLowerCase() === value ) {
return true
}
}
Expand Down
30 changes: 27 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,39 @@ context( 'HTTP Link Header', function() {
assert.strictEqual( link.toString(), expected )
})

test( 'case sensitive relation types', function() {
test( 'case insensitive relation types includes', function() {
var value = '<https://some.url>; rel="http://some.rel/caseSensitive"'
var expected = '<https://some.url>; rel="http://some.rel/caseSensitive"'
var refs = [ { uri: 'https://some.url', rel: 'http://some.rel/caseSensitive' } ]
var link = Link.parse( value )
// Check that comparison is case-insensitive, as specified in RFC8288, Section 2.2.1
assert.deepEqual( link.rel( 'http://some.rel/casesensitive' ), refs )

// Check that comparison is case-insensitive, as specified in RFC8288, Sections 2.1.1, 2.1.2
assert.strictEqual( link.has( 'rel', 'http://some.rel/casesensitive' ), true )
// Verify that re-serialization maintains input casing
assert.strictEqual( link.toString(), expected )
})

test( 'case insensitive relation types by reference', function() {
var value = '<https://some.url>; rel="http://some.rel/caseSensitive"'
var expected = '<https://some.url>; rel="http://some.rel/caseSensitive"'
var refs = [ { uri: 'https://some.url', rel: 'http://some.rel/caseSensitive' } ]
var link = Link.parse( value )

// Check that comparison is case-insensitive, as specified in RFC8288, Sections 2.1.1, 2.1.2
assert.deepEqual( link.get( 'rel', 'http://some.rel/casesensitive' ), refs )
// Verify that re-serialization maintains input casing
assert.strictEqual( link.toString(), expected )
})

test( 'case insensitive relation types by reference shorthand', function() {
var value = '<https://some.url>; rel="http://some.rel/caseSensitive"'
var expected = '<https://some.url>; rel="http://some.rel/caseSensitive"'
var refs = [ { uri: 'https://some.url', rel: 'http://some.rel/caseSensitive' } ]
var link = Link.parse( value )

// Check that comparison is case-insensitive, as specified in RFC8288, Section 2.1.1, 2.1.2
assert.deepEqual( link.rel( 'http://some.rel/casesensitive' ), refs )
// Verify that re-serialization maintains input casing
assert.strictEqual( link.toString(), expected )
})
})

0 comments on commit 80ff775

Please sign in to comment.