Skip to content

Commit

Permalink
Squeeze multiple slashes in URLs
Browse files Browse the repository at this point in the history
Ruby 2.5 has removed the normalization of multiple slashes in URLs, so
you can get 301 redirect errors if you run for example
`Model.get('/batch/id')`.

```
2.4 > URI.parse("http://example.org").merge('people//batch///id')
 => #<URI::HTTP http://example.org/people/batch/id> 
                                         ^     ^
```

```
2.5 > URI.parse("http://example.org").merge('people//batch///id')
 => #<URI::HTTP http://example.org/people//batch///id> 
                                         ^^     ^^^
```

In Hawk's use case, it should be safe to assume that paths should be
squeezed, but since this can be considered a breaking change, this
commit also bumps the major version number

Ref: https://bugs.ruby-lang.org/issues/8352
  • Loading branch information
tagliala committed Sep 7, 2024
1 parent 72a9fc2 commit ad8507e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.byebug_history
/.bundle/
/.yardoc
/coverage/
Expand Down
2 changes: 1 addition & 1 deletion lib/hawk/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def request(method, path, options)
private

def build_url(path)
base.merge(path.sub(%r{^/}, '')).to_s
base.merge(path.delete_prefix('/').squeeze('/')).to_s
end

def response_handler(response)
Expand Down
2 changes: 1 addition & 1 deletion lib/hawk/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Hawk
VERSION = '2.0.0'
VERSION = '3.0.0'
end
10 changes: 10 additions & 0 deletions spec/basic_operations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ class Person < Hawk::Model::Base
}
end

describe '.get' do
it 'squeezes multiple slashes' do
stub_request(:GET, 'https://example.org/people/batch/id')
.with(headers: { 'User-Agent' => 'Foobar' })
.to_return(status: 200, body: [2].to_json, headers: {})

expect(Person.get('/batch///id')).to contain_exactly(2)
end
end

describe '.find(id)' do
specify do
stub_request(:GET, 'https://example.org/people/2')
Expand Down

0 comments on commit ad8507e

Please sign in to comment.