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
|
# File 'lib/aspec/test.rb', line 26
def run(config)
formatter = config.formatter
is_slow = config.slow?
@app = config.get_app_under_test.call
start_time = Time.at(0)
failed = false
@steps.each_with_index do |step, time_delta|
if is_slow
sleep 0.5
end
if ARGV.include?("--debug")
formatter.debug(step)
end
if failed
formatter.step_error_title(step)
next
end
if step[:comment]
formatter.(step[:comment])
else
Time.stub!(:now).and_return(start_time + 2*time_delta)
begin
if step[:method][0] == ">"
method = step[:method][1..-1]
validate_method(method)
url = "http://" + step[:url]
FakeWeb.register_uri(method.downcase.to_sym, url,
:body => step[:exp_response],
:content_type => step[:exp_content_type]
)
else
validate_method(step[:method])
send(step[:method].downcase, step[:url])
end
rescue Object => e
formatter.exception(" " + e.class.to_s + ": " + e.message)
e.backtrace.each do |backtrace_line|
formatter.exception(" " + backtrace_line) unless backtrace_line =~ /vendor\/bundle/ or backtrace_line =~ /test.rb/
end
failed = true
end
unless failed or step[:method][0] == ">"
if last_response.status.to_s != step[:exp_status]
formatter.exception(" * Expected status #{step[:exp_status]} got #{last_response.status}")
failed = true
end
if step[:exp_content_type] == "application/json" && !step[:resp_is_regex]
begin
expected_object = JSON.parse(step[:exp_response])
begin
response_object = JSON.parse(last_response.body)
if expected_object != response_object
formatter.exception(" * Expected response #{JSON.pretty_generate(expected_object)} got #{JSON.pretty_generate(response_object)}")
failed = true
end
rescue JSON::ParserError
formatter.exception(" * Response did not parse correctly as JSON: #{last_response.body.inspect}")
failed = true
end
rescue JSON::ParserError
formatter.exception(" * Expectation did not parse correctly as JSON: #{step[:exp_response].inspect}")
failed = true
end
else
if step[:resp_is_regex]
pattern = nil, body = nil
if !(step[:exp_content_type].start_with? 'text/')
pattern = Regexp.new(step[:exp_response].force_encoding("ASCII-8BIT"), Regexp::FIXEDENCODING)
body = last_response.body.to_s.force_encoding("ASCII-8BIT")
else
pattern = Regexp.new(step[:exp_response])
body = last_response.body.to_s
end
if !(body =~ pattern)
formatter.exception(" * Expected response pattern #{step[:exp_response].inspect} didn't match #{last_response.body.inspect}")
failed = true
end
elsif !step[:resp_is_regex] & (last_response.body.to_s != step[:exp_response])
formatter.exception(" * Expected response #{step[:exp_response].inspect} got #{last_response.body.inspect[0..50] + "..."}")
failed = true
end
end
if step[:exp_content_type]
= "#{step[:exp_content_type]}"
<< ";charset=utf-8" unless .start_with? "image/"
if last_response.["Content-Type"] !=
formatter.exception(" * Expected content type #{} got #{last_response.["Content-Type"]}")
failed = true
end
end
end
if failed
formatter.step_error(step)
else
formatter.step_pass(step)
end
end
end
!failed
end
|