Commit bb3af8d24bf700d996471d7e851fefb1020ace72

Authored by phoet
1 parent 5b949712
Exists in master and in 1 other branch production

add a to_curl to notice and show on summary

app/models/notice.rb
... ... @@ -78,6 +78,16 @@ class Notice
78 78 "N/A"
79 79 end
80 80  
  81 + def to_curl
  82 + headers = %w(Accept Accept-Encoding Accept-Language Cookie Referer User-Agent).each_with_object([]) do |name, h|
  83 + if value = env_vars["HTTP_#{name.underscore.upcase}"]
  84 + h << "-H '#{name}: #{value}'"
  85 + end
  86 + end
  87 +
  88 + "curl -X #{env_vars['REQUEST_METHOD'] || 'GET'} #{headers.join(' ')} #{request['url']}"
  89 + end
  90 +
81 91 def env_vars
82 92 request['cgi-data'] || {}
83 93 end
... ...
app/views/notices/_summary.html.haml
... ... @@ -10,6 +10,9 @@
10 10 %tr
11 11 %th URL
12 12 %td.nowrap= link_to notice.request['url'], notice.request['url']
  13 + %tr
  14 + %th &nbsp;
  15 + %td= notice.to_curl
13 16 %tr
14 17 %th Where
15 18 %td= notice.where
... ...
spec/models/notice_spec.rb
... ... @@ -35,6 +35,17 @@ describe Notice do
35 35 end
36 36 end
37 37  
  38 + context "to_curl" do
  39 + let(:request) { {'url' => "http://example.com/resource/12", 'cgi-data' => {'HTTP_USER_AGENT' => 'Mozilla/5.0'}} }
  40 + let(:notice) { Fabricate.build(:notice, request: request) }
  41 +
  42 + it 'has a curl representation' do
  43 + cmd = notice.to_curl
  44 +
  45 + cmd.should eql(%q[curl -X GET -H 'User-Agent: Mozilla/5.0' http://example.com/resource/12])
  46 + end
  47 + end
  48 +
38 49 describe "user agent" do
39 50 it "should be parsed and human-readable" do
40 51 notice = Fabricate.build(:notice, :request => {'cgi-data' => {
... ...