-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdsl.rb
153 lines (130 loc) · 3.49 KB
/
dsl.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
143
144
145
146
147
148
149
150
151
152
153
require 'httparty'
require 'uri'
require_relative 'rule'
require_relative 'rules_container'
require_relative 'mailer'
require_relative 'log_container'
$stdout.sync = true
class Dsl
include HTTParty
follow_redirects false
MAX_REDIRECTS = 3
attr_accessor :id, :rules
def initialize(id, &block)
@id = id
@rules = RulesContainer.new()
@logs = LogContainer.new()
instance_eval &block
end
def get(url, options = {})
request('GET', url, options)
end
def post(url, options = {})
request('POST', url, options)
end
def follow_redirects(rule, method)
redirects_count = 0
input_url = URI.encode(rule.url)
result = []
loop do
case method
when 'GET'
response_value = self.class.get(input_url)
when 'POST'
response_value = self.class.post(input_url, body: rule.body_to_json)
else
end
result << {
url: input_url,
status: response_value.code.to_s,
redirects: redirects_count,
content_type: response_value.header['content-type']
}
input_url = response_value.header['location'] ? URI.encode(response_value.header['location']) : nil
redirects_count +=1
if input_url && input_url !~ URI::regexp
uri = URI(rule.url)
uri.path = input_url
input_url = uri.to_s
end
break unless (input_url && redirects_count < MAX_REDIRECTS)
end
result
end
def get_response_respond_with(data)
data.last[:url]
end
def get_content_type(data)
data.last[:content_type]
end
def get_response_redirects_count(data)
data.last[:redirects]
end
def validate_redirects(data, redirect_code)
data.each do |url_data|
return url_data[:status] if url_data[:status] != redirect_code.to_s
end
true
end
def request(method, url, options = {})
rule = Rule.new(url, options)
@rules.add(rule)
result_ok = true
results = []
begin
results = follow_redirects(rule, method)
if rule.redirect_url
respond_url = get_response_respond_with(results)
result = rule.redirect_url == respond_url
result_ok &&= result
rule.error_message += "the final destination is #{respond_url}, " if false == result
end
if rule.response_code
valid_or_code = validate_redirects(results[0..(results.size-2)], rule.response_code)
result_ok &&= true === valid_or_code
rule.error_message += "response code is #{valid_or_code}, " unless true === valid_or_code
end
if rule.content_type
content_type = get_content_type(results)
result = rule.content_type == content_type
result_ok &&= result
rule.error_message += "#{content_type} content type, " if false == result
end
rule.error_message += "redirects count: #{get_response_redirects_count(results)}" unless result_ok
rescue HTTParty::Error => e
error = 'HttParty::Error '+ e.message
rule.error_message = error
result_ok = false
rescue StandardError => e
error = 'StandardError '+ e.message
rule.error_message = error
result_ok = false
end
print result_ok ? "+" : "-"
end
def display_results
puts summary_results
self
end
def summary_results
res = "\n---------------\nTests done: #{rules.count}\n"
if rules.with_errors.any?
res += "Errors: #{rules.with_errors.length}\n---------------\n"
else
res += "All fine :)\n---------------"
end
res
end
def send_email
if is_someting_new
@logs.write_logs(rules.errors_to_s)
unless ENV['RACK_ENV'] == 'test'
mailer = Mailer.new(summary_results, rules.with_errors)
mailer.send
end
end
end
def is_someting_new
@logs.read_logs != rules.errors_to_s
end
end