From 4abfe7da92f744fe373571ad8590254f4ea023de Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Thu, 15 Nov 2018 14:56:29 +1100 Subject: [PATCH] fix: correct url encoding for expanded HAL links Fixes: https://github.com/pact-foundation/pact_broker-client/issues/44 --- lib/pact/hal/link.rb | 8 +++----- spec/lib/pact/hal/link_spec.rb | 4 ++++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/pact/hal/link.rb b/lib/pact/hal/link.rb index 1fa272c2..24e8cafb 100644 --- a/lib/pact/hal/link.rb +++ b/lib/pact/hal/link.rb @@ -1,4 +1,4 @@ -require 'uri' +require 'erb' require 'delegate' module Pact @@ -54,11 +54,9 @@ def wrap_response(href, http_response) end def expand_url(params, url) - new_url = url - params.each do | key, value | - new_url = new_url.gsub('{' + key.to_s + '}', URI.escape(value)) + params.inject(url) do | url, (key, value) | + url.gsub('{' + key.to_s + '}', ERB::Util.url_encode(value)) end - new_url end end end diff --git a/spec/lib/pact/hal/link_spec.rb b/spec/lib/pact/hal/link_spec.rb index c81c9715..3b560904 100644 --- a/spec/lib/pact/hal/link_spec.rb +++ b/spec/lib/pact/hal/link_spec.rb @@ -102,6 +102,10 @@ module Hal it "returns a duplicate Link with the expanded href with URL escaping" do expect(subject.expand(bar: 'wiffle meep').href).to eq "http://foo/wiffle%20meep" end + + it "returns a duplicate Link with the expanded href with URL escaping for forward slashes" do + expect(subject.expand(bar: 'wiffle/meep').href).to eq "http://foo/wiffle%2Fmeep" + end end end end