Skip to content

Commit 7481f1e

Browse files
committed
Only send the body:, form_data:, or json: data in the first request.
1 parent bce67b3 commit 7481f1e

File tree

3 files changed

+140
-1
lines changed

3 files changed

+140
-1
lines changed

lib/ronin/support/web/agent.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1079,8 +1079,15 @@ def http_unlock(url,**kwargs,&block)
10791079
#
10801080
def request(method,url, follow_redirects: @follow_redirects,
10811081
max_redirects: @max_redirects,
1082+
# request body keyword arguments
1083+
body: nil,
1084+
form_data: nil,
1085+
json: nil,
10821086
**kwargs)
1083-
response = http_request(method,url,**kwargs)
1087+
response = http_request(method,url, body: body,
1088+
form_data: form_data,
1089+
json: json,
1090+
**kwargs)
10841091

10851092
if follow_redirects && response.kind_of?(Net::HTTPRedirection)
10861093
redirect_count = 0

spec/agent/mixin_spec.rb

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,72 @@
4343
expect(WebMock).to have_requested(:get,redirect_url)
4444
end
4545

46+
context "and when the body: keyword argument is given" do
47+
let(:body) { 'foo' }
48+
49+
it "must only send the request body with the first request" do
50+
stub_request(:post,uri).with(body: body).to_return(
51+
status: 301,
52+
headers: {
53+
'Location' => redirect_url
54+
}
55+
)
56+
stub_request(:get,redirect_url).to_return(body: 'final response')
57+
58+
response = subject.request(:post,uri, body: body)
59+
expect(response).to be_kind_of(Net::HTTPResponse)
60+
expect(response.body).to eq('final response')
61+
62+
expect(WebMock).to have_requested(:post,uri)
63+
expect(WebMock).to have_requested(:get,redirect_url)
64+
end
65+
end
66+
67+
context "and when the form_data: keyword argument is given" do
68+
let(:form_data) { {'foo' => 'bar'} }
69+
70+
it "must only send the request form data with the first request" do
71+
stub_request(:post,uri).with(body: form_data).to_return(
72+
status: 301,
73+
headers: {
74+
'Location' => redirect_url
75+
}
76+
)
77+
stub_request(:get,redirect_url).to_return(body: 'final response')
78+
79+
response = subject.request(:post,uri, form_data: form_data)
80+
expect(response).to be_kind_of(Net::HTTPResponse)
81+
expect(response.body).to eq('final response')
82+
83+
expect(WebMock).to have_requested(:post,uri)
84+
expect(WebMock).to have_requested(:get,redirect_url)
85+
end
86+
end
87+
88+
context "and when the json: keyword argument is given" do
89+
let(:json) { {'foo' => 42} }
90+
91+
it "must only send the request JSON data with the first request" do
92+
stub_request(:post,uri).with(
93+
body: json,
94+
headers: {'Content-Type' => 'application/json'}
95+
).to_return(
96+
status: 301,
97+
headers: {
98+
'Location' => redirect_url
99+
}
100+
)
101+
stub_request(:get,redirect_url).to_return(body: 'final response')
102+
103+
response = subject.request(:post,uri, json: json)
104+
expect(response).to be_kind_of(Net::HTTPResponse)
105+
expect(response.body).to eq('final response')
106+
107+
expect(WebMock).to have_requested(:post,uri)
108+
expect(WebMock).to have_requested(:get,redirect_url)
109+
end
110+
end
111+
46112
context "but requesting the HTTP redirect URL returns yet to another HTTP redirect" do
47113
let(:redirect_url1) { 'https://example.com/path2' }
48114
let(:redirect_url2) { 'https://example.com/path3' }

spec/agent_spec.rb

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,72 @@
498498
expect(WebMock).to have_requested(:get,redirect_url)
499499
end
500500

501+
context "and when the body: keyword argument is given" do
502+
let(:body) { 'foo' }
503+
504+
it "must only send the request body with the first request" do
505+
stub_request(:post,uri).with(body: body).to_return(
506+
status: 301,
507+
headers: {
508+
'Location' => redirect_url
509+
}
510+
)
511+
stub_request(:get,redirect_url).to_return(body: 'final response')
512+
513+
response = subject.request(:post,uri, body: body)
514+
expect(response).to be_kind_of(Net::HTTPResponse)
515+
expect(response.body).to eq('final response')
516+
517+
expect(WebMock).to have_requested(:post,uri)
518+
expect(WebMock).to have_requested(:get,redirect_url)
519+
end
520+
end
521+
522+
context "and when the form_data: keyword argument is given" do
523+
let(:form_data) { {'foo' => 'bar'} }
524+
525+
it "must only send the request form data with the first request" do
526+
stub_request(:post,uri).with(body: form_data).to_return(
527+
status: 301,
528+
headers: {
529+
'Location' => redirect_url
530+
}
531+
)
532+
stub_request(:get,redirect_url).to_return(body: 'final response')
533+
534+
response = subject.request(:post,uri, form_data: form_data)
535+
expect(response).to be_kind_of(Net::HTTPResponse)
536+
expect(response.body).to eq('final response')
537+
538+
expect(WebMock).to have_requested(:post,uri)
539+
expect(WebMock).to have_requested(:get,redirect_url)
540+
end
541+
end
542+
543+
context "and when the json: keyword argument is given" do
544+
let(:json) { {'foo' => 42} }
545+
546+
it "must only send the request JSON data with the first request" do
547+
stub_request(:post,uri).with(
548+
body: json,
549+
headers: {'Content-Type' => 'application/json'}
550+
).to_return(
551+
status: 301,
552+
headers: {
553+
'Location' => redirect_url
554+
}
555+
)
556+
stub_request(:get,redirect_url).to_return(body: 'final response')
557+
558+
response = subject.request(:post,uri, json: json)
559+
expect(response).to be_kind_of(Net::HTTPResponse)
560+
expect(response.body).to eq('final response')
561+
562+
expect(WebMock).to have_requested(:post,uri)
563+
expect(WebMock).to have_requested(:get,redirect_url)
564+
end
565+
end
566+
501567
context "but requesting the HTTP redirect URL returns yet to another HTTP redirect" do
502568
let(:redirect_url1) { 'https://example.com/path2' }
503569
let(:redirect_url2) { 'https://example.com/path3' }

0 commit comments

Comments
 (0)