From bd07aa2fdcd03df747db06d49766b534876ca471 Mon Sep 17 00:00:00 2001 From: Geremia Taglialatela Date: Sat, 7 Sep 2024 19:55:32 +0200 Subject: [PATCH] Squeeze multiple slashes in URLs 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') => # ^ ^ ``` ``` 2.5 > URI.parse("http://example.org").merge('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 --- .gitignore | 1 + lib/hawk/http.rb | 2 +- lib/hawk/version.rb | 2 +- spec/basic_operations_spec.rb | 10 ++++++++++ 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 09fd1db..77a7e23 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.byebug_history /.bundle/ /.yardoc /coverage/ diff --git a/lib/hawk/http.rb b/lib/hawk/http.rb index ad6dc50..2143a67 100644 --- a/lib/hawk/http.rb +++ b/lib/hawk/http.rb @@ -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) diff --git a/lib/hawk/version.rb b/lib/hawk/version.rb index af6773d..930be6b 100644 --- a/lib/hawk/version.rb +++ b/lib/hawk/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Hawk - VERSION = '2.0.0' + VERSION = '3.0.0' end diff --git a/spec/basic_operations_spec.rb b/spec/basic_operations_spec.rb index f2bad1a..bf88011 100644 --- a/spec/basic_operations_spec.rb +++ b/spec/basic_operations_spec.rb @@ -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')