pliny 0.26.0 → 0.26.1

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: fc435bb8dd0c08d30696e53650d96a8ed0c38cb9
4
- data.tar.gz: 01707d0efaf29bc8e2133887d936d58a10a4a388
3
+ metadata.gz: e4adaaedbebc3ae5352103ddadbf1f85ee0ea368
4
+ data.tar.gz: d0c837f014f54d02eb7cc28903fdec073502564b
5
5
  SHA512:
6
- metadata.gz: 593f3a3e15d3f1094a78ad0fad317e86500f596dd8d7577bfe406b4a3cc1b0a813efab88630dd4ebb30e6168a30d4cf202a06df77663b24c8114c960e8884218
7
- data.tar.gz: 6300a8c749f94601fc7bec51baeffc83aa1687b51c458984c39484a0ab910a2fece3d61fc2518f6a08943efbe141b083047cca050bdde8d0080bf136b741c49d
6
+ metadata.gz: f50ff132e89a39ca8d28c9e873dd3f85a9d4fb2210fda66eaa7634af7a4a4647db0e5e158554add566dbdea69400c70332317153e45aa945315466a9b64ea58a
7
+ data.tar.gz: 88082d4d62b2b4b6e59011dfa22420f942242990fe32256be729f91400b5b5ab6d92035353c710053a82977db99dfc5929eccc7f0eca4bea1fe9488e05b21ac8
@@ -8,14 +8,23 @@ module Pliny::Helpers
8
8
 
9
9
  def parse_body_params
10
10
  if request.media_type == "application/json"
11
- p = indifferent_params(MultiJson.decode(request.body.read))
11
+ p = load_params(MultiJson.decode(request.body.read))
12
12
  request.body.rewind
13
13
  p
14
14
  elsif request.form_data?
15
- indifferent_params(request.POST)
15
+ load_params(request.POST)
16
16
  else
17
17
  {}
18
18
  end
19
19
  end
20
+
21
+ def load_params(data)
22
+ # Sinatra 1.x only supports the method. Sinatra 2.x only supports the class
23
+ if defined?(Sinatra::IndifferentHash)
24
+ Sinatra::IndifferentHash[data]
25
+ else
26
+ indifferent_params(data)
27
+ end
28
+ end
20
29
  end
21
30
  end
@@ -8,6 +8,12 @@ module Pliny::Middleware
8
8
  EXPOSE_HEADERS =
9
9
  %w( Cache-Control Content-Language Content-Type Expires Last-Modified Pragma ).freeze
10
10
 
11
+ @@additional_headers = []
12
+
13
+ def self.add_additional_header(header)
14
+ @@additional_headers << header
15
+ end
16
+
11
17
  def initialize(app)
12
18
  @app = app
13
19
  end
@@ -19,7 +25,7 @@ module Pliny::Middleware
19
25
  else
20
26
  status, headers, response = @app.call(env)
21
27
 
22
- # regualar CORS request: append CORS headers to response
28
+ # regular CORS request: append CORS headers to response
23
29
  if cors_request?(env)
24
30
  headers.merge!(cors_headers(env))
25
31
  end
@@ -32,11 +38,15 @@ module Pliny::Middleware
32
38
  env.has_key?("HTTP_ORIGIN")
33
39
  end
34
40
 
41
+ def allow_headers
42
+ ALLOW_HEADERS + @@additional_headers
43
+ end
44
+
35
45
  def cors_headers(env)
36
46
  {
37
47
  'Access-Control-Allow-Origin' => env["HTTP_ORIGIN"],
38
48
  'Access-Control-Allow-Methods' => ALLOW_METHODS.join(', '),
39
- 'Access-Control-Allow-Headers' => ALLOW_HEADERS.join(', '),
49
+ 'Access-Control-Allow-Headers' => allow_headers.join(', '),
40
50
  'Access-Control-Allow-Credentials' => "true",
41
51
  'Access-Control-Max-Age' => "1728000",
42
52
  'Access-Control-Expose-Headers' => EXPOSE_HEADERS.join(', ')
@@ -1,3 +1,3 @@
1
1
  module Pliny
2
- VERSION = "0.26.0"
2
+ VERSION = "0.26.1"
3
3
  end
@@ -0,0 +1,28 @@
1
+ require "spec_helper"
2
+
3
+ describe Pliny::Helpers::Params do
4
+
5
+ def app
6
+ Sinatra.new do
7
+ helpers Pliny::Helpers::Params
8
+ post "/" do
9
+ body_params.to_json
10
+ end
11
+ end
12
+ end
13
+
14
+ it "loads json params" do
15
+ post "/", {hello: "world"}.to_json, {'CONTENT_TYPE' => 'application/json'}
16
+ assert_equal "{\"hello\":\"world\"}", last_response.body
17
+ end
18
+
19
+ it "loads form data params" do
20
+ post "/", {hello: "world"}
21
+ assert_equal "{\"hello\":\"world\"}", last_response.body
22
+ end
23
+
24
+ it "loads from an unknown content type" do
25
+ post "/", "<hello>world</hello>", {'CONTENT_TYPE' => 'application/xml'}
26
+ assert_equal "{}", last_response.body
27
+ end
28
+ end
@@ -39,4 +39,15 @@ describe Pliny::Middleware::CORS do
39
39
  assert_equal "http://localhost",
40
40
  last_response.headers["Access-Control-Allow-Origin"]
41
41
  end
42
+
43
+ it "allows additional headers to be added to every response" do
44
+ Pliny::Middleware::CORS.add_additional_header("X-Origin")
45
+
46
+ header "Origin", "http://localhost"
47
+ get "/"
48
+ assert_equal 200, last_response.status
49
+ assert_equal "hi", last_response.body
50
+
51
+ assert last_response.headers["Access-Control-Allow-Headers"].include?("X-Origin")
52
+ end
42
53
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pliny
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.26.0
4
+ version: 0.26.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandur Leach
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-09-28 00:00:00.000000000 Z
12
+ date: 2017-12-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -488,6 +488,7 @@ files:
488
488
  - spec/error_reporters_spec.rb
489
489
  - spec/errors_spec.rb
490
490
  - spec/helpers/encode_spec.rb
491
+ - spec/helpers/params_spec.rb
491
492
  - spec/helpers/serialize_spec.rb
492
493
  - spec/integration_spec.rb
493
494
  - spec/log_spec.rb
@@ -528,7 +529,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
528
529
  version: '0'
529
530
  requirements: []
530
531
  rubyforge_project:
531
- rubygems_version: 2.6.11
532
+ rubygems_version: 2.6.13
532
533
  signing_key:
533
534
  specification_version: 4
534
535
  summary: Basic tooling to support API apps in Sinatra