Class: Web::Unit::Response

Inherits:
Object show all
Includes:
Utils
Defined in:
lib/web/unit/response.rb

Constant Summary collapse

@@host =
nil
@@port =
nil
@@http =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

#complete_url, #orthop_url, #parse_url

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



20
21
22
# File 'lib/web/unit/response.rb', line 20

def body
  @body
end

#codeObject (readonly)

Returns the value of attribute code.



20
21
22
# File 'lib/web/unit/response.rb', line 20

def code
  @code
end

#formsObject (readonly)

Returns the value of attribute forms.



21
22
23
# File 'lib/web/unit/response.rb', line 21

def forms
  @forms
end

#framesObject (readonly)

Returns the value of attribute frames.



21
22
23
# File 'lib/web/unit/response.rb', line 21

def frames
  @frames
end

#imagesObject (readonly)

Returns the value of attribute images.



21
22
23
# File 'lib/web/unit/response.rb', line 21

def images
  @images
end

Returns the value of attribute links.



21
22
23
# File 'lib/web/unit/response.rb', line 21

def links
  @links
end

#methodObject (readonly)

Returns the value of attribute method.



20
21
22
# File 'lib/web/unit/response.rb', line 20

def method
  @method
end

#passwordObject

Returns the value of attribute password.



22
23
24
# File 'lib/web/unit/response.rb', line 22

def password
  @password
end

#responseObject (readonly)

Returns the value of attribute response.



20
21
22
# File 'lib/web/unit/response.rb', line 20

def response
  @response
end

#tablesObject (readonly)

Returns the value of attribute tables.



21
22
23
# File 'lib/web/unit/response.rb', line 21

def tables
  @tables
end

#treeObject (readonly)

Returns the value of attribute tree.



21
22
23
# File 'lib/web/unit/response.rb', line 21

def tree
  @tree
end

#urlObject (readonly)

Returns the value of attribute url.



20
21
22
# File 'lib/web/unit/response.rb', line 20

def url
  @url
end

#usernameObject

Returns the value of attribute username.



22
23
24
# File 'lib/web/unit/response.rb', line 22

def username
  @username
end

Class Method Details

.get(url, data = nil, up = nil) ⇒ Object

— Response.get(url,data=nil)

return a Response.


28
29
30
31
32
33
# File 'lib/web/unit/response.rb', line 28

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.


51
52
53
54
# File 'lib/web/unit/response.rb', line 51

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.


39
40
41
42
43
44
# File 'lib/web/unit/response.rb', line 39

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

.resetObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/web/unit/response.rb', line 56

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



169
170
171
# File 'lib/web/unit/response.rb', line 169

def add_form( form )
  @forms.push( form )
end

#add_frame(frame) ⇒ Object



173
174
175
# File 'lib/web/unit/response.rb', line 173

def add_frame( frame )
  @frames.push( frame )
end

#add_image(image) ⇒ Object



181
182
183
# File 'lib/web/unit/response.rb', line 181

def add_image( image )
  @images.push( image )
end


165
166
167
# File 'lib/web/unit/response.rb', line 165

def add_link( link )
  @links.push( link )
end

#add_table(table) ⇒ Object



177
178
179
# File 'lib/web/unit/response.rb', line 177

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.


232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/web/unit/response.rb', line 232

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



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/web/unit/response.rb', line 75

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

#feedObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/web/unit/response.rb', line 144

def feed
  @forms = []
  @tables = []
  @links = []
  @frames = []
  @images = []
  
  # added by patrick to make redirect test work
  if ( @response && @response['location'] && @code == "302" )
    add_link( Link.new( 'href' => @response['location'],
                        'target' => '',
                        'onclick' => '' ) )
  end
  # end added by patrick to make redirect test work
  
  p = Parser.new
  @tree = p.feed( self )
  p.close
  freeze
end

#find(tag) ⇒ Object

— Response#find( tag )

same as Htmlelem#find.


205
206
207
# File 'lib/web/unit/response.rb', line 205

def find( tag )
  @tree.find( tag )
end

#formObject

— Response#form

short for Response.forms[0]


297
298
299
# File 'lib/web/unit/response.rb', line 297

def form
  @forms[0]
end

#formatObject

— Response#format



383
384
385
386
387
388
389
# File 'lib/web/unit/response.rb', line 383

def format
  w = MemWriter.new
  p = HTMLParser.new( AbstractFormatter.new( w ) )
  p.feed( @body )
  p.close
  w.to_s
end

#frameObject

— Response#frame

short for Response.frames[0]


315
316
317
# File 'lib/web/unit/response.rb', line 315

def frame
  @frames[0]
end

#freezeObject



185
186
187
188
189
# File 'lib/web/unit/response.rb', line 185

def freeze
  @forms.each do |f|
    f.freeze
  end
end

#imageObject

— Response#image



279
280
281
# File 'lib/web/unit/response.rb', line 279

def image
  self.images[0]
end

#init_html(html, url) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/web/unit/response.rb', line 67

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

Raises:



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/web/unit/response.rb', line 88

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 ) )
  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
        data
        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_objsObject



355
356
357
358
# File 'lib/web/unit/response.rb', line 355

def js_open_objs
  $stderr.puts "no longer support Response#js_open_objs, use Response#opens"
  opens
end

— Response#link

short for Response.links[0]


288
289
290
# File 'lib/web/unit/response.rb', line 288

def link
  @links[0]
end

#openObject

— Response#open

short for Response.opens[0]


365
366
367
# File 'lib/web/unit/response.rb', line 365

def open
  opens[0]
end

#opensObject

— Response#opens



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/web/unit/response.rb', line 335

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

#paramsObject

— Response#param

short for Response.forms[0].params


271
272
273
# File 'lib/web/unit/response.rb', line 271

def params
  @forms[0].params
end

#pbodyObject

— Response#pbody



373
374
375
376
377
# File 'lib/web/unit/response.rb', line 373

def pbody
  puts "\n" + '-' * 72
  puts self.format
  puts '-' * 72
end

#push(button = nil) ⇒ Object



261
262
263
264
# File 'lib/web/unit/response.rb', line 261

def push( button=nil )
  $stderr.puts "no longer support Response#push, use Response#submit"
  submit
end

— Response#readlink( str )

same as Htmlelem#find.


223
224
225
# File 'lib/web/unit/response.rb', line 223

def readlink( str )
  @tree.readlink( str )
end

#redirectObject

— Response#redirect



323
324
325
326
327
328
329
# File 'lib/web/unit/response.rb', line 323

def redirect
  if @code == '302'
    @links[0].read
  else
    raise BadOperation
  end
end

#search(str) ⇒ Object

— Response#search( str )

same as Htmlelem#search.


214
215
216
# File 'lib/web/unit/response.rb', line 214

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.


257
258
259
# File 'lib/web/unit/response.rb', line 257

def submit( button=nil, x=nil, y=nil )
  @forms[0].submit( button, x, y )
end

#tableObject

— Response#table

short for Response.tables[0]


306
307
308
# File 'lib/web/unit/response.rb', line 306

def table
  @tables[0]
end

#titleObject

— Response#title

return title.


196
197
198
# File 'lib/web/unit/response.rb', line 196

def title
  @tree.find( 'title' )[0].data
end