Class: WebUnit::Response
- Includes:
- Utils
- Defined in:
- lib/webunit/response.rb
Constant Summary collapse
- @@host =
nil
- @@port =
nil
- @@http =
nil
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#forms ⇒ Object
readonly
Returns the value of attribute forms.
-
#frames ⇒ Object
readonly
Returns the value of attribute frames.
-
#images ⇒ Object
readonly
Returns the value of attribute images.
-
#links ⇒ Object
readonly
Returns the value of attribute links.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#password ⇒ Object
Returns the value of attribute password.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#tables ⇒ Object
readonly
Returns the value of attribute tables.
-
#tree ⇒ Object
readonly
Returns the value of attribute tree.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
-
#username ⇒ Object
Returns the value of attribute username.
Class Method Summary collapse
-
.get(url, data = nil, up = nil) ⇒ Object
— Response.get(url,data=nil) return a Response.
-
.html(html, url = nil) ⇒ Object
— Response.html( html) return a Response.
-
.post(url, data = nil, up = nil) ⇒ Object
— Response.post(url,data=nil) return a Response.
- .reset ⇒ Object
Instance Method Summary collapse
- #add_form(form) ⇒ Object
- #add_frame(frame) ⇒ Object
- #add_image(image) ⇒ Object
- #add_link(link) ⇒ Object
- #add_table(table) ⇒ Object
-
#click(str, x = nil, y = nil) ⇒ Object
— Response#click( str ) at first Response#readlink, and then, Form#submit.
- #do_connect(host, port) ⇒ Object
- #feed ⇒ Object
-
#find(tag) ⇒ Object
— Response#find( tag ) same as Htmlelem#find.
-
#form ⇒ Object
— Response#form short for Response.forms.
-
#format ⇒ Object
— Response#format.
-
#frame ⇒ Object
— Response#frame short for Response.frames.
- #freeze ⇒ Object
-
#image ⇒ Object
— Response#image.
- #init_html(html, url) ⇒ Object
- #init_http(url, method, data = nil, retried = false, multipart = false) ⇒ Object
- #js_open_objs ⇒ Object
-
#link ⇒ Object
— Response#link short for Response.links.
-
#open ⇒ Object
— Response#open short for Response.opens.
-
#opens ⇒ Object
— Response#opens.
-
#params ⇒ Object
— Response#param short for Response.forms.params.
-
#pbody ⇒ Object
— Response#pbody.
- #push(button = nil) ⇒ Object
-
#readlink(str) ⇒ Object
— Response#readlink( str ) same as Htmlelem#find.
-
#redirect ⇒ Object
— Response#redirect.
-
#search(str) ⇒ Object
— Response#search( str ) same as Htmlelem#search.
-
#submit(button = nil, x = nil, y = nil) ⇒ Object
— Response#submit( button=nil,x=nil,y=nil) short for Response.forms.submit.
-
#table ⇒ Object
— Response#table short for Response.tables.
-
#title ⇒ Object
— Response#title return title.
Methods included from Utils
#complete_url, #orthop_url, #parse_url
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
19 20 21 |
# File 'lib/webunit/response.rb', line 19 def body @body end |
#code ⇒ Object (readonly)
Returns the value of attribute code.
19 20 21 |
# File 'lib/webunit/response.rb', line 19 def code @code end |
#forms ⇒ Object (readonly)
Returns the value of attribute forms.
20 21 22 |
# File 'lib/webunit/response.rb', line 20 def forms @forms end |
#frames ⇒ Object (readonly)
Returns the value of attribute frames.
20 21 22 |
# File 'lib/webunit/response.rb', line 20 def frames @frames end |
#images ⇒ Object (readonly)
Returns the value of attribute images.
20 21 22 |
# File 'lib/webunit/response.rb', line 20 def images @images end |
#links ⇒ Object (readonly)
Returns the value of attribute links.
20 21 22 |
# File 'lib/webunit/response.rb', line 20 def links @links end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
19 20 21 |
# File 'lib/webunit/response.rb', line 19 def method @method end |
#password ⇒ Object
Returns the value of attribute password.
21 22 23 |
# File 'lib/webunit/response.rb', line 21 def password @password end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
19 20 21 |
# File 'lib/webunit/response.rb', line 19 def response @response end |
#tables ⇒ Object (readonly)
Returns the value of attribute tables.
20 21 22 |
# File 'lib/webunit/response.rb', line 20 def tables @tables end |
#tree ⇒ Object (readonly)
Returns the value of attribute tree.
20 21 22 |
# File 'lib/webunit/response.rb', line 20 def tree @tree end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
19 20 21 |
# File 'lib/webunit/response.rb', line 19 def url @url end |
#username ⇒ Object
Returns the value of attribute username.
21 22 23 |
# File 'lib/webunit/response.rb', line 21 def username @username end |
Class Method Details
.get(url, data = nil, up = nil) ⇒ Object
— Response.get(url,data=nil)
return a Response.
27 28 29 30 31 32 |
# File 'lib/webunit/response.rb', line 27 def self::get( url, data=nil, up=nil ) r = self.new r.username = up[0] if up r.password = up[1] if up r.init_http( url, "GET", data ) end |
.html(html, url = nil) ⇒ Object
— Response.html( html)
return a Response.
50 51 52 53 |
# File 'lib/webunit/response.rb', line 50 def self::html( html, url=nil ) html = "<html>#{html}</html>" unless html =~ %r!^<html>! self.new.init_html( html, url ) end |
.post(url, data = nil, up = nil) ⇒ Object
— Response.post(url,data=nil)
return a Response.
38 39 40 41 42 43 |
# File 'lib/webunit/response.rb', line 38 def self::post( url, data=nil, up=nil ) r = self.new r.username = up[0] if up r.password = up[1] if up r.init_http( url, "POST", data ) end |
.reset ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/webunit/response.rb', line 55 def self::reset begin @@http.finish if @@http rescue IOError @@http = nil end @@host = nil @@port = nil @@http = nil end |
Instance Method Details
#add_form(form) ⇒ Object
165 166 167 |
# File 'lib/webunit/response.rb', line 165 def add_form( form ) @forms.push( form ) end |
#add_frame(frame) ⇒ Object
169 170 171 |
# File 'lib/webunit/response.rb', line 169 def add_frame( frame ) @frames.push( frame ) end |
#add_image(image) ⇒ Object
177 178 179 |
# File 'lib/webunit/response.rb', line 177 def add_image( image ) @images.push( image ) end |
#add_link(link) ⇒ Object
161 162 163 |
# File 'lib/webunit/response.rb', line 161 def add_link( link ) @links.push( link ) end |
#add_table(table) ⇒ Object
173 174 175 |
# File 'lib/webunit/response.rb', line 173 def add_table( table ) @tables.push( table ) end |
#click(str, x = nil, y = nil) ⇒ Object
— Response#click( str )
at first Response#readlink, and then, Form#submit.
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/webunit/response.rb', line 228 def click( str, x=nil, y=nil ) begin @tree.readlink( str ) rescue ElemNotFound matched = 0 @tree.find( 'input' ).each do |i| if i.class == InputSubmit matched = i.name if i.name == str || i.value == str elsif i.class == InputImage matched = i.name if i.name == str end end if matched == 0 raise ElemNotFound else submit( matched, x, y ) end end end |
#do_connect(host, port) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/webunit/response.rb', line 74 def do_connect(host, port) if @@host != host or @@port != port @@host = host @@port = port begin @@http.finish if @@http rescue IOError @@http = nil end @@http = Net::HTTP::new( host, port ) end end |
#feed ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/webunit/response.rb', line 144 def feed @forms = [] @tables = [] @links = [] @frames = [] @images = [] @code ||= nil if @code == '302' @links.push Link.new( 'href' => @response['Location'] ) if @response.key? 'Location' end p = Parser.new @tree = p.feed( self ) p.close freeze end |
#find(tag) ⇒ Object
— Response#find( tag )
same as Htmlelem#find.
201 202 203 |
# File 'lib/webunit/response.rb', line 201 def find( tag ) @tree.find( tag ) end |
#form ⇒ Object
— Response#form
short for Response.forms[0]
293 294 295 |
# File 'lib/webunit/response.rb', line 293 def form @forms[0] end |
#format ⇒ Object
— Response#format
379 380 381 382 383 384 385 |
# File 'lib/webunit/response.rb', line 379 def format w = MemWriter.new p = HTMLParser.new( AbstractFormatter.new( w ) ) p.feed( @body ) p.close w.to_s end |
#frame ⇒ Object
— Response#frame
short for Response.frames[0]
311 312 313 |
# File 'lib/webunit/response.rb', line 311 def frame @frames[0] end |
#freeze ⇒ Object
181 182 183 184 185 |
# File 'lib/webunit/response.rb', line 181 def freeze @forms.each do |f| f.freeze end end |
#image ⇒ Object
— Response#image
275 276 277 |
# File 'lib/webunit/response.rb', line 275 def image self.images[0] end |
#init_html(html, url) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/webunit/response.rb', line 66 def init_html( html, url ) @url = url @method = 'html' @body = html feed self end |
#init_http(url, method, data = nil, retried = false, multipart = false) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/webunit/response.rb', line 87 def init_http( url, method, data=nil, retried=false, multipart=false) url = $URLBASE + url unless url =~ %r!://! @url = url @method = method header = {} prot,host,port,path = parse_url( url ) do_connect( host, port) header.update( Cookies::header( path ) ) @username ||= nil; @password ||= nil if @username != nil && @password != nil header['authorization'] = 'Basic ' + ["#{@username}:#{@password}"].pack('m').gsub(/\s+/, '') end begin if multipart header['Content-type'] = "multipart/form-data,boundary=#{BOUNDARY}" #puts data @response = @@http.post2( path, data.gsub("\n", "\r\n"), header) else case @method.upcase when "GET" then path << '?' + data if data puts path if $DEBUG @response = @@http.get2( path, header ) when "POST" then if header['Content-type'] == nil header['Content-type'] = "application/x-www-form-urlencoded" end @response = @@http.post2( path, data, header ) end end rescue Errno::EPIPE @@host = nil raise if retried return self.init_http( url, method, data, true, multipart) end @body = @response.body @code = @response.code p @response['set-cookie'] if $DEBUG Cookies::update( @response['set-cookie'] ) case @response['content-type'] when %r!text/html! feed when %r!text/xml! return DomWalker::new( @response.body ) end raise HttpNotFound if @code == "404" self end |
#js_open_objs ⇒ Object
351 352 353 354 |
# File 'lib/webunit/response.rb', line 351 def js_open_objs $stderr.puts "no longer support Response#js_open_objs, use Response#opens" opens end |
#link ⇒ Object
— Response#link
short for Response.links[0]
284 285 286 |
# File 'lib/webunit/response.rb', line 284 def link @links[0] end |
#open ⇒ Object
— Response#open
short for Response.opens[0]
361 362 363 |
# File 'lib/webunit/response.rb', line 361 def open opens[0] end |
#opens ⇒ Object
— Response#opens
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/webunit/response.rb', line 331 def opens a = [] re_script = %r|<!--(.*?)// *-->|m re_open = %r|open\( *['"]([^'"]*)['"], *['"]([^'"]*)['"] *\)| body_rest = response.body while re_script =~ body_rest body_rest = $' rest = $1 while re_open =~ rest ah = {} ah['file'] = $1 ah['target'] = $2 ah['url'] = complete_url( ah['file'], @url ) a << JSciriptOpenObject::new( ah ) rest = $' end end a end |
#params ⇒ Object
— Response#param
short for Response.forms[0].params
267 268 269 |
# File 'lib/webunit/response.rb', line 267 def params @forms[0].params end |
#pbody ⇒ Object
— Response#pbody
369 370 371 372 373 |
# File 'lib/webunit/response.rb', line 369 def pbody puts "\n" + '-' * 72 puts self.format puts '-' * 72 end |
#push(button = nil) ⇒ Object
257 258 259 260 |
# File 'lib/webunit/response.rb', line 257 def push( =nil ) $stderr.puts "no longer support Response#push, use Response#submit" submit end |
#readlink(str) ⇒ Object
— Response#readlink( str )
same as Htmlelem#find.
219 220 221 |
# File 'lib/webunit/response.rb', line 219 def readlink( str ) @tree.readlink( str ) end |
#redirect ⇒ Object
— Response#redirect
319 320 321 322 323 324 325 |
# File 'lib/webunit/response.rb', line 319 def redirect if @code == '302' @links[0].read else raise BadOperation end end |
#search(str) ⇒ Object
— Response#search( str )
same as Htmlelem#search.
210 211 212 |
# File 'lib/webunit/response.rb', line 210 def search( str ) @tree.search( str ) end |
#submit(button = nil, x = nil, y = nil) ⇒ Object
— Response#submit( button=nil,x=nil,y=nil)
short for Response.forms[0].submit.
253 254 255 |
# File 'lib/webunit/response.rb', line 253 def submit( =nil, x=nil, y=nil ) @forms[0].submit( , x, y ) end |
#table ⇒ Object
— Response#table
short for Response.tables[0]
302 303 304 |
# File 'lib/webunit/response.rb', line 302 def table @tables[0] end |
#title ⇒ Object
— Response#title
return title.
192 193 194 |
# File 'lib/webunit/response.rb', line 192 def title @tree.find( 'title' )[0].data end |