blix-rest-cucumber 0.1.0
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 +7 -0
- data/LICENSE +25 -0
- data/README.md +74 -0
- data/lib/blix/rest/cucumber/hooks.rb +5 -0
- data/lib/blix/rest/cucumber/request_steps.rb +229 -0
- data/lib/blix/rest/cucumber/resource_steps.rb +28 -0
- data/lib/blix/rest/cucumber/world.rb +276 -0
- data/lib/blix/rest/cucumber.rb +8 -0
- metadata +80 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: fdb6d0885fdf71c075002990b46dd4d441993c0c1247246a3bb0cb4685dc8d5d
|
4
|
+
data.tar.gz: c9e5b74dec7dc77e9cf6b331d52bb2727d8aa1a1445b9caa0d3948d2aafcb041
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 54935eb63992297fb9747f8fc4a4a5135fcdfa21ae4e9ef0b1d698d3c81ab307b8fa1d3b8553805722ba26261f425677b53b25a2d84e5f4794c78483e468c7b7
|
7
|
+
data.tar.gz: f3de2494d7f77ed6bf4ca1ca5ad9a1f02f9e65dc4a911a9501034c37f5ec88a318e6aeae2ae886d664a1812ce54a125011a360ee61c1b2959cf456f9bde7e4a3
|
data/LICENSE
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017-2020 Clive Andrews
|
4
|
+
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person
|
7
|
+
obtaining a copy of this software and associated documentation
|
8
|
+
files (the "Software"), to deal in the Software without
|
9
|
+
restriction, including without limitation the rights to use,
|
10
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11
|
+
copies of the Software, and to permit persons to whom the
|
12
|
+
Software is furnished to do so, subject to the following
|
13
|
+
conditions:
|
14
|
+
|
15
|
+
The above copyright notice and this permission notice shall be
|
16
|
+
included in all copies or substantial portions of the Software.
|
17
|
+
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
19
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
20
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
21
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
22
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
23
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
24
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
25
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
## INSTALLATION
|
2
|
+
|
3
|
+
gem install blix-rest-cucumber
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
|
8
|
+
## Testing a Service with cucumber
|
9
|
+
|
10
|
+
|
11
|
+
in features/support/setup.rb
|
12
|
+
|
13
|
+
require 'blix/rest/cucumber'
|
14
|
+
|
15
|
+
and setup your database connections etc
|
16
|
+
|
17
|
+
|
18
|
+
in features/support/hooks.rb
|
19
|
+
|
20
|
+
reset your database
|
21
|
+
|
22
|
+
|
23
|
+
|
24
|
+
now you can use the following in scenarios ........
|
25
|
+
|
26
|
+
Given user guest gets "/info"
|
27
|
+
|
28
|
+
Given the following users exist:
|
29
|
+
| name | level |
|
30
|
+
| anon | guest |
|
31
|
+
| bob | user |
|
32
|
+
| mary | provider |
|
33
|
+
| paul | user |
|
34
|
+
| admin | admin |
|
35
|
+
|
36
|
+
Given user mary posts "/worlds" with {"name":"narnia"} [..or gets/puts/deletes]
|
37
|
+
Then store the "id" as "world_id"
|
38
|
+
|
39
|
+
Given user bob posts "/worlds/:world_id" with {"the_world_id"::world_id }
|
40
|
+
|
41
|
+
Then the status should be 200
|
42
|
+
Then the data type should be "r_type"
|
43
|
+
Then the data length should be 3
|
44
|
+
Then there should be an error
|
45
|
+
Then the error message should include "unique"
|
46
|
+
Then the data "name" should == "bob"
|
47
|
+
Then the data should include "name"
|
48
|
+
|
49
|
+
And explain
|
50
|
+
|
51
|
+
|
52
|
+
NOTE : if you need to set up your database with users then you can use the following hook ..
|
53
|
+
|
54
|
+
in features/support/world.rb .........
|
55
|
+
|
56
|
+
|
57
|
+
require 'blix/rest/cucumber'
|
58
|
+
|
59
|
+
class RestWorld
|
60
|
+
|
61
|
+
# add a hook to create the user in the database -
|
62
|
+
#
|
63
|
+
def before_user_create(user,hash)
|
64
|
+
name = hash["name"]
|
65
|
+
u = MyUser.new
|
66
|
+
u.set(:user_wid, name)
|
67
|
+
u.set(:name,name)
|
68
|
+
u.set(:is_super,true) if hash["level"] == "super"
|
69
|
+
u.save
|
70
|
+
store["myuser_#{name}_id"] = u.id.to_s
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
now you can also use eg `@myuser_foo_id` within a request path/json.
|
@@ -0,0 +1,229 @@
|
|
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
|
+
log "data is not an array"
|
109
|
+
1
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
Then(/^the data length should equal (\d+)$/) do |len|
|
114
|
+
if valid_response.data.kind_of? Array
|
115
|
+
expect(valid_response.data.length).to eq len.to_i
|
116
|
+
else
|
117
|
+
expect(valid_response.data.kind_of?(Array) ).to eq true
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
Then(/^the data "(.*?)" should == (.*?)$/) do |field,val|
|
122
|
+
check_data_value(field,val)
|
123
|
+
end
|
124
|
+
|
125
|
+
Then(/^the data "(.*?)" should be (.*?)$/) do |field,val|
|
126
|
+
check_data_value(field,val)
|
127
|
+
end
|
128
|
+
|
129
|
+
|
130
|
+
def check_data_value(field,val)
|
131
|
+
if valid_data.kind_of? Array
|
132
|
+
data = valid_data[0]
|
133
|
+
else
|
134
|
+
data = valid_data
|
135
|
+
end
|
136
|
+
v = data[field].to_s
|
137
|
+
if val =~ %r{^@([^@]*)$}
|
138
|
+
expect(v).to eq store[$1].to_s
|
139
|
+
elsif val =~ %r{^(::)?[A-Z][A-z_a-z:]*$}
|
140
|
+
expect(v).to eq Module.const_get(val).to_s
|
141
|
+
elsif val =~ %r{^['"](.*)['"]$}
|
142
|
+
expect(v).to eq $1
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
Then(/^the data "(.*?)" should equal ["'](.*?)["']$/) do |field,val|
|
147
|
+
# if valid_response.data.kind_of? Array
|
148
|
+
# data = valid_response.data[0]
|
149
|
+
# else
|
150
|
+
# data = valid_response.data
|
151
|
+
# end
|
152
|
+
# check = String.new(data[field].to_s)
|
153
|
+
# check.force_encoding('UTF-8')
|
154
|
+
# expect(check).to eq val
|
155
|
+
check_data_value(field,val)
|
156
|
+
end
|
157
|
+
|
158
|
+
Then(/^the data "(.*?)" should include ["'](.*?)["']$/) do |field,val|
|
159
|
+
if valid_response.data.kind_of? Array
|
160
|
+
data = valid_response.data[0]
|
161
|
+
else
|
162
|
+
data = valid_response.data
|
163
|
+
end
|
164
|
+
check = String.new(data[field].to_s)
|
165
|
+
check.force_encoding('UTF-8')
|
166
|
+
expect(check).to include val
|
167
|
+
end
|
168
|
+
|
169
|
+
Then(/^the data ["'](.*?)["'] should == nil$/) do |field|
|
170
|
+
if valid_response.data.kind_of? Array
|
171
|
+
data = valid_response.data[0]
|
172
|
+
else
|
173
|
+
data = valid_response.data
|
174
|
+
end
|
175
|
+
expect(data[field]).to be nil
|
176
|
+
end
|
177
|
+
|
178
|
+
Then(/^the data ["'](.*?)["'] should equal nil$/) do |field|
|
179
|
+
if valid_response.data.kind_of? Array
|
180
|
+
data = valid_response.data[0]
|
181
|
+
else
|
182
|
+
data =valid_response.data
|
183
|
+
end
|
184
|
+
expect(data[field]).to be nil
|
185
|
+
end
|
186
|
+
|
187
|
+
Then(/^the body should eq ["'](.*?)["']$/) do |val|
|
188
|
+
val.gsub!("\\n","\n")
|
189
|
+
expect(valid_response.body).to eq val
|
190
|
+
end
|
191
|
+
|
192
|
+
|
193
|
+
|
194
|
+
Then(/^the data should( not)? include ["'](.*?)["']$/) do |state, field|
|
195
|
+
if valid_response.data.kind_of? Array
|
196
|
+
data = valid_response.data[0]
|
197
|
+
else
|
198
|
+
data = valid_response.data
|
199
|
+
end
|
200
|
+
if state == " not"
|
201
|
+
expect(data.key?(field)).to eq false
|
202
|
+
else
|
203
|
+
expect(data.key?(field)).to eq true
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
Then(/^store the ["'](.*?)["'] as ["'](.*?)["']$/) do |name,key|
|
210
|
+
if valid_response.data.kind_of?(Array)
|
211
|
+
data = valid_response.data[0]
|
212
|
+
else
|
213
|
+
data = valid_response.data
|
214
|
+
end
|
215
|
+
if data.kind_of?(Hash) && data.key?(name)
|
216
|
+
store[key] = data[name]
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
Given(/^save the "([^"]*)"$/) do |name|
|
221
|
+
if valid_response.data.kind_of?(Array)
|
222
|
+
data = valid_response.data[0]
|
223
|
+
else
|
224
|
+
data = valid_response.data
|
225
|
+
end
|
226
|
+
if data.kind_of?(Hash) && data.key?(name)
|
227
|
+
store[name] = data[name]
|
228
|
+
end
|
229
|
+
end
|
@@ -0,0 +1,28 @@
|
|
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
|
@@ -0,0 +1,276 @@
|
|
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 ||= begin
|
8
|
+
[Rack::Builder.parse_file('config.ru')].flatten.first
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
# a dummy request to sent to the server
|
13
|
+
def self.request
|
14
|
+
@_req ||= Rack::MockRequest.new(app)
|
15
|
+
end
|
16
|
+
|
17
|
+
# a class to represent a response from the server
|
18
|
+
class Response
|
19
|
+
def initialize(resp)
|
20
|
+
@resp = resp
|
21
|
+
content_type = @resp.headers['Content-Type'] || @resp.headers['content-type']
|
22
|
+
if content_type == 'application/json'
|
23
|
+
begin
|
24
|
+
@h = MultiJson.load(body) || {}
|
25
|
+
rescue Exception => e
|
26
|
+
log 'INVALID RESPONSE BODY=>' + body
|
27
|
+
raise
|
28
|
+
end
|
29
|
+
else
|
30
|
+
@h = { 'html' => body }
|
31
|
+
end
|
32
|
+
|
33
|
+
# get_ids_from_hash
|
34
|
+
end
|
35
|
+
|
36
|
+
def [](k)
|
37
|
+
@h[k]
|
38
|
+
end
|
39
|
+
|
40
|
+
def body
|
41
|
+
[@resp.body].flatten.join('')
|
42
|
+
end
|
43
|
+
|
44
|
+
def data
|
45
|
+
@h['data']
|
46
|
+
end
|
47
|
+
|
48
|
+
def error
|
49
|
+
@h['error']
|
50
|
+
end
|
51
|
+
|
52
|
+
def status
|
53
|
+
@resp.status.to_i
|
54
|
+
end
|
55
|
+
|
56
|
+
def header
|
57
|
+
@resp.headers || {}
|
58
|
+
end
|
59
|
+
|
60
|
+
def content_type
|
61
|
+
header['Content-Type']
|
62
|
+
end
|
63
|
+
|
64
|
+
def inspect
|
65
|
+
@resp.inspect
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# store cookies for each user here
|
70
|
+
def cookies
|
71
|
+
@_cookies ||= {}
|
72
|
+
@_cookies[@_current_user] ||= []
|
73
|
+
end
|
74
|
+
|
75
|
+
# store current user information here
|
76
|
+
def users
|
77
|
+
@_users ||= {}
|
78
|
+
end
|
79
|
+
|
80
|
+
# store current user tokens here
|
81
|
+
def tokens
|
82
|
+
@_tokens ||= {}
|
83
|
+
end
|
84
|
+
|
85
|
+
# store general information here
|
86
|
+
def store
|
87
|
+
@_store ||= {}
|
88
|
+
end
|
89
|
+
|
90
|
+
def valid_response
|
91
|
+
@_response || raise('no valid response from service')
|
92
|
+
end
|
93
|
+
|
94
|
+
def valid_data
|
95
|
+
@_response && @_response.data || raise("no valid data returned from service:#{@_response.error}")
|
96
|
+
end
|
97
|
+
|
98
|
+
def explain
|
99
|
+
log "request ==> #{@_verb} #{@_request}"
|
100
|
+
log "cookies ==> #{cookies.join('; ')}" if cookies.length > 0
|
101
|
+
log "body ==> #{@_body}" if @_body
|
102
|
+
log "response ==> #{@_response.inspect}"
|
103
|
+
end
|
104
|
+
|
105
|
+
def before_parse_path(path); end
|
106
|
+
|
107
|
+
def before_parse_body(json); end
|
108
|
+
|
109
|
+
def parse_path(path)
|
110
|
+
path = path.dup
|
111
|
+
|
112
|
+
before_parse_path(path)
|
113
|
+
|
114
|
+
path = path.gsub /\/(@[a-z0-9_]+)/ do |str|
|
115
|
+
str = str[2..-1]
|
116
|
+
id = store[str]
|
117
|
+
raise ":#{str} has not been stored" unless id
|
118
|
+
if id[0] == '/'
|
119
|
+
"#{id}"
|
120
|
+
else
|
121
|
+
"/#{id}"
|
122
|
+
end
|
123
|
+
end
|
124
|
+
# and the query part
|
125
|
+
path.gsub /\=(@[a-z0-9_]+)/ do |str|
|
126
|
+
str = str[2..-1]
|
127
|
+
id = store[str]
|
128
|
+
raise ":#{str} has not been stored" unless id
|
129
|
+
|
130
|
+
"=#{id}"
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
def parse_json(json)
|
135
|
+
# replace original format
|
136
|
+
json = json.gsub /:@([a-z0-9_]+)/ do |str|
|
137
|
+
str = str[2..-1]
|
138
|
+
id = store[str]
|
139
|
+
raise ":#{str} has not been stored" unless id
|
140
|
+
|
141
|
+
if id.is_a?(String)
|
142
|
+
":\"#{id}\""
|
143
|
+
else
|
144
|
+
":#{id}"
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
# replace alternative format
|
149
|
+
json = json.gsub /#\{([a-z0-9_]+)\}/ do |str|
|
150
|
+
str = str[2..-2]
|
151
|
+
id = store[str]
|
152
|
+
raise ":#{str} has not been stored" unless id
|
153
|
+
|
154
|
+
if id.is_a?(String)
|
155
|
+
"\"#{id}\""
|
156
|
+
else
|
157
|
+
"#{id}"
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
def parse_body(json)
|
163
|
+
json = json.dup
|
164
|
+
before_parse_body(json)
|
165
|
+
parse_json(json)
|
166
|
+
end
|
167
|
+
|
168
|
+
def add_token_to_request
|
169
|
+
return if @_request.include?('token=')
|
170
|
+
if @_user
|
171
|
+
token = @_user.get(:token) || "token12345678-#{@_user.get(:login)}"
|
172
|
+
@_request = if @_request.include?('?')
|
173
|
+
@_request + "&token=#{token}"
|
174
|
+
else
|
175
|
+
@_request + "?token=#{token}"
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
def add_token_to_path(path,token)
|
181
|
+
return unless token
|
182
|
+
if path.include?('?')
|
183
|
+
path + "&token=" + token
|
184
|
+
else
|
185
|
+
path + "?token=" + token
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
def rack_request_headers
|
190
|
+
env = {}
|
191
|
+
env['REMOTE_ADDR'] = '10.0.0.1'
|
192
|
+
env['HTTP_COOKIE'] = cookies.join('; ')
|
193
|
+
env["HTTP_AUTHORIZATION"] = @_auth if @_auth
|
194
|
+
env["HTTP_HOST"] = @_host if @_host
|
195
|
+
env
|
196
|
+
end
|
197
|
+
|
198
|
+
def request
|
199
|
+
RestWorld.request
|
200
|
+
end
|
201
|
+
|
202
|
+
def set_host(name)
|
203
|
+
@_host = name
|
204
|
+
end
|
205
|
+
|
206
|
+
def set_auth_headers(user)
|
207
|
+
raise "invalid user name:#{user}" unless u = users[user]
|
208
|
+
pw = u.get(:pw)
|
209
|
+
raise "iuser name:#{user} has no password!" unless pw
|
210
|
+
str = user + ":" + pw
|
211
|
+
str = Base64.encode64(str)
|
212
|
+
str = "Basic " + str
|
213
|
+
#Rack::MockRequest::DEFAULT_ENV["HTTP_AUTHORIZATION"] = str
|
214
|
+
@_auth = str
|
215
|
+
end
|
216
|
+
|
217
|
+
# save the response for furthur enquiries and store any cookies.
|
218
|
+
def handle_response(raw_response)
|
219
|
+
@_auth = nil
|
220
|
+
@_response = Response.new(raw_response)
|
221
|
+
# add cookies to the cookie jar.
|
222
|
+
#unless @_current_user=="guest"
|
223
|
+
if cookie = @_response.header["Set-Cookie"] || @_response.header["set-cookie"]
|
224
|
+
parts = cookie.split(';')
|
225
|
+
cookies << parts[0].strip
|
226
|
+
end
|
227
|
+
#end
|
228
|
+
end
|
229
|
+
|
230
|
+
def parse_user(user)
|
231
|
+
user.split(' ')[-1]
|
232
|
+
end
|
233
|
+
|
234
|
+
def send_request(verb, username, path, json)
|
235
|
+
username = parse_user(username)
|
236
|
+
@_verb = verb
|
237
|
+
@_body = json && parse_body(json)
|
238
|
+
@_request = parse_path(path)
|
239
|
+
@_current_user = username
|
240
|
+
|
241
|
+
if username == 'guest'
|
242
|
+
@_user = nil
|
243
|
+
else
|
244
|
+
@_user = users[username]
|
245
|
+
raise "user :#{username} has not been initialized" unless @_user
|
246
|
+
|
247
|
+
pw = @_user.get(:pw)
|
248
|
+
add_token_to_request
|
249
|
+
set_auth_headers(username)
|
250
|
+
end
|
251
|
+
case verb
|
252
|
+
when 'GET'
|
253
|
+
handle_response(request.get(@_request, rack_request_headers))
|
254
|
+
when 'OPTIONS'
|
255
|
+
handle_response(request.options(@_request, rack_request_headers))
|
256
|
+
when 'POST'
|
257
|
+
handle_response(request.post(@_request, rack_request_headers.merge(input: @_body)))
|
258
|
+
when 'PUT'
|
259
|
+
handle_response(request.put(@_request, rack_request_headers.merge(input: @_body)))
|
260
|
+
when 'DELETE'
|
261
|
+
handle_response(request.delete(@_request, rack_request_headers.merge(input: @_body)))
|
262
|
+
end
|
263
|
+
end
|
264
|
+
|
265
|
+
# a hook that is called before creating a user
|
266
|
+
def before_user_create(user, hash); end
|
267
|
+
|
268
|
+
# a hook that is called before creating a user
|
269
|
+
def after_user_create(user, hash); end
|
270
|
+
end
|
271
|
+
|
272
|
+
|
273
|
+
|
274
|
+
World do
|
275
|
+
RestWorld.new
|
276
|
+
end
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: blix-rest-cucumber
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Clive Andrews
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2025-04-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: blix-rest
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.10.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.10.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: cucumber
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 8.0.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 8.0.0
|
41
|
+
description: Cucumber testing support for blix/rest
|
42
|
+
email:
|
43
|
+
- gems@realitybites.eu
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files:
|
47
|
+
- README.md
|
48
|
+
- LICENSE
|
49
|
+
files:
|
50
|
+
- LICENSE
|
51
|
+
- README.md
|
52
|
+
- lib/blix/rest/cucumber.rb
|
53
|
+
- lib/blix/rest/cucumber/hooks.rb
|
54
|
+
- lib/blix/rest/cucumber/request_steps.rb
|
55
|
+
- lib/blix/rest/cucumber/resource_steps.rb
|
56
|
+
- lib/blix/rest/cucumber/world.rb
|
57
|
+
homepage: https://github.com/realbite/blix-rest
|
58
|
+
licenses:
|
59
|
+
- MIT
|
60
|
+
metadata: {}
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
requirements: []
|
76
|
+
rubygems_version: 3.0.9
|
77
|
+
signing_key:
|
78
|
+
specification_version: 4
|
79
|
+
summary: Test Blix/Rest Server with Cucumber
|
80
|
+
test_files: []
|