Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix case insensitive relation type comparison for has and get #31

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
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 )
})
})
Loading