blix-rest 0.9.3 → 0.11.1
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 +4 -4
- data/README.md +484 -503
- data/lib/blix/rest/cache.rb +1 -29
- data/lib/blix/rest/controller.rb +80 -0
- data/lib/blix/rest/memory_cache.rb +30 -0
- data/lib/blix/rest/request_mapper.rb +36 -28
- data/lib/blix/rest/response.rb +10 -0
- data/lib/blix/rest/server.rb +57 -51
- data/lib/blix/rest/session.rb +8 -3
- data/lib/blix/rest/version.rb +1 -1
- data/lib/blix/rest.rb +7 -0
- data/lib/blix/utils/misc.rb +39 -0
- metadata +5 -9
- data/lib/blix/rest/cucumber/hooks.rb +0 -5
- data/lib/blix/rest/cucumber/request_steps.rb +0 -207
- data/lib/blix/rest/cucumber/resource_steps.rb +0 -28
- data/lib/blix/rest/cucumber/world.rb +0 -274
- data/lib/blix/rest/cucumber.rb +0 -8
@@ -1,207 +0,0 @@
|
|
1
|
-
#========== requests with tokens
|
2
|
-
|
3
|
-
Given(/^(.*?) requests token for service "(.*?)"$/) do |user,service|
|
4
|
-
path = "/myservices/" + service + "/token"
|
5
|
-
send_request('POST',user,path,nil)
|
6
|
-
@_token = valid_response.data["token"]
|
7
|
-
end
|
8
|
-
|
9
|
-
Given(/^(.*?) validates the token for service "(.*?)"$/) do |user,service|
|
10
|
-
path = "/services/" + service + "/validate/" + @_token
|
11
|
-
send_request('GET',user,path,nil)
|
12
|
-
end
|
13
|
-
|
14
|
-
|
15
|
-
# Given(/^guest gets "(.*?)"( .*)?$/) do |path,condition|
|
16
|
-
# path = add_token_to_path(path,@_token) if condition == " with token"
|
17
|
-
# send_request('GET',"guest",path,nil)
|
18
|
-
# end
|
19
|
-
|
20
|
-
# general requests
|
21
|
-
|
22
|
-
Given(/^(.*?) gets ["'](.*?)["']( with token)?$/) do |user, path, condition|
|
23
|
-
path = add_token_to_path(path,@_token) if condition == " with token"
|
24
|
-
send_request('GET',user,path,nil)
|
25
|
-
end
|
26
|
-
|
27
|
-
Given(/^(.*?) options ["'](.*?)["']( with token)?$/) do |user, path, condition|
|
28
|
-
path = add_token_to_path(path,@_token) if condition == " with token"
|
29
|
-
send_request('OPTIONS',user,path,nil)
|
30
|
-
end
|
31
|
-
|
32
|
-
Given(/^(.*?) posts ["'](.*?)["'] with (.*?)$/) do |user, path, json|
|
33
|
-
send_request('POST',user,path,json)
|
34
|
-
end
|
35
|
-
|
36
|
-
# doc string version
|
37
|
-
Given(/^(.*?) posts ["'](.*?)["'] with$/) do |user, path, json|
|
38
|
-
send_request('POST',user,path,json)
|
39
|
-
end
|
40
|
-
|
41
|
-
Given(/^(.*?) deletes ["'](.*?)["']$/) do |user, path|
|
42
|
-
send_request('DELETE',user,path,nil)
|
43
|
-
end
|
44
|
-
|
45
|
-
Given(/^(.*?) puts ["'](.*?)["'] with (.*?)$/) do |user, path, json|
|
46
|
-
send_request('PUT',user,path,json)
|
47
|
-
end
|
48
|
-
|
49
|
-
# doc string version
|
50
|
-
Given(/^(.*?) puts ["'](.*?)["'] with$/) do |user, path, json|
|
51
|
-
send_request('PUT',user,path,json)
|
52
|
-
end
|
53
|
-
|
54
|
-
# alternative format
|
55
|
-
|
56
|
-
Given(/^I am (.*?)$/) do | string |
|
57
|
-
@_current_user = string.split(' ')[-1]
|
58
|
-
end
|
59
|
-
|
60
|
-
Given(/^I get ["'](.*?)["']$/) do |path|
|
61
|
-
send_request('GET',@_current_user,path,nil)
|
62
|
-
end
|
63
|
-
|
64
|
-
Given(/^I post ["'](.*?)["'] with (.*?)$/) do |path, json|
|
65
|
-
send_request('POST',@_current_user,path,json)
|
66
|
-
end
|
67
|
-
|
68
|
-
Given(/^I delete ["'](.*?)["']$/) do | path|
|
69
|
-
send_request('DELETE',@_current_user,path,nil)
|
70
|
-
end
|
71
|
-
|
72
|
-
Given(/^I put ["'](.*?)["'] with (.*?)$/) do |path, json|
|
73
|
-
send_request('PUT',@_current_user,path,json)
|
74
|
-
end
|
75
|
-
|
76
|
-
# response steps ==============================
|
77
|
-
|
78
|
-
Then(/^the status should be (\d+)$/) do |code|
|
79
|
-
expect(valid_response.status).to eq code.to_i
|
80
|
-
end
|
81
|
-
|
82
|
-
Then(/^there should be an error$/) do
|
83
|
-
expect(valid_response.status.to_s[0,1]).not_to eq '2'
|
84
|
-
expect(valid_response.error).not_to be_nil
|
85
|
-
end
|
86
|
-
|
87
|
-
Then(/^the error message should include ["'](.*?)["']$/) do |field|
|
88
|
-
expect(valid_response.error && (valid_response.error =~ %r/#{field}/)).to_not be nil
|
89
|
-
end
|
90
|
-
|
91
|
-
Given(/^explain$/) do
|
92
|
-
explain
|
93
|
-
end
|
94
|
-
|
95
|
-
Then(/^the data type should be ["'](.*?)["']$/) do |type|
|
96
|
-
if valid_response.data.kind_of? Array
|
97
|
-
expect(valid_response.data[0]["_type"]).to eq type
|
98
|
-
else
|
99
|
-
expect(valid_response.data["_type"]).to eq type
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
|
104
|
-
Then(/^the data length should be (\d+)$/) do |len|
|
105
|
-
if valid_response.data.kind_of? Array
|
106
|
-
expect(valid_response.data.length).to eq len.to_i
|
107
|
-
else
|
108
|
-
1
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
Then(/^the data length should equal (\d+)$/) do |len|
|
113
|
-
if valid_response.data.kind_of? Array
|
114
|
-
expect(valid_response.data.length).to eq len.to_i
|
115
|
-
else
|
116
|
-
1
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
Then(/^the data "(.*?)" should == (.*?)$/) do |field,val|
|
121
|
-
if valid_data.kind_of? Array
|
122
|
-
data = valid_data[0]
|
123
|
-
else
|
124
|
-
data = valid_data
|
125
|
-
end
|
126
|
-
v = data[field].to_s
|
127
|
-
|
128
|
-
if val =~ %r{^@([^@]*)$}
|
129
|
-
expect(v).to eq store[$1].to_s
|
130
|
-
elsif val =~ %r{^(::)?[A-Z][A-z_a-z:]*$}
|
131
|
-
expect(v).to eq Module.const_get(val).to_s
|
132
|
-
elsif val =~ %r{^['"](.*)['"]$}
|
133
|
-
expect(v).to eq $1
|
134
|
-
end
|
135
|
-
|
136
|
-
end
|
137
|
-
|
138
|
-
Then(/^the data "(.*?)" should equal ["'](.*?)["']$/) do |field,val|
|
139
|
-
if valid_response.data.kind_of? Array
|
140
|
-
data = valid_response.data[0]
|
141
|
-
else
|
142
|
-
data = valid_response.data
|
143
|
-
end
|
144
|
-
expect(data[field].to_s).to eq val
|
145
|
-
end
|
146
|
-
|
147
|
-
Then(/^the data ["'](.*?)["'] should == nil$/) do |field|
|
148
|
-
if valid_response.data.kind_of? Array
|
149
|
-
data = valid_response.data[0]
|
150
|
-
else
|
151
|
-
data = valid_response.data
|
152
|
-
end
|
153
|
-
expect(data[field]).to be nil
|
154
|
-
end
|
155
|
-
|
156
|
-
Then(/^the data ["'](.*?)["'] should equal nil$/) do |field|
|
157
|
-
if valid_response.data.kind_of? Array
|
158
|
-
data = valid_response.data[0]
|
159
|
-
else
|
160
|
-
data =valid_response.data
|
161
|
-
end
|
162
|
-
expect(data[field]).to be nil
|
163
|
-
end
|
164
|
-
|
165
|
-
Then(/^the body should eq ["'](.*?)["']$/) do |val|
|
166
|
-
val.gsub!("\\n","\n")
|
167
|
-
expect(valid_response.body).to eq val
|
168
|
-
end
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
Then(/^the data should( not)? include ["'](.*?)["']$/) do |state, field|
|
173
|
-
if valid_response.data.kind_of? Array
|
174
|
-
data = valid_response.data[0]
|
175
|
-
else
|
176
|
-
data = valid_response.data
|
177
|
-
end
|
178
|
-
if state == " not"
|
179
|
-
expect(data.key?(field)).to eq false
|
180
|
-
else
|
181
|
-
expect(data.key?(field)).to eq true
|
182
|
-
end
|
183
|
-
end
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
Then(/^store the ["'](.*?)["'] as ["'](.*?)["']$/) do |name,key|
|
188
|
-
if valid_response.data.kind_of?(Array)
|
189
|
-
data = valid_response.data[0]
|
190
|
-
else
|
191
|
-
data = valid_response.data
|
192
|
-
end
|
193
|
-
if data.kind_of?(Hash) && data.key?(name)
|
194
|
-
store[key] = data[name]
|
195
|
-
end
|
196
|
-
end
|
197
|
-
|
198
|
-
Given(/^save the "([^"]*)"$/) do |name|
|
199
|
-
if valid_response.data.kind_of?(Array)
|
200
|
-
data = valid_response.data[0]
|
201
|
-
else
|
202
|
-
data = valid_response.data
|
203
|
-
end
|
204
|
-
if data.kind_of?(Hash) && data.key?(name)
|
205
|
-
store[name] = data[name]
|
206
|
-
end
|
207
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
class UserHash < Hash
|
2
|
-
def set(k,v)
|
3
|
-
self[k.to_s] = v
|
4
|
-
end
|
5
|
-
|
6
|
-
def get(k)
|
7
|
-
self[k.to_s]
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
Given(/^the following users exist:$/) do |table|
|
12
|
-
table.hashes.each do |h|
|
13
|
-
|
14
|
-
name = h["name"] || h["login"]
|
15
|
-
|
16
|
-
next if name=="guest"
|
17
|
-
|
18
|
-
u = UserHash.new.merge!(h)
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
u.set(:pw,h["secret"] || h["password"] || name+"@12345678")
|
23
|
-
|
24
|
-
before_user_create(u,h)
|
25
|
-
users[name] = u
|
26
|
-
after_user_create(u,h)
|
27
|
-
end
|
28
|
-
end
|
@@ -1,274 +0,0 @@
|
|
1
|
-
# the step definitions are executed in an instance of world so
|
2
|
-
# we can add helper methods for use in the step definitions.
|
3
|
-
|
4
|
-
class RestWorld
|
5
|
-
# the entry point to the rack application to be tested
|
6
|
-
def self.app
|
7
|
-
@_app ||= Rack::Builder.parse_file('config.ru')
|
8
|
-
end
|
9
|
-
|
10
|
-
# a dummy request to sent to the server
|
11
|
-
def self.request
|
12
|
-
@_req ||= Rack::MockRequest.new(app)
|
13
|
-
end
|
14
|
-
|
15
|
-
# a class to represent a response from the server
|
16
|
-
class Response
|
17
|
-
def initialize(resp)
|
18
|
-
@resp = resp
|
19
|
-
content_type = @resp.headers['Content-Type'] || @resp.headers['content-type']
|
20
|
-
if content_type == 'application/json'
|
21
|
-
begin
|
22
|
-
@h = MultiJson.load(body) || {}
|
23
|
-
rescue Exception => e
|
24
|
-
log 'INVALID RESPONSE BODY=>' + body
|
25
|
-
raise
|
26
|
-
end
|
27
|
-
else
|
28
|
-
@h = { 'html' => body }
|
29
|
-
end
|
30
|
-
|
31
|
-
# get_ids_from_hash
|
32
|
-
end
|
33
|
-
|
34
|
-
def [](k)
|
35
|
-
@h[k]
|
36
|
-
end
|
37
|
-
|
38
|
-
def body
|
39
|
-
[@resp.body].flatten.join('')
|
40
|
-
end
|
41
|
-
|
42
|
-
def data
|
43
|
-
@h['data']
|
44
|
-
end
|
45
|
-
|
46
|
-
def error
|
47
|
-
@h['error']
|
48
|
-
end
|
49
|
-
|
50
|
-
def status
|
51
|
-
@resp.status.to_i
|
52
|
-
end
|
53
|
-
|
54
|
-
def header
|
55
|
-
@resp.headers || {}
|
56
|
-
end
|
57
|
-
|
58
|
-
def content_type
|
59
|
-
header['Content-Type']
|
60
|
-
end
|
61
|
-
|
62
|
-
def inspect
|
63
|
-
@resp.inspect
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
# store cookies for each user here
|
68
|
-
def cookies
|
69
|
-
@_cookies ||= {}
|
70
|
-
@_cookies[@_current_user] ||= []
|
71
|
-
end
|
72
|
-
|
73
|
-
# store current user information here
|
74
|
-
def users
|
75
|
-
@_users ||= {}
|
76
|
-
end
|
77
|
-
|
78
|
-
# store current user tokens here
|
79
|
-
def tokens
|
80
|
-
@_tokens ||= {}
|
81
|
-
end
|
82
|
-
|
83
|
-
# store general information here
|
84
|
-
def store
|
85
|
-
@_store ||= {}
|
86
|
-
end
|
87
|
-
|
88
|
-
def valid_response
|
89
|
-
@_response || raise('no valid response from service')
|
90
|
-
end
|
91
|
-
|
92
|
-
def valid_data
|
93
|
-
@_response && @_response.data || raise("no valid data returned from service:#{@_response.error}")
|
94
|
-
end
|
95
|
-
|
96
|
-
def explain
|
97
|
-
log "request ==> #{@_verb} #{@_request}"
|
98
|
-
log "cookies ==> #{cookies.join('; ')}" if cookies.length > 0
|
99
|
-
log "body ==> #{@_body}" if @_body
|
100
|
-
log "response ==> #{@_response.inspect}"
|
101
|
-
end
|
102
|
-
|
103
|
-
def before_parse_path(path); end
|
104
|
-
|
105
|
-
def before_parse_body(json); end
|
106
|
-
|
107
|
-
def parse_path(path)
|
108
|
-
path = path.dup
|
109
|
-
|
110
|
-
before_parse_path(path)
|
111
|
-
|
112
|
-
path = path.gsub /\/(@[a-z0-9_]+)/ do |str|
|
113
|
-
str = str[2..-1]
|
114
|
-
id = store[str]
|
115
|
-
raise ":#{str} has not been stored" unless id
|
116
|
-
if id[0] == '/'
|
117
|
-
"#{id}"
|
118
|
-
else
|
119
|
-
"/#{id}"
|
120
|
-
end
|
121
|
-
end
|
122
|
-
# and the query part
|
123
|
-
path.gsub /\=(@[a-z0-9_]+)/ do |str|
|
124
|
-
str = str[2..-1]
|
125
|
-
id = store[str]
|
126
|
-
raise ":#{str} has not been stored" unless id
|
127
|
-
|
128
|
-
"=#{id}"
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
def parse_json(json)
|
133
|
-
# replace original format
|
134
|
-
json = json.gsub /:@([a-z0-9_]+)/ do |str|
|
135
|
-
str = str[2..-1]
|
136
|
-
id = store[str]
|
137
|
-
raise ":#{str} has not been stored" unless id
|
138
|
-
|
139
|
-
if id.is_a?(String)
|
140
|
-
":\"#{id}\""
|
141
|
-
else
|
142
|
-
":#{id}"
|
143
|
-
end
|
144
|
-
end
|
145
|
-
|
146
|
-
# replace alternative format
|
147
|
-
json = json.gsub /#\{([a-z0-9_]+)\}/ do |str|
|
148
|
-
str = str[2..-2]
|
149
|
-
id = store[str]
|
150
|
-
raise ":#{str} has not been stored" unless id
|
151
|
-
|
152
|
-
if id.is_a?(String)
|
153
|
-
"\"#{id}\""
|
154
|
-
else
|
155
|
-
"#{id}"
|
156
|
-
end
|
157
|
-
end
|
158
|
-
end
|
159
|
-
|
160
|
-
def parse_body(json)
|
161
|
-
json = json.dup
|
162
|
-
before_parse_body(json)
|
163
|
-
parse_json(json)
|
164
|
-
end
|
165
|
-
|
166
|
-
def add_token_to_request
|
167
|
-
return if @_request.include?('token=')
|
168
|
-
if @_user
|
169
|
-
token = @_user.get(:token) || "token12345678-#{@_user.get(:login)}"
|
170
|
-
@_request = if @_request.include?('?')
|
171
|
-
@_request + "&token=#{token}"
|
172
|
-
else
|
173
|
-
@_request + "?token=#{token}"
|
174
|
-
end
|
175
|
-
end
|
176
|
-
end
|
177
|
-
|
178
|
-
def add_token_to_path(path,token)
|
179
|
-
return unless token
|
180
|
-
if path.include?('?')
|
181
|
-
path + "&token=" + token
|
182
|
-
else
|
183
|
-
path + "?token=" + token
|
184
|
-
end
|
185
|
-
end
|
186
|
-
|
187
|
-
def rack_request_headers
|
188
|
-
env = {}
|
189
|
-
env['REMOTE_ADDR'] = '10.0.0.1'
|
190
|
-
env['HTTP_COOKIE'] = cookies.join('; ')
|
191
|
-
env["HTTP_AUTHORIZATION"] = @_auth if @_auth
|
192
|
-
env["HTTP_HOST"] = @_host if @_host
|
193
|
-
env
|
194
|
-
end
|
195
|
-
|
196
|
-
def request
|
197
|
-
RestWorld.request
|
198
|
-
end
|
199
|
-
|
200
|
-
def set_host(name)
|
201
|
-
@_host = name
|
202
|
-
end
|
203
|
-
|
204
|
-
def set_auth_headers(user)
|
205
|
-
raise "invalid user name:#{user}" unless u = users[user]
|
206
|
-
pw = u.get(:pw)
|
207
|
-
raise "iuser name:#{user} has no password!" unless pw
|
208
|
-
str = user + ":" + pw
|
209
|
-
str = Base64.encode64(str)
|
210
|
-
str = "Basic " + str
|
211
|
-
#Rack::MockRequest::DEFAULT_ENV["HTTP_AUTHORIZATION"] = str
|
212
|
-
@_auth = str
|
213
|
-
end
|
214
|
-
|
215
|
-
# save the response for furthur enquiries and store any cookies.
|
216
|
-
def handle_response(raw_response)
|
217
|
-
@_auth = nil
|
218
|
-
@_response = Response.new(raw_response)
|
219
|
-
# add cookies to the cookie jar.
|
220
|
-
#unless @_current_user=="guest"
|
221
|
-
if cookie = @_response.header["Set-Cookie"] || @_response.header["set-cookie"]
|
222
|
-
parts = cookie.split(';')
|
223
|
-
cookies << parts[0].strip
|
224
|
-
end
|
225
|
-
#end
|
226
|
-
end
|
227
|
-
|
228
|
-
def parse_user(user)
|
229
|
-
user.split(' ')[-1]
|
230
|
-
end
|
231
|
-
|
232
|
-
def send_request(verb, username, path, json)
|
233
|
-
username = parse_user(username)
|
234
|
-
@_verb = verb
|
235
|
-
@_body = json && parse_body(json)
|
236
|
-
@_request = parse_path(path)
|
237
|
-
@_current_user = username
|
238
|
-
|
239
|
-
if username == 'guest'
|
240
|
-
@_user = nil
|
241
|
-
else
|
242
|
-
@_user = users[username]
|
243
|
-
raise "user :#{username} has not been initialized" unless @_user
|
244
|
-
|
245
|
-
pw = @_user.get(:pw)
|
246
|
-
add_token_to_request
|
247
|
-
set_auth_headers(username)
|
248
|
-
end
|
249
|
-
case verb
|
250
|
-
when 'GET'
|
251
|
-
handle_response(request.get(@_request, rack_request_headers))
|
252
|
-
when 'OPTIONS'
|
253
|
-
handle_response(request.options(@_request, rack_request_headers))
|
254
|
-
when 'POST'
|
255
|
-
handle_response(request.post(@_request, rack_request_headers.merge(input: @_body)))
|
256
|
-
when 'PUT'
|
257
|
-
handle_response(request.put(@_request, rack_request_headers.merge(input: @_body)))
|
258
|
-
when 'DELETE'
|
259
|
-
handle_response(request.delete(@_request, rack_request_headers.merge(input: @_body)))
|
260
|
-
end
|
261
|
-
end
|
262
|
-
|
263
|
-
# a hook that is called before creating a user
|
264
|
-
def before_user_create(user, hash); end
|
265
|
-
|
266
|
-
# a hook that is called before creating a user
|
267
|
-
def after_user_create(user, hash); end
|
268
|
-
end
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
World do
|
273
|
-
RestWorld.new
|
274
|
-
end
|
data/lib/blix/rest/cucumber.rb
DELETED