Skip to content
This repository was archived by the owner on Oct 22, 2020. It is now read-only.

Commit b63ade0

Browse files
committed
Add support for making requests relative to the target URI
1 parent eb8745f commit b63ade0

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

lib/wpxf/net/http_client.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def execute_request(opts)
125125
return Wpxf::Net::HttpResponse.new(res)
126126
end
127127

128-
emit_info "Requesting #{opts[:url]}...", true
128+
emit_info "Requesting #{req.url}...", true
129129
req.run
130130
end
131131

lib/wpxf/net/typhoeus_helper.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,15 @@ def create_typhoeus_request(opts)
3737
opts[:body],
3838
headers
3939
)
40-
Typhoeus::Request.new(opts[:url], options)
40+
Typhoeus::Request.new(normalize_relative_uri(opts[:url]), options)
41+
end
42+
43+
def normalize_relative_uri(uri)
44+
if uri.start_with?('/')
45+
normalize_uri(full_uri, uri)
46+
else
47+
uri
48+
end
4149
end
4250
end
4351
end

spec/net/typhoeus_helper_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
end.new
1010
end
1111

12+
before :each do
13+
allow(subject).to receive(:full_uri).and_return('http://127.0.0.1/path')
14+
end
15+
1216
describe '#advanced_typhoeus_options' do
1317
it 'contains Typhoeus options using values in the datastore' do
1418
subject.set_option_value('basic_auth_creds', 'root:toor')
@@ -71,5 +75,25 @@
7175
expect(subject.create_typhoeus_request(method: :get, url: 'github.com'))
7276
.to be_a Typhoeus::Request
7377
end
78+
79+
it 'creates a relative request if the url begins with a forward slash' do
80+
req = subject.create_typhoeus_request(method: :get, url: '/test.txt')
81+
expect(req.url).to eq 'http://127.0.0.1/path/test.txt'
82+
end
83+
84+
it 'creates an absolute request if the url does not start with a forward slash' do
85+
req = subject.create_typhoeus_request(method: :get, url: 'http://www.github.com/')
86+
expect(req.url).to eq 'http://www.github.com/'
87+
end
88+
end
89+
90+
describe '#normalize_relative_uri' do
91+
it 'joins the uri on to the value of #full_uri if it starts with a forward slash' do
92+
expect(subject.normalize_relative_uri('/test.txt')).to eq 'http://127.0.0.1/path/test.txt'
93+
end
94+
95+
it 'uses the full url specified if it does not start with a forward slash' do
96+
expect(subject.normalize_relative_uri('www.github.com')).to eq 'www.github.com'
97+
end
7498
end
7599
end

0 commit comments

Comments
 (0)