Skip to content

Commit

Permalink
Add type check to key_base '<=>' operator
Browse files Browse the repository at this point in the history
  • Loading branch information
magneland committed Oct 13, 2023
1 parent b273a16 commit a945343
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/jwt/jwk/key_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def ==(other)
alias eql? ==

def <=>(other)
return nil unless other.is_a?(::JWT::JWK::KeyBase)

self[:kid] <=> other[:kid]
end

Expand Down
32 changes: 32 additions & 0 deletions spec/jwk/hmac_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,36 @@
expect(jwk == other).to eq false
end
end

describe '#<=>' do
it 'is equal to itself' do
other = jwk
expect(jwk <=> other).to eq 0
end

it 'is equal to a clone of itself' do
other = jwk.clone
expect(jwk <=> other).to eq 0
end

it 'is not comparable to nil' do
other = nil
expect(jwk <=> other).to eq nil
end

it 'is not comparable to boolean true' do
other = true
expect(jwk <=> other).to eq nil
end

it 'is not comparable to a non-key' do
other = Object.new
expect(jwk <=> other).to eq nil
end

it 'is not equal to a different key' do
other = described_class.new('other-key')
expect(jwk <=> other).not_to eq 0
end
end
end

0 comments on commit a945343

Please sign in to comment.