faraday_hal_middleware 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/lint.yml +16 -0
- data/.github/workflows/test.yml +18 -0
- data/.rubocop.yml +5 -0
- data/.rubocop_todo.yml +67 -20
- data/CHANGELOG.md +5 -0
- data/Gemfile +5 -1
- data/README.md +4 -2
- data/Rakefile +2 -0
- data/faraday_hal_middleware.gemspec +7 -5
- data/lib/faraday/hal_json/request.rb +68 -0
- data/lib/faraday/hal_json/response.rb +8 -0
- data/lib/faraday/hal_json/version.rb +7 -0
- data/lib/faraday/hal_json.rb +12 -0
- data/lib/faraday_hal_middleware.rb +3 -18
- data/spec/spec_helper.rb +3 -35
- data/spec/unit/{encode_hal_json_spec.rb → request_spec.rb} +7 -4
- data/spec/unit/response_spec.rb +192 -0
- metadata +22 -22
- data/.travis.yml +0 -6
- data/lib/faraday_hal_middleware/version.rb +0 -3
- data/lib/faraday_middleware/request/encode_hal_json.rb +0 -52
- data/lib/faraday_middleware/response/parse_hal_json.rb +0 -7
- data/spec/unit/parse_hal_json_spec.rb +0 -79
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 129ae7965c9e0d8c0e7b4e83386f4555a1b694d704b559f18e7f62e16b815851
|
4
|
+
data.tar.gz: 639ae15211c5ec76fe2e29492a7c0fc2791127eddfd65360fb9442453cd7d825
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cd2271ad2df1acec1af6c6eeaba8ba187869a4196e0bb7d3a0c1b7b44f9fd699fe73d2affc7286d136f4e933c672673b236ef5aee11d9c40910f4cd7013442d5
|
7
|
+
data.tar.gz: 85c9cac01aa86c83994a19336db03754e9530b0dbe047c2a7fa11c51c6f996ea4921fdde98c51cf2c5c44360be6be2c428c227b87ce55c4fcd01014c460ac523
|
@@ -0,0 +1,16 @@
|
|
1
|
+
name: lint
|
2
|
+
on: [push, pull_request]
|
3
|
+
jobs:
|
4
|
+
rubocop:
|
5
|
+
name: RuboCop
|
6
|
+
runs-on: ubuntu-latest
|
7
|
+
steps:
|
8
|
+
- name: Checkout
|
9
|
+
uses: actions/checkout@v3
|
10
|
+
- name: Set up Ruby
|
11
|
+
uses: ruby/setup-ruby@v1
|
12
|
+
with:
|
13
|
+
ruby-version: 2.7
|
14
|
+
bundler-cache: true
|
15
|
+
- run: bundle exec rubocop
|
16
|
+
|
@@ -0,0 +1,18 @@
|
|
1
|
+
---
|
2
|
+
name: test
|
3
|
+
on: [push, pull_request]
|
4
|
+
jobs:
|
5
|
+
test:
|
6
|
+
runs-on: ubuntu-latest
|
7
|
+
strategy:
|
8
|
+
fail-fast: false
|
9
|
+
matrix:
|
10
|
+
ruby: [ '2.7', '3.0', '3.1', '3.2' ]
|
11
|
+
name: test (${{ matrix.ruby }})
|
12
|
+
steps:
|
13
|
+
- uses: actions/checkout@v3
|
14
|
+
- uses: ruby/setup-ruby@v1
|
15
|
+
with:
|
16
|
+
ruby-version: ${{ matrix.ruby }}
|
17
|
+
- run: bundle install --jobs=3 --retry=3 --path=vendor/bundle
|
18
|
+
- run: bundle exec rake spec
|
data/.rubocop.yml
CHANGED
data/.rubocop_todo.yml
CHANGED
@@ -1,36 +1,83 @@
|
|
1
1
|
# This configuration was generated by
|
2
2
|
# `rubocop --auto-gen-config`
|
3
|
-
# on
|
3
|
+
# on 2024-02-01 13:35:51 UTC using RuboCop version 1.60.2.
|
4
4
|
# The point is for the user to remove these configuration records
|
5
5
|
# one by one as the offenses are removed from the code base.
|
6
6
|
# Note that changes in the inspected code, or installation of new
|
7
7
|
# versions of RuboCop, may require this file to be generated again.
|
8
8
|
|
9
|
-
# Offense count:
|
10
|
-
# Configuration parameters:
|
11
|
-
|
12
|
-
|
9
|
+
# Offense count: 1
|
10
|
+
# Configuration parameters: Severity, Include.
|
11
|
+
# Include: **/*.gemspec
|
12
|
+
Gemspec/RequiredRubyVersion:
|
13
|
+
Exclude:
|
14
|
+
- 'faraday_hal_middleware.gemspec'
|
13
15
|
|
14
16
|
# Offense count: 1
|
15
|
-
# Configuration parameters:
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
#
|
20
|
-
|
17
|
+
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
|
18
|
+
Metrics/AbcSize:
|
19
|
+
Max: 21
|
20
|
+
|
21
|
+
# Offense count: 10
|
22
|
+
# Configuration parameters: Prefixes, AllowedPatterns.
|
23
|
+
# Prefixes: when, with, without
|
24
|
+
RSpec/ContextWording:
|
21
25
|
Exclude:
|
22
|
-
- 'spec
|
23
|
-
- '
|
26
|
+
- 'spec/unit/request_spec.rb'
|
27
|
+
- 'spec/unit/response_spec.rb'
|
24
28
|
|
25
29
|
# Offense count: 2
|
26
|
-
|
30
|
+
# Configuration parameters: Include, CustomTransform, IgnoreMethods, SpecSuffixOnly.
|
31
|
+
# Include: **/*_spec*rb*, **/spec/**/*
|
32
|
+
RSpec/FilePath:
|
27
33
|
Exclude:
|
28
|
-
- 'spec
|
29
|
-
- '
|
30
|
-
|
34
|
+
- 'spec/unit/request_spec.rb'
|
35
|
+
- 'spec/unit/response_spec.rb'
|
36
|
+
|
37
|
+
# Offense count: 4
|
38
|
+
# Configuration parameters: .
|
39
|
+
# SupportedStyles: have_received, receive
|
40
|
+
RSpec/MessageSpies:
|
41
|
+
EnforcedStyle: receive
|
42
|
+
|
43
|
+
# Offense count: 7
|
44
|
+
RSpec/MultipleExpectations:
|
45
|
+
Max: 2
|
46
|
+
|
47
|
+
# Offense count: 4
|
48
|
+
# Configuration parameters: AllowSubject.
|
49
|
+
RSpec/MultipleMemoizedHelpers:
|
50
|
+
Max: 6
|
51
|
+
|
52
|
+
# Offense count: 2
|
53
|
+
RSpec/RepeatedExample:
|
54
|
+
Exclude:
|
55
|
+
- 'spec/unit/response_spec.rb'
|
56
|
+
|
57
|
+
# Offense count: 2
|
58
|
+
# Configuration parameters: Include, CustomTransform, IgnoreMethods, IgnoreMetadata.
|
59
|
+
# Include: **/*_spec.rb
|
60
|
+
RSpec/SpecFilePathFormat:
|
61
|
+
Exclude:
|
62
|
+
- '**/spec/routing/**/*'
|
63
|
+
- 'spec/unit/request_spec.rb'
|
64
|
+
- 'spec/unit/response_spec.rb'
|
65
|
+
|
66
|
+
# Offense count: 4
|
67
|
+
RSpec/StubbedMock:
|
68
|
+
Exclude:
|
69
|
+
- 'spec/unit/response_spec.rb'
|
31
70
|
|
32
71
|
# Offense count: 1
|
33
|
-
# Configuration parameters:
|
34
|
-
|
72
|
+
# Configuration parameters: IgnoreNameless, IgnoreSymbolicNames.
|
73
|
+
RSpec/VerifiedDoubles:
|
35
74
|
Exclude:
|
36
|
-
- '
|
75
|
+
- 'spec/unit/response_spec.rb'
|
76
|
+
|
77
|
+
# Offense count: 1
|
78
|
+
# Configuration parameters: AllowedConstants.
|
79
|
+
Style/Documentation:
|
80
|
+
Exclude:
|
81
|
+
- 'spec/**/*'
|
82
|
+
- 'test/**/*'
|
83
|
+
- 'lib/faraday/hal_json.rb'
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
### 0.2.0 (2024/02/01)
|
2
|
+
|
3
|
+
* [#11](https://github.com/fetch/faraday_hal_middleware/pull/11): Refactor into Faraday::HalJson::Request and Response - [@dblock](https://github.com/dblock).
|
4
|
+
* [#9](https://github.com/fetch/faraday_hal_middleware/pull/9): Added support for Faraday 2, removed dependency on deprecated `faraday_hal_middleware` - [@dblock](https://github.com/dblock).
|
5
|
+
|
1
6
|
### 0.1.1 (2020/05/10)
|
2
7
|
|
3
8
|
* [#6](https://github.com/fetch/faraday_hal_middleware/pull/6): Added CHANGELOG, CONTRIBUTING and RELEASING - [@dblock](https://github.com/dblock).
|
data/Gemfile
CHANGED
@@ -1,10 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
source 'https://rubygems.org'
|
2
4
|
|
3
5
|
gem 'json'
|
4
6
|
gem 'rake', '>= 0.9'
|
5
7
|
|
6
8
|
group :development do
|
7
|
-
gem 'rubocop', '
|
9
|
+
gem 'rubocop', '1.60.2'
|
10
|
+
gem 'rubocop-rake'
|
11
|
+
gem 'rubocop-rspec'
|
8
12
|
end
|
9
13
|
|
10
14
|
group :test do
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
[Faraday](https://github.com/lostisland/faraday) Middleware for JSON HAL requests and responses with `application/hal+json` content-type.
|
4
4
|
|
5
|
-
[![
|
5
|
+
[![test](https://github.com/fetch/faraday_hal_middleware/actions/workflows/test.yml/badge.svg)](https://github.com/fetch/faraday_hal_middleware/actions/workflows/test.yml)
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
@@ -12,9 +12,11 @@ Add this line to your application's Gemfile:
|
|
12
12
|
gem 'faraday_hal_middleware'
|
13
13
|
```
|
14
14
|
|
15
|
+
Use version 0.1.1 for Faraday 1.x, and >= 0.2.0 for newer versions of Faraday.
|
16
|
+
|
15
17
|
## Usage
|
16
18
|
|
17
|
-
Use `faraday_hal_middleware
|
19
|
+
Use `faraday_hal_middleware`.
|
18
20
|
|
19
21
|
```ruby
|
20
22
|
require 'faraday_hal_middleware'
|
data/Rakefile
CHANGED
@@ -1,11 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
|
2
|
-
lib = File.expand_path('
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
3
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
|
5
|
+
|
6
|
+
require 'faraday/hal_json/version'
|
5
7
|
|
6
8
|
Gem::Specification.new do |spec|
|
7
9
|
spec.name = 'faraday_hal_middleware'
|
8
|
-
spec.version =
|
10
|
+
spec.version = Faraday::HalJson::VERSION
|
9
11
|
spec.authors = ['Koen Punt']
|
10
12
|
spec.email = ['koen@fetch.nl']
|
11
13
|
spec.summary = 'Faraday Middleware for JSON HAL requests and responses.'
|
@@ -15,8 +17,8 @@ Gem::Specification.new do |spec|
|
|
15
17
|
|
16
18
|
spec.files = `git ls-files -z`.split("\x0")
|
17
19
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
-
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
20
|
spec.require_paths = ['lib']
|
20
21
|
|
21
|
-
spec.add_dependency '
|
22
|
+
spec.add_dependency 'faraday', '~> 2.0'
|
23
|
+
spec.metadata['rubygems_mfa_required'] = 'true'
|
22
24
|
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'faraday'
|
4
|
+
|
5
|
+
module Faraday
|
6
|
+
module HalJson
|
7
|
+
# Request middleware that encodes the body as JSON.
|
8
|
+
#
|
9
|
+
# Processes only requests with matching Content-type or those without a type.
|
10
|
+
# If a request doesn't have a type but has a body, it sets the Content-type
|
11
|
+
# to JSON MIME-type.
|
12
|
+
#
|
13
|
+
# Doesn't try to encode bodies that already are in string form.
|
14
|
+
class Request < Faraday::Middleware
|
15
|
+
CONTENT_TYPE = 'Content-Type'
|
16
|
+
MIME_TYPE = 'application/hal+json'
|
17
|
+
|
18
|
+
def on_request(env)
|
19
|
+
match_content_type(env) do |data|
|
20
|
+
env[:body] = encode(data)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def encode(data)
|
27
|
+
if options[:encoder].is_a?(Array) && options[:encoder].size >= 2
|
28
|
+
options[:encoder][0].public_send(options[:encoder][1], data)
|
29
|
+
elsif options[:encoder].respond_to?(:dump)
|
30
|
+
options[:encoder].dump(data)
|
31
|
+
else
|
32
|
+
::JSON.generate(data)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def match_content_type(env)
|
37
|
+
return unless process_request?(env)
|
38
|
+
|
39
|
+
env[:request_headers][CONTENT_TYPE] ||= MIME_TYPE
|
40
|
+
yield env[:body] unless env[:body].respond_to?(:to_str)
|
41
|
+
end
|
42
|
+
|
43
|
+
def process_request?(env)
|
44
|
+
type = request_type(env)
|
45
|
+
body?(env) && (type.empty? || type == MIME_TYPE)
|
46
|
+
end
|
47
|
+
|
48
|
+
def body?(env)
|
49
|
+
body = env[:body]
|
50
|
+
case body
|
51
|
+
when true, false
|
52
|
+
true
|
53
|
+
when nil
|
54
|
+
# NOTE: nil can be converted to `"null"`, but this middleware doesn't process `nil` for the compatibility.
|
55
|
+
false
|
56
|
+
else
|
57
|
+
!(body.respond_to?(:to_str) && body.empty?)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def request_type(env)
|
62
|
+
type = env[:request_headers][CONTENT_TYPE].to_s
|
63
|
+
type = type.split(';', 2).first if type.index(';')
|
64
|
+
type
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'hal_json/version'
|
4
|
+
require_relative 'hal_json/request'
|
5
|
+
require_relative 'hal_json/response'
|
6
|
+
|
7
|
+
module Faraday
|
8
|
+
module HalJson
|
9
|
+
::Faraday::Request.register_middleware(hal_json: ::Faraday::HalJson::Request)
|
10
|
+
::Faraday::Response.register_middleware(hal_json: ::Faraday::HalJson::Response)
|
11
|
+
end
|
12
|
+
end
|
@@ -1,19 +1,4 @@
|
|
1
|
-
|
2
|
-
require 'faraday'
|
3
|
-
require 'faraday_middleware'
|
4
|
-
|
5
|
-
module FaradayHalMiddleware
|
6
|
-
end
|
7
|
-
|
8
|
-
module FaradayMiddleware
|
9
|
-
autoload :EncodeHalJson, 'faraday_middleware/request/encode_hal_json'
|
10
|
-
autoload :ParseHalJson, 'faraday_middleware/response/parse_hal_json'
|
1
|
+
# frozen_string_literal: true
|
11
2
|
|
12
|
-
|
13
|
-
|
14
|
-
hal_json: -> { EncodeHalJson }
|
15
|
-
|
16
|
-
Faraday::Response.register_middleware \
|
17
|
-
hal_json: -> { ParseHalJson }
|
18
|
-
end
|
19
|
-
end
|
3
|
+
require 'faraday'
|
4
|
+
require_relative 'faraday/hal_json'
|
data/spec/spec_helper.rb
CHANGED
@@ -1,43 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'rspec'
|
2
4
|
require 'webmock/rspec'
|
3
5
|
require 'faraday'
|
4
|
-
|
5
|
-
module EnvCompatibility
|
6
|
-
def faraday_env(env)
|
7
|
-
if defined?(Faraday::Env)
|
8
|
-
Faraday::Env.from(env)
|
9
|
-
else
|
10
|
-
env
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
module ResponseMiddlewareExampleGroup
|
16
|
-
def self.included(base)
|
17
|
-
base.let(:options) { {} }
|
18
|
-
base.let(:headers) { {} }
|
19
|
-
base.let(:middleware) do
|
20
|
-
described_class.new(lambda { |env|
|
21
|
-
Faraday::Response.new(env)
|
22
|
-
}, options)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
def process(body, content_type = nil, options = {})
|
27
|
-
env = {
|
28
|
-
body: body, request: options,
|
29
|
-
request_headers: Faraday::Utils::Headers.new,
|
30
|
-
response_headers: Faraday::Utils::Headers.new(headers)
|
31
|
-
}
|
32
|
-
env[:response_headers]['content-type'] = content_type if content_type
|
33
|
-
yield(env) if block_given?
|
34
|
-
middleware.call(faraday_env(env))
|
35
|
-
end
|
36
|
-
end
|
6
|
+
require 'faraday_hal_middleware'
|
37
7
|
|
38
8
|
RSpec.configure do |config|
|
39
|
-
config.include EnvCompatibility
|
40
|
-
config.include ResponseMiddlewareExampleGroup, type: :response
|
41
9
|
config.expect_with :rspec do |c|
|
42
10
|
c.syntax = :expect
|
43
11
|
end
|
@@ -1,12 +1,15 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
# mostly https://github.com/lostisland/faraday/blob/main/spec/faraday/request/json_spec.rb
|
6
|
+
describe Faraday::HalJson::Request, type: :request do
|
7
|
+
let(:middleware) { described_class.new(->(env) { Faraday::Response.new(env) }) }
|
5
8
|
|
6
9
|
def process(body, content_type = nil)
|
7
10
|
env = { body: body, request_headers: Faraday::Utils::Headers.new }
|
8
11
|
env[:request_headers]['content-type'] = content_type if content_type
|
9
|
-
middleware.call(
|
12
|
+
middleware.call(Faraday::Env.from(env)).env
|
10
13
|
end
|
11
14
|
|
12
15
|
def result_body
|
@@ -0,0 +1,192 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
# copy of https://github.com/lostisland/faraday/blob/main/spec/faraday/response/json_spec.rb
|
6
|
+
describe Faraday::HalJson::Response do
|
7
|
+
let(:options) { {} }
|
8
|
+
let(:headers) { {} }
|
9
|
+
let(:middleware) do
|
10
|
+
described_class.new(lambda { |env|
|
11
|
+
Faraday::Response.new(env)
|
12
|
+
}, **options)
|
13
|
+
end
|
14
|
+
|
15
|
+
def process(body, content_type = 'application/json', options = {})
|
16
|
+
env = {
|
17
|
+
body: body, request: options,
|
18
|
+
request_headers: Faraday::Utils::Headers.new,
|
19
|
+
response_headers: Faraday::Utils::Headers.new(headers)
|
20
|
+
}
|
21
|
+
env[:response_headers]['content-type'] = content_type if content_type
|
22
|
+
yield(env) if block_given?
|
23
|
+
middleware.call(Faraday::Env.from(env))
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'no type matching' do
|
27
|
+
it "doesn't change nil body" do
|
28
|
+
expect(process(nil).body).to be_nil
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'nullifies empty body' do
|
32
|
+
expect(process('').body).to be_nil
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'parses json body' do
|
36
|
+
response = process('{"a":1}')
|
37
|
+
expect(response.body).to eq('a' => 1)
|
38
|
+
expect(response.env[:raw_body]).to be_nil
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context 'with preserving raw' do
|
43
|
+
let(:options) { { preserve_raw: true } }
|
44
|
+
|
45
|
+
it 'parses json body' do
|
46
|
+
response = process('{"a":1}')
|
47
|
+
expect(response.body).to eq('a' => 1)
|
48
|
+
expect(response.env[:raw_body]).to eq('{"a":1}')
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'with default regexp type matching' do
|
53
|
+
it 'parses json body of correct type' do
|
54
|
+
response = process('{"a":1}', 'application/x-json')
|
55
|
+
expect(response.body).to eq('a' => 1)
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'ignores json body of incorrect type' do
|
59
|
+
response = process('{"a":1}', 'text/json-xml')
|
60
|
+
expect(response.body).to eq('{"a":1}')
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context 'with array type matching' do
|
65
|
+
let(:options) { { content_type: %w[a/b c/d] } }
|
66
|
+
|
67
|
+
it 'parses json body of correct type' do
|
68
|
+
expect(process('{"a":1}', 'a/b').body).to be_a(Hash)
|
69
|
+
expect(process('{"a":1}', 'c/d').body).to be_a(Hash)
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'ignores json body of incorrect type' do
|
73
|
+
expect(process('{"a":1}', 'a/d').body).not_to be_a(Hash)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'chokes on invalid json' do
|
78
|
+
expect { process('{!') }.to raise_error(Faraday::ParsingError)
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'includes the response on the ParsingError instance' do
|
82
|
+
process('{') { |env| env[:response] = Faraday::Response.new }
|
83
|
+
raise 'Parsing should have failed.'
|
84
|
+
rescue Faraday::ParsingError => e
|
85
|
+
expect(e.response).to be_a(Faraday::Response)
|
86
|
+
end
|
87
|
+
|
88
|
+
context 'HEAD responses' do
|
89
|
+
it "nullifies the body if it's only one space" do
|
90
|
+
response = process(' ')
|
91
|
+
expect(response.body).to be_nil
|
92
|
+
end
|
93
|
+
|
94
|
+
it "nullifies the body if it's two spaces" do
|
95
|
+
response = process(' ')
|
96
|
+
expect(response.body).to be_nil
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
context 'JSON options' do
|
101
|
+
let(:body) { '{"a": 1}' }
|
102
|
+
let(:result) { { a: 1 } }
|
103
|
+
let(:options) do
|
104
|
+
{
|
105
|
+
parser_options: {
|
106
|
+
symbolize_names: true
|
107
|
+
}
|
108
|
+
}
|
109
|
+
end
|
110
|
+
|
111
|
+
it 'passes relevant options to JSON parse' do
|
112
|
+
expect(JSON).to receive(:parse)
|
113
|
+
.with(body, options[:parser_options])
|
114
|
+
.and_return(result)
|
115
|
+
|
116
|
+
response = process(body)
|
117
|
+
expect(response.body).to eq(result)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
context 'with decoder' do
|
122
|
+
let(:decoder) do
|
123
|
+
double('Decoder').tap do |e|
|
124
|
+
allow(e).to receive(:load) { |s, opts| JSON.parse(s, opts) }
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
let(:body) { '{"a": 1}' }
|
129
|
+
let(:result) { { a: 1 } }
|
130
|
+
|
131
|
+
context 'when decoder is passed as object' do
|
132
|
+
let(:options) do
|
133
|
+
{
|
134
|
+
parser_options: {
|
135
|
+
decoder: decoder,
|
136
|
+
option: :option_value,
|
137
|
+
symbolize_names: true
|
138
|
+
}
|
139
|
+
}
|
140
|
+
end
|
141
|
+
|
142
|
+
it 'passes relevant options to specified decoder\'s load method' do
|
143
|
+
expect(decoder).to receive(:load)
|
144
|
+
.with(body, { option: :option_value, symbolize_names: true })
|
145
|
+
.and_return(result)
|
146
|
+
|
147
|
+
response = process(body)
|
148
|
+
expect(response.body).to eq(result)
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
context 'when decoder is passed as an object-method pair' do
|
153
|
+
let(:options) do
|
154
|
+
{
|
155
|
+
parser_options: {
|
156
|
+
decoder: [decoder, :load],
|
157
|
+
option: :option_value,
|
158
|
+
symbolize_names: true
|
159
|
+
}
|
160
|
+
}
|
161
|
+
end
|
162
|
+
|
163
|
+
it 'passes relevant options to specified decoder\'s method' do
|
164
|
+
expect(decoder).to receive(:load)
|
165
|
+
.with(body, { option: :option_value, symbolize_names: true })
|
166
|
+
.and_return(result)
|
167
|
+
|
168
|
+
response = process(body)
|
169
|
+
expect(response.body).to eq(result)
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
context 'when decoder is not passed' do
|
174
|
+
let(:options) do
|
175
|
+
{
|
176
|
+
parser_options: {
|
177
|
+
symbolize_names: true
|
178
|
+
}
|
179
|
+
}
|
180
|
+
end
|
181
|
+
|
182
|
+
it 'passes relevant options to JSON parse' do
|
183
|
+
expect(JSON).to receive(:parse)
|
184
|
+
.with(body, { symbolize_names: true })
|
185
|
+
.and_return(result)
|
186
|
+
|
187
|
+
response = process(body)
|
188
|
+
expect(response.body).to eq(result)
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
end
|
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faraday_hal_middleware
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Koen Punt
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-02-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: faraday
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '0
|
19
|
+
version: '2.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '0
|
26
|
+
version: '2.0'
|
27
27
|
description: Faraday Middleware for JSON HAL requests and responses.
|
28
28
|
email:
|
29
29
|
- koen@fetch.nl
|
@@ -31,11 +31,12 @@ executables: []
|
|
31
31
|
extensions: []
|
32
32
|
extra_rdoc_files: []
|
33
33
|
files:
|
34
|
+
- ".github/workflows/lint.yml"
|
35
|
+
- ".github/workflows/test.yml"
|
34
36
|
- ".gitignore"
|
35
37
|
- ".rspec"
|
36
38
|
- ".rubocop.yml"
|
37
39
|
- ".rubocop_todo.yml"
|
38
|
-
- ".travis.yml"
|
39
40
|
- CHANGELOG.md
|
40
41
|
- CONTRIBUTING.md
|
41
42
|
- Gemfile
|
@@ -44,18 +45,20 @@ files:
|
|
44
45
|
- RELEASING.md
|
45
46
|
- Rakefile
|
46
47
|
- faraday_hal_middleware.gemspec
|
48
|
+
- lib/faraday/hal_json.rb
|
49
|
+
- lib/faraday/hal_json/request.rb
|
50
|
+
- lib/faraday/hal_json/response.rb
|
51
|
+
- lib/faraday/hal_json/version.rb
|
47
52
|
- lib/faraday_hal_middleware.rb
|
48
|
-
- lib/faraday_hal_middleware/version.rb
|
49
|
-
- lib/faraday_middleware/request/encode_hal_json.rb
|
50
|
-
- lib/faraday_middleware/response/parse_hal_json.rb
|
51
53
|
- spec/spec_helper.rb
|
52
|
-
- spec/unit/
|
53
|
-
- spec/unit/
|
54
|
+
- spec/unit/request_spec.rb
|
55
|
+
- spec/unit/response_spec.rb
|
54
56
|
homepage: https://github.com/fetch/faraday_hal_middleware
|
55
57
|
licenses:
|
56
58
|
- MIT
|
57
|
-
metadata:
|
58
|
-
|
59
|
+
metadata:
|
60
|
+
rubygems_mfa_required: 'true'
|
61
|
+
post_install_message:
|
59
62
|
rdoc_options: []
|
60
63
|
require_paths:
|
61
64
|
- lib
|
@@ -70,11 +73,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
70
73
|
- !ruby/object:Gem::Version
|
71
74
|
version: '0'
|
72
75
|
requirements: []
|
73
|
-
rubygems_version: 3.
|
74
|
-
signing_key:
|
76
|
+
rubygems_version: 3.1.6
|
77
|
+
signing_key:
|
75
78
|
specification_version: 4
|
76
79
|
summary: Faraday Middleware for JSON HAL requests and responses.
|
77
|
-
test_files:
|
78
|
-
- spec/spec_helper.rb
|
79
|
-
- spec/unit/encode_hal_json_spec.rb
|
80
|
-
- spec/unit/parse_hal_json_spec.rb
|
80
|
+
test_files: []
|
data/.travis.yml
DELETED
@@ -1,52 +0,0 @@
|
|
1
|
-
require 'faraday'
|
2
|
-
|
3
|
-
module FaradayMiddleware
|
4
|
-
# Request middleware that encodes the body as JSON.
|
5
|
-
#
|
6
|
-
# Processes only requests with matching Content-type or those without a type.
|
7
|
-
# If a request doesn't have a type but has a body, it sets the Content-type
|
8
|
-
# to JSON MIME-type.
|
9
|
-
#
|
10
|
-
# Doesn't try to encode bodies that already are in string form.
|
11
|
-
class EncodeHalJson < Faraday::Middleware
|
12
|
-
CONTENT_TYPE = 'Content-Type'.freeze
|
13
|
-
MIME_TYPE = 'application/hal+json'.freeze
|
14
|
-
|
15
|
-
dependency do
|
16
|
-
require 'json' unless defined?(::JSON)
|
17
|
-
end
|
18
|
-
|
19
|
-
def call(env)
|
20
|
-
match_content_type(env) do |data|
|
21
|
-
env[:body] = encode data
|
22
|
-
end
|
23
|
-
@app.call env
|
24
|
-
end
|
25
|
-
|
26
|
-
def encode(data)
|
27
|
-
::JSON.dump data
|
28
|
-
end
|
29
|
-
|
30
|
-
def match_content_type(env)
|
31
|
-
if process_request?(env)
|
32
|
-
env[:request_headers][CONTENT_TYPE] ||= MIME_TYPE
|
33
|
-
yield env[:body] unless env[:body].respond_to?(:to_str)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
def process_request?(env)
|
38
|
-
type = request_type(env)
|
39
|
-
has_body?(env) && (type.empty? || (type == MIME_TYPE))
|
40
|
-
end
|
41
|
-
|
42
|
-
def has_body?(env)
|
43
|
-
(body = env[:body]) && !(body.respond_to?(:to_str) && body.empty?)
|
44
|
-
end
|
45
|
-
|
46
|
-
def request_type(env)
|
47
|
-
type = env[:request_headers][CONTENT_TYPE].to_s
|
48
|
-
type = type.split(';', 2).first if type.index(';')
|
49
|
-
type
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
@@ -1,79 +0,0 @@
|
|
1
|
-
require 'faraday_middleware/response/parse_hal_json'
|
2
|
-
|
3
|
-
describe FaradayMiddleware::ParseHalJson, type: :response do
|
4
|
-
context 'no type matching' do
|
5
|
-
it "doesn't change nil body" do
|
6
|
-
expect(process(nil).body).to be_nil
|
7
|
-
end
|
8
|
-
|
9
|
-
it 'nullifies empty body' do
|
10
|
-
expect(process('').body).to be_nil
|
11
|
-
end
|
12
|
-
|
13
|
-
it 'parses json body' do
|
14
|
-
response = process('{"a":1}')
|
15
|
-
expect(response.body).to eq('a' => 1)
|
16
|
-
expect(response.env[:raw_body]).to be_nil
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
context 'with preserving raw' do
|
21
|
-
let(:options) { { preserve_raw: true } }
|
22
|
-
|
23
|
-
it 'parses json body' do
|
24
|
-
response = process('{"a":1}')
|
25
|
-
expect(response.body).to eq('a' => 1)
|
26
|
-
expect(response.env[:raw_body]).to eq('{"a":1}')
|
27
|
-
end
|
28
|
-
|
29
|
-
it 'can opt out of preserving raw' do
|
30
|
-
response = process('{"a":1}', nil, preserve_raw: false)
|
31
|
-
expect(response.env[:raw_body]).to be_nil
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
context 'with regexp type matching' do
|
36
|
-
let(:options) { { content_type: /\bjson$/ } }
|
37
|
-
|
38
|
-
it 'parses json body of correct type' do
|
39
|
-
response = process('{"a":1}', 'application/x-json')
|
40
|
-
expect(response.body).to eq('a' => 1)
|
41
|
-
end
|
42
|
-
|
43
|
-
it 'ignores json body of incorrect type' do
|
44
|
-
response = process('{"a":1}', 'text/json-xml')
|
45
|
-
expect(response.body).to eq('{"a":1}')
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
context 'with array type matching' do
|
50
|
-
let(:options) { { content_type: %w[a/b c/d] } }
|
51
|
-
|
52
|
-
it 'parses json body of correct type' do
|
53
|
-
expect(process('{"a":1}', 'a/b').body).to be_a(Hash)
|
54
|
-
expect(process('{"a":1}', 'c/d').body).to be_a(Hash)
|
55
|
-
end
|
56
|
-
|
57
|
-
it 'ignores json body of incorrect type' do
|
58
|
-
expect(process('{"a":1}', 'a/d').body).not_to be_a(Hash)
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
it 'chokes on invalid json' do
|
63
|
-
['{!', '"a'].each do |data|
|
64
|
-
expect { process(data) }.to raise_error(Faraday::ParsingError)
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
context 'HEAD responses' do
|
69
|
-
it "nullifies the body if it's only one space" do
|
70
|
-
response = process(' ')
|
71
|
-
expect(response.body).to be_nil
|
72
|
-
end
|
73
|
-
|
74
|
-
it "nullifies the body if it's two spaces" do
|
75
|
-
response = process(' ')
|
76
|
-
expect(response.body).to be_nil
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|