webtester 0.0.7 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aaaff53873b7affd9e6922638b72b6b03c22c5c1
4
- data.tar.gz: 2463767556fdecfe728e63a9201cb4621d67d943
3
+ metadata.gz: f4bfe738723c0ec5a5fb0073e68d55594b7cc64a
4
+ data.tar.gz: 85522d011791fb9de097b30fb4c004101f2c4314
5
5
  SHA512:
6
- metadata.gz: 9f2965ca5213a7ce9e6ad068a73b71ac28bb931d4dcaabf285386ccd51d6bb1076e245af5889a389c5dd577e6de54578f14e264a3790b0f70eb4cd8c87aecf84
7
- data.tar.gz: cbdb5b21de896652017a03b25af3cc676aafc60eed2d34dd17f6111e423903c8d5ba56d9a70ae1046791fcee354d70a6591566f602ab79389f023794b2a45ef6
6
+ metadata.gz: 0aebc95872b35abea66ca56d30509fe6f6a38f76d26d0133d1fa88a71869672feea97fa628cd4f7fad1021800ae53a617ec13166e262ecfca617a51debac3cd4
7
+ data.tar.gz: d4fd83062445233eccba32ce1bbdd93759b0f2cdccdf8c49ddb50296cb38cf40870e74ea847127771595034ec74653ef8c09dc2de4cc2d504ffd9d918b656242
data/lib/request.rb CHANGED
@@ -35,19 +35,28 @@ module WT
35
35
 
36
36
  def get
37
37
  url = URI.parse @request_url
38
+ url.path = '/' unless url.path.length > 0
38
39
  req = Net::HTTP::Get.new(url.to_s)
39
40
 
40
- return self.send_request url.host, url.port, req
41
+ puts url.to_s
42
+
43
+ return self.send url.host, url.port, req
41
44
  end
42
45
 
43
46
  def post
44
47
  url = URI.parse @request_url
48
+ url.path = '/' unless url.path.length > 0
45
49
  req = Net::HTTP::Post.new(url.to_s)
46
50
 
47
- return send_request url.host, url.port, req
51
+ puts url.to_s
52
+
53
+ return send url.host, url.port, req
48
54
  end
49
55
 
50
- def send_request(host, port, req)
56
+ def send(host, port, req)
57
+
58
+ ret = nil
59
+
51
60
  begin
52
61
  req['Cookie'] = @headers['Cookie'].to_s if @headers['Cookie']
53
62
  req.body = URI.encode_www_form @request_data if @request_data.length > 0
@@ -60,31 +69,24 @@ module WT
60
69
  http = Net::HTTP.new(host, port)
61
70
  end
62
71
 
63
- response = http.request req
72
+ response = http.request(req)
64
73
 
65
- if not response
66
- raise "Could not connect to host: #{host}:#{port}"
67
- end
74
+ raise "Could not connect to host: #{host}:#{port}" unless response
68
75
 
69
- result = WT::Result.new response
70
- result.request_type = @request_type
71
- result.request_url = @request_url
76
+ result = WT::Result.new(self, response)
72
77
 
73
78
  if @follow_redirect and (response.code == '301' or response.code == '302')
74
79
  @request_data = {}
75
80
  @request_url = response['Location']
76
- return self.get
81
+ ret = self.get
82
+ else
83
+ ret = result
77
84
  end
78
-
79
- return result
80
85
  rescue Exception => e
81
- result = WT::Result.new '0', 'empty', nil
82
- result.request_type = @request_type
83
- result.request_url = @request_url
84
- result.message = e
85
-
86
- return result
86
+ ret = WT::Result.new self, nil
87
87
  end
88
+
89
+ return ret
88
90
  end
89
91
  end
90
92
 
data/lib/result.rb CHANGED
@@ -1,11 +1,10 @@
1
1
  module WT
2
2
  class Result
3
- attr_accessor :content_type
4
- attr_accessor :request_type
5
- attr_accessor :request_url
3
+ attr_accessor :request
6
4
  attr_accessor :response
7
5
 
8
- def initialize(response)
6
+ def initialize(request, response)
7
+ @request = request
9
8
  @response = response
10
9
  end
11
10
  end
data/lib/test.rb CHANGED
@@ -1,77 +1,15 @@
1
1
  require 'request'
2
2
  require 'cookie'
3
-
4
- # = Example
5
- #
6
- # #!/usr/bin/env ruby
7
- #
8
- # require 'webtester'
9
- #
10
- # #
11
- # # create a new test called "Login Test"
12
- # #
13
- #
14
- # loginTest = WT::Test.new "Login Test"
15
- #
16
- # #
17
- # # create 2 requests. The first one will access login page through a GET
18
- # # request page to receive the session token and a View State
19
- # # obtained from the body of the result.
20
- # # The second will send the POST request with all the parameters.
21
- # #
22
- #
23
- # req1 = WT::Request.new "GET", "http://example.com/login.jsf"
24
- # req2 = WT::Request.new "POST", "http://example/login.jsf",
25
- # { 'form' => 'form',
26
- # 'form:login' => 'user1',
27
- # 'form:senha' => 'passwd',
28
- # 'form:logar.x' => '0',
29
- # 'form:logar.y' => '0',
30
- # 'javax.faces.ViewState' => "$?" }
31
- #
32
- # #
33
- # # if the HTTP Response code be 301 or 302 (Redirect), it will not
34
- # # follow the 'Location' HTTP Header. If follow_redirect be 'true',
35
- # # it would send another GET request to the address contained on
36
- # # the HTTP Header and would return the response of the second
37
- # # Request.
38
- # #
39
- #
40
- # req2.follow_redirect = false
41
- #
42
- # #
43
- # # Add the requests req1 and req2 to the "Login Test"
44
- # #
45
- #
46
- # loginTest.add_request(req1)
47
- # loginTest.add_request(req2)
48
- #
49
- # #
50
- # # Execute the test. The requests will be sent in the insertion
51
- # # order (add_request).
52
- # #
53
- #
54
- # loginTest.exec do |result|
55
- # puts "#{result.request_type} #{result.request_url} #{result.response.code} [#{result.response.message}]"
56
- # end
57
- #
3
+ require 'web_session'
58
4
 
59
5
  module WT
60
6
  class Test
61
7
  attr_accessor :name
62
8
  attr_accessor :requests
63
- attr_accessor :cookies
64
- attr_accessor :form_fields
65
- attr_accessor :proxy_host
66
- attr_accessor :proxy_port
67
9
 
68
- def initialize(name)
10
+ def initialize(name: 'New Test', requests: [])
69
11
  @name = name
70
- @requests = []
71
- @cookies = []
72
- @form_fields = {}
73
- @proxy_host = nil
74
- @proxy_port = 0
12
+ @requests = requests
75
13
  end
76
14
 
77
15
  def add_request(request)
@@ -79,76 +17,15 @@ module WT
79
17
  end
80
18
 
81
19
  def exec
82
- @requests.each do |req|
83
- req.headers['Cookie'] = WT::Cookie.explode(@cookies) if @cookies.length > 0
84
-
85
- @form_fields.each do |name, value|
86
- req.request_data[name].gsub! "$?", "#{value}" if req.request_data and req.request_data[name]
87
- end
88
-
89
- # set proxy if exists
90
- req.proxy_host = @proxy_host
91
- req.proxy_port = @proxy_port
92
-
93
- result = req.exec
94
-
95
- all_cookies = result.response['Set-Cookie']
96
-
97
- if all_cookies
98
- all_cookies.split(',').each do |str_cookie|
99
- @cookies << WT::Cookie.parse(str_cookie.strip)
100
- end
101
- end
102
-
103
- @form_fields = find_form_fields(result.response.body)
104
-
105
- yield result
106
-
107
- break if result.response.code != '200'
108
- end
109
- end
110
-
111
- private
112
-
113
- def find_form_fields(content)
20
+ session = WT::WebSession.new
114
21
 
115
- form_fields = {}
116
-
117
- str = content
118
-
119
- while true
120
- sidx = str.index('<input')
121
-
122
- break if !sidx
123
-
124
- str = str[sidx,str.length]
125
-
126
- eidx = str.index('>')
127
-
128
- break if !eidx
129
-
130
- attrs = extract_form_field_attrs "#{str[0, eidx+1]}"
131
-
132
- if attrs['name'] && attrs['value']
133
- form_fields[attrs['name']] = attrs['value']
134
- end
135
-
136
- str = str[eidx, str.length]
137
- end
138
-
139
- return form_fields
140
- end
22
+ @requests.each do |req|
23
+ result = session.request(req)
141
24
 
142
- def extract_form_field_attrs(str)
143
- attrs = {}
144
- regexp=/<input\s+.*(name|value)="([^"]*)".*\s+(name|value)="([^"]*)"\s*.*\/>/
25
+ yield result;
145
26
 
146
- str.match(regexp) do
147
- attrs["#{$1}"] = "#{$2}"
148
- attrs["#{$3}"] = "#{$4}"
27
+ break unless ['200', '301', '302'].include?(result.response.code)
149
28
  end
150
-
151
- return attrs
152
29
  end
153
30
  end
154
31
  end
@@ -0,0 +1,91 @@
1
+ require 'request'
2
+ require 'cookie'
3
+
4
+ module WT
5
+ class WebSession
6
+ attr_accessor :cookies
7
+ attr_accessor :form_fields
8
+ attr_accessor :proxy_host
9
+ attr_accessor :proxy_port
10
+
11
+ def initialize
12
+ @cookies = []
13
+ @form_fields = {}
14
+ @proxy_host = nil
15
+ @proxy_port = 0
16
+ end
17
+
18
+ def request(req, &block)
19
+ req.headers['Cookie'] = WT::Cookie.explode(@cookies) if @cookies.length > 0
20
+
21
+ @form_fields.each do |name, value|
22
+ req.request_data[name].gsub! "$?", "#{value}" if req.request_data and req.request_data[name]
23
+ end
24
+
25
+ # set proxy if exists
26
+ req.proxy_host = @proxy_host
27
+ req.proxy_port = @proxy_port
28
+
29
+ result = req.exec
30
+
31
+ if result.response != nil
32
+ all_cookies = result.response['Set-Cookie']
33
+
34
+ if all_cookies
35
+ all_cookies.split(',').each do |str_cookie|
36
+ @cookies << WT::Cookie.parse(str_cookie.strip)
37
+ end
38
+ end
39
+
40
+ @form_fields = find_form_fields(result.response.body)
41
+ end
42
+
43
+ block.call(result) if block
44
+
45
+ return result
46
+ end
47
+
48
+ private
49
+
50
+ def find_form_fields(content)
51
+
52
+ form_fields = {}
53
+
54
+ str = content
55
+
56
+ while true
57
+ sidx = str.index('<input')
58
+
59
+ break if !sidx
60
+
61
+ str = str[sidx,str.length]
62
+
63
+ eidx = str.index('>')
64
+
65
+ break if !eidx
66
+
67
+ attrs = extract_form_field_attrs "#{str[0, eidx+1]}"
68
+
69
+ if attrs['name'] && attrs['value']
70
+ form_fields[attrs['name']] = attrs['value']
71
+ end
72
+
73
+ str = str[eidx, str.length]
74
+ end
75
+
76
+ return form_fields
77
+ end
78
+
79
+ def extract_form_field_attrs(str)
80
+ attrs = {}
81
+ regexp=/<input\s+.*(name|value)="([^"]*)".*\s+(name|value)="([^"]*)"\s*.*\/>/
82
+
83
+ str.match(regexp) do
84
+ attrs["#{$1}"] = "#{$2}"
85
+ attrs["#{$3}"] = "#{$4}"
86
+ end
87
+
88
+ return attrs
89
+ end
90
+ end
91
+ end
data/lib/webtester.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'test'
2
+ require 'web_session'
2
3
  require 'request'
3
4
  require 'result'
4
5
  require 'cookie'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webtester
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Diego F. Nascimento
@@ -20,6 +20,7 @@ files:
20
20
  - lib/request.rb
21
21
  - lib/result.rb
22
22
  - lib/test.rb
23
+ - lib/web_session.rb
23
24
  - lib/webtester.rb
24
25
  homepage: http://rubygems.org/gems/webtester
25
26
  licenses:
@@ -41,7 +42,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
41
42
  version: '0'
42
43
  requirements: []
43
44
  rubyforge_project:
44
- rubygems_version: 2.4.6
45
+ rubygems_version: 2.4.5
45
46
  signing_key:
46
47
  specification_version: 4
47
48
  summary: Web Application Request Tester