scenario_server 0.0.7 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 428e1ab2442b942ab63071ea8598148e79574df5
4
- data.tar.gz: 469192547f52f20755ec2b64dec1e6c487572970
3
+ metadata.gz: ba5c753c602276fababe1e5ed34531f804a07ef1
4
+ data.tar.gz: ff6edf100eabe7a24b9cb0283d101f7962f5dcde
5
5
  SHA512:
6
- metadata.gz: 34269dfd4153e5932e32ce393d58008e7ee9ff7143bec9cd09273e0ff49b28987a2a676010445e661d92ed805087bc387863a877c94bca7ed38f490c3566f80a
7
- data.tar.gz: f87c3e8a0c778da0a4316f284845fe867c0e144dd7b3102e0f6cd340aa734fe82e99e01e259cf6e5092c7d088675cee4998d1188c8d49df69dd328b496f87f9b
6
+ metadata.gz: c33253655a852d7962bd8f074a20f3e486ad120bed2a76db3fa1ca92ab648ac038a75fa5350ff06905f6e2a5c1c2cd354e7bca8b40f5f61fb1e271fee1db4cd9
7
+ data.tar.gz: f5e6fc554effad0c652af2258f7b7e5da1501f3631fc1967eadb32dabe675c918ce4731829f78464cb9459ff535e9b3c763ff746c907020d96d8cb303131b9af
data/WIP.txt ADDED
@@ -0,0 +1,2 @@
1
+ . scenario server should run with postgres too
2
+ . add mechanism so the response fixture can be refreshed.
@@ -12,9 +12,7 @@ class ScenarioServer < Sinatra::Base
12
12
  register Sinatra::ConfigFile
13
13
  use Rack::MethodOverride
14
14
 
15
- #config_file File.expand_path('~/.scenarios') + '/config.yml'
16
-
17
- configure :production do
15
+ configure :production do
18
16
  puts 'Production Environment'
19
17
  config_file File.expand_path('~/.scenarios') + '/config.yml'
20
18
  set :db_file , File.dirname(File.expand_path(__FILE__)) + '/../data/scenario_db.sqlite3'
@@ -50,8 +48,6 @@ class ScenarioServer < Sinatra::Base
50
48
  end
51
49
 
52
50
  before do
53
-
54
- # configure database
55
51
  if defined?(settings.localdbfile)
56
52
  puts "using db: "+settings.localdbfile
57
53
  options = {:db_file=>settings.localdbfile}
@@ -91,8 +87,8 @@ class ScenarioServer < Sinatra::Base
91
87
 
92
88
  selected_scenario = self.scenario_db.get_scenario_for_id(params[:scenario_id])
93
89
  routes = self.scenario_db.get_routes_for_scenario(params[:scenario_id])
94
-
95
- if (request.env['HTTP_ACCEPT'] && request.env['HTTP_ACCEPT'].include?('text/html'))
90
+
91
+ if (request.env['HTTP_ACCEPT'] && request.env['HTTP_ACCEPT'].include?('text/html'))
96
92
  if params.has_key?('error')
97
93
  erb :'scenario', :locals => {'scenario'=>selected_scenario, 'routes'=>routes, 'error'=>params['error']}
98
94
  else
@@ -135,7 +131,11 @@ class ScenarioServer < Sinatra::Base
135
131
 
136
132
  if valid_fixture
137
133
  self.scenario_db.add_route_for_scenario(params['route_type'],
138
- params['path'], params['fixture'], params[:scenario_id])
134
+ params['path'],
135
+ params['status_code'],
136
+ params['headers'],
137
+ params['fixture'],
138
+ params[:scenario_id])
139
139
  redirect('/scenarios/'+params[:scenario_id])
140
140
  else
141
141
  redirect('/scenarios/'+params[:scenario_id]+'?error=invalid%20json')
@@ -144,7 +144,11 @@ class ScenarioServer < Sinatra::Base
144
144
  else
145
145
  json_body = JSON.parse(request.body.read)
146
146
  route_id = self.scenario_db.add_route_for_scenario(json_body['route_type'],
147
- json_body['path'], json_body['fixture'], params[:scenario_id])
147
+ json_body['path'],
148
+ json_body['status_code'],
149
+ json_body['headers'],
150
+ json_body['fixture'],
151
+ params[:scenario_id])
148
152
  content = {'url'=>'/scenarios/'+params[:scenario_id]+'/routes/'+route_id.to_s}.to_json
149
153
  [200, content]
150
154
  end
@@ -176,7 +180,6 @@ class ScenarioServer < Sinatra::Base
176
180
  get '/scenarios/:scenario_id/routes/:route_id' do
177
181
  route = self.scenario_db.get_route(params[:route_id])
178
182
 
179
-
180
183
  if (request.env['HTTP_ACCEPT'] && request.env['HTTP_ACCEPT'].include?('text/html'))
181
184
  erb :'route', :locals => { :route => route }
182
185
  else
@@ -209,62 +212,95 @@ class ScenarioServer < Sinatra::Base
209
212
  end
210
213
 
211
214
  # route path calls
212
- get "/*" do
213
- content_type 'application/json'
215
+ get '/*' do
216
+ #TODO: Validate scene from the list of scenarios
217
+ ordered_scenarios = self.scenario_db.get_ordered_scenarios
218
+
219
+
220
+ #TODO: dry up the implementation
221
+ #TODO: add ignore scene header
222
+ current_scenario = settings.scenario
223
+ if (request.env['HTTP_SCENE'])
224
+ current_scenario = request.env['HTTP_SCENE']
225
+ end
214
226
 
215
227
  route_type = request.request_method.upcase
216
228
  path = request.path.downcase
217
229
 
218
- fixture = get_fixture(route_type, path,settings.scenario)
219
- if fixture.nil?
230
+ status_code, header, fixture = get_fixture(route_type, path, current_scenario)
231
+ if header.nil? || header == ""
232
+ header = {'Content-Type' => 'application/json'}
233
+ else
234
+ header = eval(header)
235
+ end
236
+
237
+ if fixture.nil?
220
238
  404
221
239
  else
222
- fixture
240
+ status status_code
241
+ headers header
242
+ body fixture
223
243
  end
224
244
  end
225
245
 
226
- post "/*" do
246
+ post '/*' do
227
247
  content_type 'application/json'
228
248
 
229
249
  route_type = request.request_method.upcase
230
250
  path = request.path.downcase
231
251
 
232
- fixture = get_fixture(route_type, path,settings.scenario)
252
+ current_scenario = settings.scenario
253
+ if (request.env['HTTP_SCENE'])
254
+ current_scenario = request.env['HTTP_SCENE']
255
+ end
256
+
257
+ status_code, header, fixture = get_fixture(route_type, path, current_scenario)
233
258
  if fixture.nil?
234
259
  404
235
260
  else
236
- fixture
261
+ [status_code, fixture]
237
262
  end
238
263
  end
239
264
 
240
- put "/*" do
265
+ put '/*' do
241
266
  content_type 'application/json'
242
267
 
243
268
  route_type = request.request_method.upcase
244
269
  path = request.path.downcase
245
270
 
246
- fixture = get_fixture(route_type, path,settings.scenario)
271
+ current_scenario = settings.scenario
272
+ if (request.env['HTTP_SCENE'])
273
+ current_scenario = request.env['HTTP_SCENE']
274
+ end
275
+
276
+ status_code, header, fixture = get_fixture(route_type, path, current_scenario)
247
277
  if fixture.nil?
248
278
  404
249
279
  else
250
- fixture
280
+ [status_code, fixture]
251
281
  end
282
+
283
+
252
284
  end
253
285
 
254
- delete "/*" do
286
+ delete '/*' do
255
287
  content_type 'application/json'
256
288
 
257
289
  route_type = request.request_method.upcase
258
290
  path = request.path.downcase
259
291
 
260
- fixture = get_fixture(route_type, path,settings.scenario)
292
+ current_scenario = settings.scenario
293
+ if (request.env['HTTP_SCENE'])
294
+ current_scenario = request.env['HTTP_SCENE']
295
+ end
296
+
297
+ status_code, header, fixture = get_fixture(route_type, path, current_scenario)
261
298
  if fixture.nil?
262
299
  404
263
300
  else
264
- fixture
301
+ [status_code, fixture]
265
302
  end
266
303
  end
267
304
 
268
-
269
305
  end
270
306
 
@@ -20,11 +20,14 @@ def default?(name)
20
20
  end
21
21
 
22
22
  def get_fixture(route_type, path, scenario_name)
23
- fixture = self.scenario_db.get_fixture_from_routes(route_type, path, scenario_name)
24
-
23
+ status_code, header, fixture = self.scenario_db.get_fixture_from_routes(route_type, path, scenario_name)
24
+
25
25
  if fixture.nil? and default?(scenario_name)
26
- fixture = self.scenario_db.get_fixture_from_routes(route_type, path, 'default')
26
+ status_code, header, fixture = self.scenario_db.get_fixture_from_routes(route_type, path, 'default')
27
27
  end
28
28
 
29
- fixture
29
+ if status_code == nil
30
+ status_code = 200
31
+ end
32
+ return status_code, header, fixture
30
33
  end
@@ -0,0 +1,41 @@
1
+ body {
2
+ font-family: Verdana;
3
+ color: navy; }
4
+
5
+ h1 {
6
+ color: black;
7
+ }
8
+
9
+ table {
10
+ table-layout: fixed;
11
+ width: 100%;
12
+ border-collapse: collapse;
13
+ border: 1px solid black;
14
+ }
15
+
16
+ th {
17
+ height: 50px;
18
+ border: 1px solid black;
19
+ }
20
+ td {
21
+ height: 40px;
22
+ text-align: center;
23
+ vertical-align: center;
24
+ border: 1px solid black;
25
+ }
26
+
27
+ input[type="text"] {
28
+ width: 50%;
29
+ display: block;
30
+ font-size:18px;
31
+ margin-bottom: 10px;
32
+ }
33
+
34
+ textarea {
35
+ width: 50%;
36
+ display: block;
37
+ font-size:18px;
38
+ margin-bottom: 10px;
39
+ }
40
+
41
+
@@ -1,5 +1,7 @@
1
1
  <html xmlns="http://www.w3.org/1999/html">
2
-
2
+ <head>
3
+ <link rel="stylesheet" type="text/css" href="/css/style.css">
4
+ </head>
3
5
  <body>
4
6
  <h1>Scenarios</h1>
5
7
  <table>
@@ -1,4 +1,7 @@
1
1
  <html>
2
+ <head>
3
+ <link rel="stylesheet" type="text/css" href="/css/style.css">
4
+ </head>
2
5
  <body>
3
6
  <h1><%= route[:path] %></h1>
4
7
  <table>
@@ -9,6 +12,8 @@
9
12
  <th>Scenario ID</th>
10
13
  <th>Route Type</th>
11
14
  <th>Path</th>
15
+ <th>Status Code</th>
16
+ <th>Headers</th>
12
17
  <th>Fixture</th>
13
18
  <th>Created At</th>
14
19
  <th>Updated At</th>
@@ -20,6 +25,8 @@
20
25
  <td><%=route[:scenario_id]%></td>
21
26
  <td><%=route[:route_type]%></td>
22
27
  <td><%=route[:path]%></td>
28
+ <td><%=route[:status_code]%></td>
29
+ <td><%=route[:headers]%></td>
23
30
  <td><%=route[:fixture]%></td>
24
31
  <td><%=route[:created_at]%></td>
25
32
  <td><%=route[:updated_at]%></td>
@@ -1,23 +1,34 @@
1
1
  <html>
2
+ <head>
3
+ <link rel="stylesheet" type="text/css" href="/css/style.css">
4
+ </head>
5
+
2
6
  <body>
3
7
  <a href="/scenarios">Home</a>
4
8
  <br>
5
9
 
6
10
  <%= error %>
7
11
  <h2><%= scenario[:name] %></h2>
8
- <table>
9
- <thead>
10
- <tr>
11
- <th>Request Type</th>
12
- <th>Path</th>
13
- <th>Fixture</th>
14
- </tr>
15
- </thead>
16
- <tbody>
12
+ <% if routes.count > 0 %>
13
+ <table>
14
+ <thead>
15
+ <tr>
16
+ <th>Request Type</th>
17
+ <th>Path</th>
18
+ <th>Status Code</th>
19
+ <th>Headers</th>
20
+ <th>Fixture</th>
21
+ <th>Action</th>
22
+ </tr>
23
+ </thead>
24
+ <tbody>
25
+ <% end %>
17
26
  <% routes.each do |route| %>
18
27
  <tr>
19
28
  <td><%= route[:route_type] %></td>
20
29
  <td><a href="/scenarios/<%= scenario[:id] %>/routes/<%= route[:id]%>"><%= route[:path] %></a></td>
30
+ <td><%= route[:status_code] %></td>
31
+ <td><%= route[:headers] %></td>
21
32
  <td><%= route[:fixture] %></td>
22
33
  <td>
23
34
  <form action="/scenarios/<%= scenario[:id] %>/routes/<%= route[:id]%>" method="post" style="display:inline;">
@@ -29,25 +40,33 @@
29
40
  <% end %>
30
41
  </tbody>
31
42
  </table>
43
+ <hr>
44
+ <br>
45
+ <br>
32
46
  <p>
33
47
  <form action="/scenarios/<%= scenario[:id] %>/routes/new" method="post" id="routeform">
34
48
  <p>
35
49
 
36
- <label>Request Type</label>
37
- <select name="route_type">
50
+ <label>Request Type</label> <br>
51
+ <select name="route_type" width="100" style="width: 100px">
38
52
  <option value="GET">GET</option>
39
53
  <option value="POST">POST</option>
40
54
  <option value="DELETE">DELETE</option>
41
55
  <option value="PUT">PUT</option>
42
56
  </select>
43
- <br>
44
- <label>Path</label>
57
+ <br>
58
+ <label>Path</label>
45
59
  <input type="text" name="path"> <br>
46
- <label>Fixture</label>
60
+ <label>Status Code</label>
61
+ <input type="text" name="status_code"> <br>
62
+ <label>Headers</label><br>
63
+ <textarea name="headers" rows="5" ></textarea>
64
+ </p>
65
+ <label>Fixture</label><br>
47
66
  <textarea name="fixture" rows="10" ></textarea>
48
67
  </p>
49
68
  <p>
50
- <button type="submit">Submit</button>
69
+ <button type="submit"> Submit </button>
51
70
  </p>
52
71
  </form>
53
72
  </p>
@@ -46,10 +46,10 @@ class ScenarioDB
46
46
  end
47
47
  end
48
48
 
49
- if !options[:add_route].nil? && options[:add_route].length == 4
49
+ if !options[:add_route].nil? && options[:add_route].length == 6
50
50
  configure_database
51
51
 
52
- add_route_for_scenario(options[:add_route][0], options[:add_route][1], options[:add_route][2], options[:add_route][3])
52
+ add_route_for_scenario(options[:add_route][0], options[:add_route][1], options[:add_route][2], options[:add_route][3], options[:add_route][4], options[:add_route][5])
53
53
  if self.routes.count > 0
54
54
  Sequel::PrettyTable.print(self.routes)
55
55
  else
@@ -210,6 +210,8 @@ class ScenarioDB
210
210
  foreign_key :scenario_id, :scenarios, :on_delete => :cascade, :on_update => :cascade
211
211
  String :route_type
212
212
  String :path
213
+ Integer :status_code
214
+ String :headers
213
215
  String :fixture
214
216
  DateTime :created_at
215
217
  DateTime :updated_at
@@ -217,10 +219,10 @@ class ScenarioDB
217
219
  end
218
220
  end
219
221
 
220
- def add_route_for_scenario(route_type, path, fixture, scenario_id)
221
- now = DateTime.now
222
+ def add_route_for_scenario(route_type, path, status_code, headers, fixture, scenario_id)
223
+ now = DateTime.now
222
224
  if self.routes.where(:scenario_id => scenario_id, :route_type=> route_type, :path=> path).to_a.count == 0
223
- self.routes.insert(:scenario_id => scenario_id, :route_type=>route_type, :path => path, :fixture => fixture, :created_at => now, :updated_at => now)
225
+ self.routes.insert(:scenario_id => scenario_id, :route_type=> route_type, :path => path, :status_code => status_code, :headers => headers, :fixture => fixture, :created_at => now, :updated_at => now)
224
226
  end
225
227
  # puts "<scenario_id: #{scenario_id} #{}route_type} #Path: #{path} fixture: #{fixture}>"
226
228
  end
@@ -230,17 +232,14 @@ class ScenarioDB
230
232
  end
231
233
 
232
234
  def get_routes_for_scenario(scenario_id)
233
- return self.routes.filter(:scenario_id=>scenario_id).select(:id, :scenario_id,:route_type,:path,:fixture).to_a
235
+ return self.routes.filter(:scenario_id=>scenario_id).select(:id, :scenario_id, :route_type, :path, :status_code, :headers, :fixture).to_a
234
236
  end
235
237
 
236
238
  def get_fixture_from_routes(route_type, path, scenario_name)
237
-
238
239
  self.routes.left_outer_join(:scenarios, :id=>:scenario_id).where(:route_type=>route_type,
239
240
  :path=>path,
240
- :name=>scenario_name).map(:fixture).first
241
-
241
+ :name=>scenario_name).map([:status_code, :headers, :fixture]).first
242
242
 
243
- #TODO instead of returning first fixture, add checks to avoid duplicate insertions
244
243
  end
245
244
 
246
245
  def get_route(route_id)
@@ -1,3 +1,3 @@
1
1
  module Scenarios
2
- VERSION = "0.0.7"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scenario_server
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vaibhav Bhatia
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-24 00:00:00.000000000 Z
11
+ date: 2014-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sinatra
@@ -178,6 +178,7 @@ files:
178
178
  - LICENSE.txt
179
179
  - README.md
180
180
  - Rakefile
181
+ - WIP.txt
181
182
  - bin/scenario_service
182
183
  - bin/scenarios
183
184
  - bin/scenariosdb
@@ -187,6 +188,7 @@ files:
187
188
  - lib/scenarios.rb
188
189
  - lib/scenarios/application/app.rb
189
190
  - lib/scenarios/application/app_helpers.rb
191
+ - lib/scenarios/application/public/css/style.css
190
192
  - lib/scenarios/application/views/index.erb
191
193
  - lib/scenarios/application/views/route.erb
192
194
  - lib/scenarios/application/views/scenario.erb