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')
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



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

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



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

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



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

def after_user_create(user, hash); end

#before_parse_body(json) ⇒ Object



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

def before_parse_body(json); end

#before_parse_path(path) ⇒ Object



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

def before_parse_path(path); end

#before_user_create(user, hash) ⇒ Object

a hook that is called before creating a user



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

def before_user_create(user, hash); end

#cookiesObject

store cookies for each user here



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

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

#explainObject



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

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

#handle_response(raw_response) ⇒ Object

save the response for furthur enquiries and store any cookies.



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

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"] || @_response.header["set-cookie"]
    parts = cookie.split(';')
    cookies << parts[0].strip
  end
  #end
end

#parse_body(json) ⇒ Object



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

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

#parse_json(json) ⇒ Object



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

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



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

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



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

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

#rack_request_headersObject



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

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



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

def request
  RestWorld.request
end

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



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

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



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

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



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

def set_host(name)
  @_host = name
end

#storeObject

store general information here



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

def store
  @_store ||= {}
end

#tokensObject

store current user tokens here



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

def tokens
  @_tokens ||= {}
end

#usersObject

store current user information here



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

def users
  @_users ||= {}
end

#valid_dataObject



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

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

#valid_responseObject



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

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