Class: RestWorld

Inherits:
Object
  • Object
show all
Defined in:
lib/blix/rest/cucumber/world.rb

Overview

the step definitions are executed in an instance of world so we can add helper methods for use in the step definitions.

Defined Under Namespace

Classes: Response

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.appObject

the entry point to the rack application to be tested



6
7
8
# File 'lib/blix/rest/cucumber/world.rb', line 6

def self.app
  @_app ||= Rack::Builder.parse_file('config.ru').first
end

.requestObject

a dummy request to sent to the server



11
12
13
# File 'lib/blix/rest/cucumber/world.rb', line 11

def self.request
  @_req ||= Rack::MockRequest.new(app)
end

Instance Method Details

#add_token_to_path(path, token) ⇒ Object



177
178
179
180
181
182
183
184
# File 'lib/blix/rest/cucumber/world.rb', line 177

def add_token_to_path(path,token)
  return unless token
  if path.include?('?')
    path + "&token=" + token
  else
    path + "?token=" + token
  end
end

#add_token_to_requestObject



165
166
167
168
169
170
171
172
173
174
175
# File 'lib/blix/rest/cucumber/world.rb', line 165

def add_token_to_request
  return if @_request.include?('token=')
  if @_user
    token = @_user.get(:token) || "token12345678-#{@_user.get(:login)}"
    @_request = if @_request.include?('?')
                  @_request + "&token=#{token}"
                else
                  @_request + "?token=#{token}"
                end
  end
end

#after_user_create(user, hash) ⇒ Object

a hook that is called before creating a user



266
# File 'lib/blix/rest/cucumber/world.rb', line 266

def after_user_create(user, hash); end

#before_parse_body(json) ⇒ Object



104
# File 'lib/blix/rest/cucumber/world.rb', line 104

def before_parse_body(json); end

#before_parse_path(path) ⇒ Object



102
# File 'lib/blix/rest/cucumber/world.rb', line 102

def before_parse_path(path); end

#before_user_create(user, hash) ⇒ Object

a hook that is called before creating a user



263
# File 'lib/blix/rest/cucumber/world.rb', line 263

def before_user_create(user, hash); end

#cookiesObject

store cookies for each user here



67
68
69
70
# File 'lib/blix/rest/cucumber/world.rb', line 67

def cookies
  @_cookies ||= {}
  @_cookies[@_current_user] ||= []
end

#explainObject



95
96
97
98
99
100
# File 'lib/blix/rest/cucumber/world.rb', line 95

def explain
  puts "request ==> #{@_verb} #{@_request}"
  puts "cookies ==> #{cookies.join('; ')}" if cookies.length > 0
  puts "body ==> #{@_body}" if @_body
  puts "response ==> #{@_response.inspect}"
end

#handle_response(raw_response) ⇒ Object

save the response for furthur enquiries and store any cookies.



215
216
217
218
219
220
221
222
223
224
225
# File 'lib/blix/rest/cucumber/world.rb', line 215

def handle_response(raw_response)
  @_auth = nil
  @_response = Response.new(raw_response)
  # add cookies to the cookie jar.
  #unless @_current_user=="guest"
  if cookie = @_response.header["Set-Cookie"]
    parts = cookie.split(';')
    cookies << parts[0].strip
  end
  #end
end

#parse_body(json) ⇒ Object



159
160
161
162
163
# File 'lib/blix/rest/cucumber/world.rb', line 159

def parse_body(json)
  json = json.dup
  before_parse_body(json)
  parse_json(json)
end

#parse_json(json) ⇒ Object



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
# File 'lib/blix/rest/cucumber/world.rb', line 131

def parse_json(json)
  # replace original format
  json = json.gsub /:@([a-z0-9_]+)/ do |str|
    str = str[2..-1]
    id = store[str]
    raise ":#{str} has not been stored" unless id

    if id.is_a?(String)
      ":\"#{id}\""
    else
      ":#{id}"
    end
  end

  # replace alternative format
  json = json.gsub /#\{([a-z0-9_]+)\}/ do |str|
    str = str[2..-2]
    id = store[str]
    raise ":#{str} has not been stored" unless id

    if id.is_a?(String)
      "\"#{id}\""
    else
      "#{id}"
    end
  end
end

#parse_path(path) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/blix/rest/cucumber/world.rb', line 106

def parse_path(path)
  path = path.dup

  before_parse_path(path)

  path = path.gsub /\/(@[a-z0-9_]+)/ do |str|
    str = str[2..-1]
    id  = store[str]
    raise ":#{str} has not been stored" unless id
    if id[0] == '/'
      "#{id}"
    else
      "/#{id}"
    end
  end
  # and the query part
  path.gsub /\=(@[a-z0-9_]+)/ do |str|
    str = str[2..-1]
    id  = store[str]
    raise ":#{str} has not been stored" unless id

    "=#{id}"
  end
end

#parse_user(user) ⇒ Object



227
228
229
# File 'lib/blix/rest/cucumber/world.rb', line 227

def parse_user(user)
  user.split(' ')[-1]
end

#rack_request_headersObject



186
187
188
189
190
191
192
193
# File 'lib/blix/rest/cucumber/world.rb', line 186

def rack_request_headers
  env = {}
  env['REMOTE_ADDR'] = '10.0.0.1'
  env['HTTP_COOKIE'] = cookies.join('; ')
  env["HTTP_AUTHORIZATION"] = @_auth if @_auth
  env["HTTP_HOST"] = @_host if @_host
  env
end

#requestObject



195
196
197
# File 'lib/blix/rest/cucumber/world.rb', line 195

def request
  RestWorld.request
end

#send_request(verb, username, path, json) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/blix/rest/cucumber/world.rb', line 231

def send_request(verb, username, path, json)
  username = parse_user(username)
  @_verb = verb
  @_body = json && parse_body(json)
  @_request = parse_path(path)
  @_current_user  = username

  if username == 'guest'
    @_user = nil
  else
    @_user = users[username]
    raise "user :#{username} has not been initialized" unless @_user

    pw = @_user.get(:pw)
    add_token_to_request
    set_auth_headers(username)
  end
  case verb
  when 'GET'
    handle_response(request.get(@_request, rack_request_headers))
  when 'OPTIONS'
    handle_response(request.options(@_request, rack_request_headers))
  when 'POST'
    handle_response(request.post(@_request, rack_request_headers.merge(input: @_body)))
  when 'PUT'
    handle_response(request.put(@_request, rack_request_headers.merge(input: @_body)))
  when 'DELETE'
    handle_response(request.delete(@_request, rack_request_headers.merge(input: @_body)))
  end
end

#set_auth_headers(user) ⇒ Object



203
204
205
206
207
208
209
210
211
212
# File 'lib/blix/rest/cucumber/world.rb', line 203

def set_auth_headers(user)
  raise "invalid user name:#{user}" unless u = users[user]
  pw = u.get(:pw)
  raise "iuser name:#{user} has no password!" unless pw
  str = user + ":" + pw
  str = Base64.encode64(str)
  str = "Basic " + str
  #Rack::MockRequest::DEFAULT_ENV["HTTP_AUTHORIZATION"] = str
  @_auth  = str
end

#set_host(name) ⇒ Object



199
200
201
# File 'lib/blix/rest/cucumber/world.rb', line 199

def set_host(name)
  @_host = name
end

#storeObject

store general information here



83
84
85
# File 'lib/blix/rest/cucumber/world.rb', line 83

def store
  @_store ||= {}
end

#tokensObject

store current user tokens here



78
79
80
# File 'lib/blix/rest/cucumber/world.rb', line 78

def tokens
  @_tokens ||= {}
end

#usersObject

store current user information here



73
74
75
# File 'lib/blix/rest/cucumber/world.rb', line 73

def users
  @_users ||= {}
end

#valid_dataObject



91
92
93
# File 'lib/blix/rest/cucumber/world.rb', line 91

def valid_data
  @_response && @_response.data || raise("no valid data returned from service:#{@_response.error}")
end

#valid_responseObject



87
88
89
# File 'lib/blix/rest/cucumber/world.rb', line 87

def valid_response
  @_response || raise('no valid response from service')
end