-
Notifications
You must be signed in to change notification settings - Fork 1
/
testcase.rb
173 lines (141 loc) · 4.08 KB
/
testcase.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# Testcase expects three required parameters:
# 1. method (GET, POST, etc)
# 2. status (the expected response from the server, e.g. 200 or 406)
# 3. response (the body of the response from the server)
# There are also two optional params:
# 1. path (the url path, e.g. /foo/bar)
# 2. headers (the list of headers to be passed as part of the query)
# 3. body (body of the request made to the server)
require 'net/http'
require 'json'
class Testcase
@@state = {}
def initialize(paramhash, verbose)
@paramhash = paramhash
@verbose = verbose
end
def runtest(host)
begin
fullpath = host
if @paramhash['path']
fullpath += @paramhash['path']
end
if @verbose
puts "Full path: " + fullpath
end
method = @paramhash['method']
raise ArgumentError.new("Unable to continue tests due to missing http method") unless method
if method == 'GET'
runtestget(fullpath)
elsif method == 'POST'
runtestpost(fullpath, false)
elsif method == 'PUT'
runtestpost(fullpath, true)
end
rescue Exception => e
puts e.message
end
end
def checkstatus(res)
result = false
begin
expected_status = @paramhash['status']
raise ArgumentError.new("No expected response status for this test specified") unless expected_status
result = res.code.eql?(expected_status.to_s)
if @verbose
output = "Checking for status #{expected_status} - "
if result
output += "Matched!"
else
output += "Not Matched! - Got " + res.code + " instead"
end
puts output
end
rescue Exception => e
puts e.message
end
# response code from server matches would return true, false otherwise.
result
end
def checkresponse(res)
result = false
begin
expected_response = JSON.parse(@paramhash['response'])
raise ArgumentError.new("No expected response status for this test specified") unless expected_response
responsebody = JSON.parse(res.body)
result = (responsebody == expected_response)
if @verbose
puts responsebody.to_s
puts expected_response.to_s
if result
puts "Response body matched"
else
errormsg = "Incorrect response body received: " + responsebody.to_s
puts errormsg
end
end
rescue Exception => e
puts e.message
end
result
end
def addheaders(request)
# Add standard headers
request["Accept"] = "application/json"
request["Content-type"] = "application/json"
# Add other headers
if @paramhash['headers']
headers = @paramhash['headers']
headers = headers[1..-2].split(',').collect! {|n| n.to_s.strip}
for header in headers
pair = header.split(":")
request[pair[0].strip] = pair[1].strip
end
end
end
def runtestget(fullpath)
begin
if @verbose
puts "Running GET test...."
end
# Creating request
uri = URI.parse(fullpath)
request = Net::HTTP::Get.new(uri.path)
addheaders(request)
# Make the actual rest call
response = Net::HTTP.new(uri.host, uri.port).start {|http| http.request(request) }
checkstatus(response) and checkresponse(response)
rescue Exception => e
puts e.message
end
end
def runtestpost(fullpath, useput)
begin
if @verbose
puts "Running POST test...."
end
uri = URI.parse(fullpath)
data = ""
if @paramhash['body']
data = @paramhash['body']
end
if @verbose
puts "Body: " + data
end
# Creating the request
if useput
request = Net::HTTP::Put.new(uri.path)
else
request = Net::HTTP::Post.new(uri.path)
end
addheaders(request)
request.body = data
# Make the actual rest call
response = Net::HTTP.new(uri.host, uri.port).start {|http| http.request(request) }
# Validate response
checkstatus(response) and checkresponse(response)
rescue Exception => e
puts e.message
end
end
end