Class: WebUnit::Response

Inherits:
Object show all
Includes:
Utils
Defined in:
lib/webunit/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.



19
20
21
# File 'lib/webunit/response.rb', line 19

def body
  @body
end

#codeObject (readonly)

Returns the value of attribute code.



19
20
21
# File 'lib/webunit/response.rb', line 19

def code
  @code
end

#formsObject (readonly)

Returns the value of attribute forms.



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

def forms
  @forms
end

#framesObject (readonly)

Returns the value of attribute frames.



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

def frames
  @frames
end

#imagesObject (readonly)

Returns the value of attribute images.



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

def images
  @images
end

Returns the value of attribute links.



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

def links
  @links
end

#methodObject (readonly)

Returns the value of attribute method.



19
20
21
# File 'lib/webunit/response.rb', line 19

def method
  @method
end

#passwordObject

Returns the value of attribute password.



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

def password
  @password
end

#responseObject (readonly)

Returns the value of attribute response.



19
20
21
# File 'lib/webunit/response.rb', line 19

def response
  @response
end

#tablesObject (readonly)

Returns the value of attribute tables.



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

def tables
  @tables
end

#treeObject (readonly)

Returns the value of attribute tree.



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

def tree
  @tree
end

#urlObject (readonly)

Returns the value of attribute url.



19
20
21
# File 'lib/webunit/response.rb', line 19

def url
  @url
end

#usernameObject

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

.resetObject



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


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

#feedObject



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

#formObject

— Response#form

short for Response.forms[0]


293
294
295
# File 'lib/webunit/response.rb', line 293

def form
  @forms[0]
end

#formatObject

— 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

#frameObject

— Response#frame

short for Response.frames[0]


311
312
313
# File 'lib/webunit/response.rb', line 311

def frame
  @frames[0]
end

#freezeObject



181
182
183
184
185
# File 'lib/webunit/response.rb', line 181

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

#imageObject

— 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

Raises:



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_objsObject



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

— Response#link

short for Response.links[0]


284
285
286
# File 'lib/webunit/response.rb', line 284

def link
  @links[0]
end

#openObject

— Response#open

short for Response.opens[0]


361
362
363
# File 'lib/webunit/response.rb', line 361

def open
  opens[0]
end

#opensObject

— 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

#paramsObject

— Response#param

short for Response.forms[0].params


267
268
269
# File 'lib/webunit/response.rb', line 267

def params
  @forms[0].params
end

#pbodyObject

— 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( button=nil )
  $stderr.puts "no longer support Response#push, use Response#submit"
  submit
end

— Response#readlink( str )

same as Htmlelem#find.


219
220
221
# File 'lib/webunit/response.rb', line 219

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

#redirectObject

— 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( button=nil, x=nil, y=nil )
  @forms[0].submit( button, x, y )
end

#tableObject

— Response#table

short for Response.tables[0]


302
303
304
# File 'lib/webunit/response.rb', line 302

def table
  @tables[0]
end

#titleObject

— Response#title

return title.


192
193
194
# File 'lib/webunit/response.rb', line 192

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