webtester 0.0.5

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 42dc7b520b4d46aa37845f7fa640ffd702f5d7f3
4
+ data.tar.gz: 15456b956f5951d26cfd613a59ed1f7b4b9cf65d
5
+ SHA512:
6
+ metadata.gz: a2ea168e56940107ff533fe02e5bf33dcbc99e02016c4c17f190f6e1e1f849eeaa3f4596e1509321c67b9b5ac9f69073eb7c11ebb557f8c2eebf9928fe9f62c0
7
+ data.tar.gz: 84a63c6c4566423ea29e741850b8b3ecac266e53fe60a9d42e9f247844a393ade37d8a29eece7dedf137e381ce1299785ce6ec300c8fab35aa5c5485e4b10469
data/lib/cookie.rb ADDED
@@ -0,0 +1,60 @@
1
+ module WT
2
+ class Cookie
3
+
4
+ attr_accessor :name
5
+ attr_accessor :value
6
+ attr_accessor :domain
7
+ attr_accessor :path
8
+ attr_accessor :secure
9
+ attr_accessor :expires
10
+ attr_accessor :http_only
11
+
12
+ def initialize
13
+ @domain = nil
14
+ @expires = nil
15
+ @path = ''
16
+ @http_only = false
17
+ @secure = false
18
+ end
19
+
20
+ def self.parse(str)
21
+ cookie = Cookie.new
22
+
23
+ parts = str.split ';'
24
+
25
+ parts.each do |part|
26
+ part.strip!
27
+
28
+ if part.match /^HttpOnly$/i
29
+ cookie.http_only = true
30
+ elsif part.downcase == 'secure'
31
+ cookie.secure = true
32
+ elsif part.match /^Domain=(.*)$/i
33
+ cookie.domain = "#{$1}"
34
+ elsif part.match /^Path=(.*)$/i
35
+ cookie.path = "#{$1}"
36
+ elsif part.match /^Expires=(.*)$/i
37
+ cookie.expires = "#{$1}"
38
+ elsif part.match /^(.*)=(.*)$/i
39
+ cookie.name = "#{$1}"
40
+ cookie.value = "#{$2}"
41
+ end
42
+ end
43
+
44
+ return cookie
45
+ end
46
+
47
+ def to_s
48
+ attrs = []
49
+
50
+ attrs << "#{@name}=#{@value}; "
51
+ attrs << "Path=#{@path}; "
52
+ attrs << "Expires=#{@expires}; " if @expires
53
+ attrs << "Domain=#{@domain}; " if @domain
54
+ attrs << "secure; " if @secure
55
+ attrs << "HttpOnly" if @http_only
56
+
57
+ attrs.join
58
+ end
59
+ end
60
+ end
data/lib/request.rb ADDED
@@ -0,0 +1,81 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+
4
+ require 'result'
5
+ require 'cookie'
6
+
7
+ module WT
8
+ class Request
9
+
10
+ attr_accessor :request_type
11
+ attr_accessor :request_url
12
+ attr_accessor :request_data
13
+ attr_accessor :follow_redirect
14
+ attr_accessor :headers
15
+
16
+ def initialize(request_type, request_url, request_data = {})
17
+ @request_type = request_type
18
+ @request_url = request_url
19
+ @request_data = request_data
20
+ @follow_redirect = true
21
+ @headers = {}
22
+ end
23
+
24
+ def exec
25
+ if @request_type == "GET"
26
+ self.get
27
+ elsif @request_type == "POST"
28
+ self.post
29
+ end
30
+ end
31
+
32
+ def get
33
+ url = URI.parse @request_url
34
+ req = Net::HTTP::Get.new(url.to_s)
35
+
36
+ return self.send_request url.host, url.port, req
37
+ end
38
+
39
+ def post
40
+ url = URI.parse @request_url
41
+ req = Net::HTTP::Post.new(url.to_s)
42
+
43
+ return send_request url.host, url.port, req
44
+ end
45
+
46
+ def send_request(host, port, req)
47
+ begin
48
+ req['Cookie'] = @headers['Cookie'].to_s if @headers['Cookie']
49
+ req.body = URI.encode_www_form @request_data if @request_data.length > 0
50
+
51
+ http = Net::HTTP.new host, port
52
+
53
+ response = http.request req
54
+
55
+ if not response
56
+ raise "Could not connect to host: #{host}:#{port}"
57
+ end
58
+
59
+ result = WT::Result.new response
60
+ result.request_type = @request_type
61
+ result.request_url = @request_url
62
+
63
+ if @follow_redirect and (response.code == '301' or response.code == '302')
64
+ @request_data = {}
65
+ @request_url = response['Location']
66
+ return self.get
67
+ end
68
+
69
+ return result
70
+ rescue Exception => e
71
+ result = WT::Result.new '0', 'empty', nil
72
+ result.request_type = @request_type
73
+ result.request_url = @request_url
74
+ result.message = e
75
+
76
+ return result
77
+ end
78
+ end
79
+ end
80
+
81
+ end
data/lib/result.rb ADDED
@@ -0,0 +1,12 @@
1
+ module WT
2
+ class Result
3
+ attr_accessor :content_type
4
+ attr_accessor :request_type
5
+ attr_accessor :request_url
6
+ attr_accessor :response
7
+
8
+ def initialize(response)
9
+ @response = response
10
+ end
11
+ end
12
+ end
data/lib/test.rb ADDED
@@ -0,0 +1,139 @@
1
+ require 'request'
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
+ #
58
+
59
+ module WT
60
+ class Test
61
+ attr_accessor :name
62
+ attr_accessor :requests
63
+ attr_accessor :cookie
64
+ attr_accessor :form_fields
65
+
66
+ def initialize(name)
67
+ @name = name
68
+ @requests = []
69
+ @cookie = nil
70
+ @form_fields = {}
71
+ end
72
+
73
+ def add_request(request)
74
+ @requests << request
75
+ end
76
+
77
+ def exec
78
+ @requests.each do |req|
79
+ req.headers['Cookie'] = @cookie if @cookie
80
+
81
+ @form_fields.each do |name, value|
82
+ req.request_data[name].gsub! "$?", "#{value}" if req.request_data and req.request_data[name]
83
+ end
84
+
85
+ result = req.exec
86
+
87
+ @cookie = WT::Cookie.parse result.response['Set-Cookie'] if result.response['Set-Cookie']
88
+ @form_fields = find_form_fields(result.response.body)
89
+
90
+ yield result
91
+
92
+ break if result.response.code != '200'
93
+ end
94
+ end
95
+
96
+ private
97
+
98
+ def find_form_fields(content)
99
+
100
+ form_fields = {}
101
+
102
+ str = content
103
+
104
+ while true
105
+ sidx = str.index('<input')
106
+
107
+ break if !sidx
108
+
109
+ str = str[sidx,str.length]
110
+
111
+ eidx = str.index('>')
112
+
113
+ break if !eidx
114
+
115
+ attrs = extract_form_field_attrs "#{str[0, eidx+1]}"
116
+
117
+ if attrs['name'] && attrs['value']
118
+ form_fields[attrs['name']] = attrs['value']
119
+ end
120
+
121
+ str = str[eidx, str.length]
122
+ end
123
+
124
+ return form_fields
125
+ end
126
+
127
+ def extract_form_field_attrs(str)
128
+ attrs = {}
129
+ regexp=/<input\s+.*(name|value)="([^"]*)".*\s+(name|value)="([^"]*)"\s*.*\/>/
130
+
131
+ str.match(regexp) do
132
+ attrs["#{$1}"] = "#{$2}"
133
+ attrs["#{$3}"] = "#{$4}"
134
+ end
135
+
136
+ return attrs
137
+ end
138
+ end
139
+ end
data/lib/webtester.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'test'
2
+ require 'request'
3
+ require 'result'
4
+ require 'cookie'
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: webtester
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.5
5
+ platform: ruby
6
+ authors:
7
+ - Diego F. Nascimento
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-07 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: " A simple API to create HTTP Request tests. "
14
+ email: diego.fnascimento@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/cookie.rb
20
+ - lib/request.rb
21
+ - lib/result.rb
22
+ - lib/test.rb
23
+ - lib/webtester.rb
24
+ homepage: http://rubygems.org/gems/webtester
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.4.6
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: Web Application Request Tester
48
+ test_files: []