better_rest 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eec73eed2d71fd24e5ef1a6380ba9b96c0c3cc86
4
- data.tar.gz: d31fae1445848a5ceac23ea96fecdcd53f707291
3
+ metadata.gz: e990eb58751881bf19708d2bb4a521c954a07850
4
+ data.tar.gz: baee501acb9b69804a6b903601e1dc1d96c6e24e
5
5
  SHA512:
6
- metadata.gz: 4de8bf79426d9fcd5fde7879d08fbd9c9fcd324431e0dad4b9d1e6d691069693aea5123c4b90d008000e04f8985eccf1b7901377076d1b1123ec5e44f1aefef2
7
- data.tar.gz: 70c11e61852ad401b97063cac090335b6a7aa075c88347e8d9423ea5c269616dc290080a5da19112547d8f4715f2d91f1389b2fab38d95dffaf76622530153e1
6
+ metadata.gz: 6840e1c632dd3d70ee43979c40bffdf63585787a016b0fdba43a5d555433e16f0a261df189226d01632bb7e577068823e22e8ecbdcf3adb3ea61340bbdbdf822
7
+ data.tar.gz: 361612d6422112d2fd219ec56397f859fc1a35e37f3d161b5917ee2ee7ed1112d8b116a523abed26ae86e0be97f2fbcf2ef967364ed85349ea0106c0f0334565
data/.gitignore CHANGED
@@ -1,5 +1,6 @@
1
1
  .DS_Store
2
- .gem
3
2
  requests
4
3
  logs
5
4
  tmp
5
+ cookiejar
6
+ *.gem
data/README.md CHANGED
@@ -8,9 +8,16 @@ BetteR emphasises both a clean and easy to use interface, as well as allowing us
8
8
 
9
9
  ![Screenshot](http://at1as.github.io/github_repo_assets/better-rest-client.jpg)
10
10
 
11
+ ### Features
12
+
13
+ * Import collections from POSTMAN
14
+ * Save and load Requests
15
+ * Attach files to requests
16
+ * Send parallel requests
17
+
11
18
  ### Usage
12
19
 
13
- The easiest way to use BetteR is to install it using the [Ruby Gem](http://rubygems.org/gems/better_rest) (note that this usually lags a few commits behind):
20
+ The easiest way to use BetteR is to install it using the [Ruby Gem](http://rubygems.org/gems/better_rest) (note that I usually keep the gem several commits behind the github repo):
14
21
  ```bash
15
22
  $ gem install better_rest
16
23
  ```
@@ -18,7 +25,7 @@ Or download the repository here for the latest version and launch via:
18
25
  ```bash
19
26
  $ ./bin/better_rest
20
27
  ```
21
- To try BetteR, without installing the Gem, it's also hosted on [Heroku](http://better-rest.herokuapp.com/). Note that when run on Heroku, some of functionality won't work due to lack of access to the filesystem (saving logs, etc.). Also note that this is likely to be from an older commit.
28
+ To try BetteR, without installing the Gem, it's also hosted on [Heroku](http://better-rest.herokuapp.com/). Note that when run on Heroku, the build is likely to be from a much older commit.
22
29
 
23
30
  ### Dependencies
24
31
 
data/better_rest.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "better_rest"
3
- s.version = "0.2.1"
3
+ s.version = "0.2.2"
4
4
  s.licenses = ['MIT']
5
5
  s.summary = "REST Test Client"
6
6
  s.date = "2014-12-13"
data/lib/better_rest.rb CHANGED
@@ -14,6 +14,32 @@ set :views, File.expand_path('../../views', __FILE__)
14
14
  helpers do
15
15
  include Rack::Utils
16
16
  alias_method :h, :escape_html
17
+
18
+ def parse_cookies(cookies)
19
+ cookie_hash = {}
20
+ cookies.each do |c|
21
+ key, value = c.split('; ').first.split('=', 2)
22
+ cookie_hash[key] = value
23
+ end
24
+ cookie_hash.to_json
25
+ end
26
+
27
+ def stringify_cookies(cookies)
28
+ JSON.parse(cookies).map { |key, value| "#{key}=#{value}" }.join('; ')
29
+ end
30
+
31
+ def parse_postman_headers(headers)
32
+ header_hash = {}
33
+ header_list = headers.strip.split(': ')
34
+ keys = header_list.select.each_with_index { |str, i| i.even? }
35
+ values = header_list.select.each_with_index { |str, i| i.odd? }
36
+
37
+ keys.each_with_index do |key, index|
38
+ header_hash[key] = values[index]
39
+ end
40
+ header_hash
41
+ end
42
+
17
43
  end
18
44
 
19
45
  configure do
@@ -32,7 +58,8 @@ get '/?' do
32
58
  @requests = ["GET","POST","PUT","DELETE","HEAD","OPTIONS","PATCH"]
33
59
  @times = ["1", "2", "5", "10"]
34
60
  @header_hash = {"" => ""}
35
- @follow, @verbose, @ssl, @log_requests = true, true, false, false
61
+ @follow, @cookies, @verbose = [true] * 3
62
+ @ssl, @log_requests = [false] * 2
36
63
  @timeout_interval = 2
37
64
 
38
65
  erb :index
@@ -53,6 +80,7 @@ post '/request' do
53
80
  @verbose = @request_body['verbose'] rescue false
54
81
  @ssl = @request_body['ssl_ver'] rescue false
55
82
  @log_requests = @request_body['logging'] rescue false
83
+ @cookies = @request_body['cookies'] rescue true
56
84
  @timeout_interval = Integer(@request_body['timeout']) rescue 1
57
85
  @request_body['headers']['User-Agent'] = BETTER_SIGNATURE unless @request_body['headers']['User-Agent']
58
86
 
@@ -77,6 +105,15 @@ post '/request' do
77
105
  request.options[:body] = { file: File.open('tmp/' + @request_body['file'], 'r') }
78
106
  end
79
107
 
108
+ # Attach cookie to header
109
+ if @cookies && @request_body['headers']['Cookie'].nil?
110
+ if File.exists? "cookiejar"
111
+ cookie = File.read("cookiejar")
112
+ end
113
+ request.options[:headers]['Cookie'] = stringify_cookies(cookie) unless cookie.empty?
114
+ end
115
+
116
+
80
117
  # Remove unused fields from request
81
118
  request.options.delete(:body) if request.options[:body].empty?
82
119
  if @request_body['user'].empty? || @request_body['password'].empty?
@@ -97,6 +134,12 @@ post '/request' do
97
134
  end
98
135
  end
99
136
 
137
+ # Write cookie to file
138
+ if response.headers_hash['set-cookie']
139
+ cookies = parse_cookies(response.headers_hash['set-cookie'])
140
+ File.open('cookiejar', 'w') { |file| file.write(cookies) }
141
+ end
142
+
100
143
  # Response parameters return to View
101
144
  @response_body = {}
102
145
  request.options[:url] = request.url
@@ -110,7 +153,14 @@ post '/request' do
110
153
 
111
154
  @response_body['return_code'] = response.code
112
155
  @response_body['return_time'] = response.time
113
- @response_body['return_body'] = response.body.force_encoding('ISO-8859-1')
156
+
157
+ # Return body with correct encoding
158
+ if response.headers_hash['Content-Type'] == "application/json"
159
+ @response_body['return_body'] = JSON.pretty_generate(JSON.parse(response.body)).force_encoding('ISO-8859-1')
160
+ else
161
+ @response_body['return_body'] = response.body.force_encoding('ISO-8859-1')
162
+ end
163
+
114
164
  @response_body['return_headers'] = response.response_headers
115
165
 
116
166
  @response_body.to_json
@@ -168,6 +218,7 @@ get '/savedrequests/:collection/:request' do
168
218
  if File.exists? "requests/#{params[:collection]}.json"
169
219
  collection = JSON.parse File.read("requests/#{params[:collection]}.json")
170
220
  request = collection[params[:request]]
221
+
171
222
  return request.to_json
172
223
  else
173
224
  return 404
@@ -178,8 +229,10 @@ end
178
229
 
179
230
  # Delete Request Collection
180
231
  delete '/collections/:collection' do
181
- if File.exists? "requests/#{params[:collection]}.json"
182
- File.delete("requests/#{params[:collection]}.json")
232
+ collection = "requests/#{params[:collection]}.json"
233
+
234
+ if File.exists? collection
235
+ File.delete(collection)
183
236
  else
184
237
  return 404
185
238
  end
@@ -189,11 +242,13 @@ end
189
242
 
190
243
  # Delete Request from Collection
191
244
  delete '/collections/:collection/:request' do
192
- if File.exists? "requests/#{params[:collection]}.json"
193
- stored_collection = JSON.parse File.read("requests/#{params[:collection]}.json")
245
+ collection = "requests/#{params[:collection]}.json"
246
+
247
+ if File.exists? collection
248
+ stored_collection = JSON.parse File.read(collection)
194
249
  stored_collection.delete(params[:request])
195
250
 
196
- File.open("requests/#{params[:collection]}.json", "w") do |f|
251
+ File.open(collection, "w") do |f|
197
252
  f.write(stored_collection.to_json)
198
253
  end
199
254
  else
@@ -223,8 +278,10 @@ end
223
278
 
224
279
  # Delete log
225
280
  delete '/logs/:log' do
226
- if File.exists? "logs/#{params[:log]}.log"
227
- File.delete("logs/#{params[:log]}.log")
281
+ log = "logs/#{params[:log]}.log"
282
+
283
+ if File.exists? log
284
+ File.delete(log)
228
285
  else
229
286
  return 404
230
287
  end
@@ -250,19 +307,21 @@ end
250
307
  # Import from POSTMAN Collection
251
308
  post '/import' do
252
309
 
310
+ file = 'tmp/postman_import.json'
311
+
253
312
  # Clear tmp directory before writing file
254
313
  FileUtils.rm_rf(Dir.glob('tmp/*'))
255
314
 
256
315
  # Save File
257
316
  unless request.body.nil?
258
- File.open('tmp/postman_import.json', 'w') do |f|
317
+ File.open(file, 'w') do |f|
259
318
  f.write(params[:file][:tempfile].read)
260
319
  end
261
320
  end
262
321
 
263
322
  # Read File
264
- if File.exists? "tmp/postman_import.json"
265
- stored_collection = JSON.parse File.read("tmp/postman_import.json") rescue return 500
323
+ if File.exists? file
324
+ stored_collection = JSON.parse File.read(file) rescue return 500
266
325
 
267
326
  # Data dump of multiple collections
268
327
  if stored_collection['collections']
@@ -283,7 +342,7 @@ post '/import' do
283
342
  request_details['collection'] = collection['name']
284
343
  request_details['url'] = request['url']
285
344
  request_details['request'] = request['method']
286
- request_details['headers'] = request['headers']
345
+ request_details['headers'] = parse_postman_headers(request['headers'])
287
346
  request_details['payload'] = request['data']
288
347
  request_details['quantity'] = 1
289
348
 
data/public/main.css CHANGED
@@ -17,6 +17,9 @@ h2 {
17
17
  h2:hover{
18
18
  color:black;
19
19
  }
20
+ hr{
21
+ margin-bottom:10px;
22
+ }
20
23
  input{
21
24
  border-radius: 2px;
22
25
  background-color:#FEFEFE;
@@ -239,6 +242,7 @@ ul li:hover ul {
239
242
  width: -moz-calc(100% - 40px);
240
243
  width: -webkit-calc(100% - 40px);
241
244
  width: calc(100% - 40px);
245
+ overflow: hidden;
242
246
  }
243
247
  .rm-collection{
244
248
  line-height:inherit;
@@ -311,14 +315,13 @@ ul li:hover ul {
311
315
  #auth-span{
312
316
  display:inline-block;
313
317
  white-space:nowrap;
314
- margin-top:4px;
315
318
  margin-bottom:4px;
316
319
  margin-left:15px;
317
320
  }
318
321
  #response-status{
319
322
  margin-left:15px;
320
323
  margin-top: 10px;
321
- margin-bottom: 12px;
324
+ margin-bottom: 10px;
322
325
  }
323
326
  #response-filters{
324
327
  margin-left:0px;
@@ -327,7 +330,7 @@ ul li:hover ul {
327
330
  margin-top:4px;
328
331
  }
329
332
  #reqdata{
330
- width:250px;
333
+ width:400px;
331
334
  max-height:300px;
332
335
  overflow:scroll;
333
336
  }
@@ -360,6 +363,9 @@ ul li:hover ul {
360
363
  overflow:scroll;
361
364
  padding-bottom:15px;
362
365
  }
366
+ #logs-modal{
367
+ width:400px;
368
+ }
363
369
 
364
370
 
365
371
  /* Tooltips */
data/views/index.erb CHANGED
@@ -194,6 +194,11 @@
194
194
  <input type="checkbox" name="followlocation" id="followlocation" class="checkbox" style="float:right"
195
195
  <% if @follow %>checked<% end %>>
196
196
  </div>
197
+ <div>
198
+ <label class="form-label conf-label" for="cookies">Send Cookies</label>
199
+ <input type="checkbox" name="cookies" id="cookies" class="checkbox" style="float:right"
200
+ <% if @cookies %>checked<% end %>>
201
+ </div>
197
202
  <div>
198
203
  <label class="form-label conf-label" for="verbose">Verbose Response</label>
199
204
  <input type="checkbox" name="verbose" id="verbose" class="checkbox" style="float:right"
@@ -371,6 +376,7 @@
371
376
 
372
377
  /* SETTINGS */
373
378
  body.redirect = document.getElementById('followlocation').checked;
379
+ body.cookies = document.getElementById('cookies').checked;
374
380
  body.verbose = document.getElementById('verbose').checked;
375
381
  body.ssl_ver = document.getElementById('ssl_verifypeer').checked;
376
382
  body.logging = document.getElementById('logging').checked;
@@ -518,9 +524,10 @@
518
524
  client.send();
519
525
 
520
526
  if (client.status == 200) {
521
- modalToggle('load-modal');
527
+ modalHideAll();
522
528
  clearFile();
523
529
  populateView(JSON.parse(client.responseText));
530
+ warn();
524
531
  } else {
525
532
  console.log('Unknown Error: GET ' + url + ' failed');
526
533
  }
@@ -565,6 +572,9 @@
565
572
  if (fielddata.redirect) {
566
573
  document.getElementById('followlocation').value = fielddata.redirect;
567
574
  }
575
+ if (fielddata.cookies) {
576
+ document.getElementById('cookies').value = fielddata.cookies;
577
+ }
568
578
  if (fielddata.verbose) {
569
579
  document.getElementById('verbose').value = fielddata.verbose;
570
580
  }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: better_rest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Willems