eac_envs-http 0.1.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/eac_envs/http/request/faraday_connection.rb +75 -0
- data/lib/eac_envs/http/request.rb +5 -31
- data/lib/eac_envs/http/response.rb +9 -3
- data/lib/eac_envs/http/rspec/echo_server/request_processor.rb +42 -0
- data/lib/eac_envs/http/rspec/echo_server/webrick_servlet.rb +22 -0
- data/lib/eac_envs/http/rspec/echo_server.rb +63 -0
- data/lib/eac_envs/http/rspec.rb +11 -0
- data/lib/eac_envs/http/version.rb +1 -1
- metadata +42 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a6c22151cda479613cd35ca7c01f5b838db242db70ab052a646c1e83fcf54509
|
4
|
+
data.tar.gz: 27f012142091b0dbefeb57c49cabfa0e31823edc4f4dd84f8bf68fb818802717
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d42a1a5f35cbb274efb9251fb651bf3ff730b735951a85d9e20b5a6edfa1cd1df4499c40e27c3415d4f41c146266ab87200e1fad559a1dec225490bb47e31ebd
|
7
|
+
data.tar.gz: 1778ddfdb370f27756547f89285be738f030a776e194a7a6a0a4f950a3a23ac438c4eb108a02c0aae7b0ce12c6213a8ef2a8a06e218cbe7307263a8e3f12b387
|
@@ -0,0 +1,75 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'eac_ruby_utils/core_ext'
|
4
|
+
require 'faraday'
|
5
|
+
require 'faraday/follow_redirects'
|
6
|
+
require 'faraday/gzip'
|
7
|
+
require 'faraday/multipart'
|
8
|
+
require 'faraday/retry'
|
9
|
+
|
10
|
+
module EacEnvs
|
11
|
+
module Http
|
12
|
+
class Request
|
13
|
+
class FaradayConnection
|
14
|
+
enable_method_class
|
15
|
+
common_constructor :request
|
16
|
+
|
17
|
+
SETUPS = %i[multipart authorization follow_redirect gzip retry].freeze
|
18
|
+
|
19
|
+
# @return [Faraday::Connection]
|
20
|
+
def result
|
21
|
+
::Faraday.default_connection_options[:headers] = {}
|
22
|
+
::Faraday::Connection.new(connection_options) do |conn|
|
23
|
+
SETUPS.each { |setup| send("setup_#{setup}", conn) }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
# @return [Boolean]
|
30
|
+
def body_with_file?
|
31
|
+
request.body_fields.with_file?
|
32
|
+
end
|
33
|
+
|
34
|
+
# @return [Hash]
|
35
|
+
def connection_options
|
36
|
+
{
|
37
|
+
request: { params_encoder: Faraday::FlatParamsEncoder },
|
38
|
+
ssl: { verify: request.ssl_verify? }
|
39
|
+
}
|
40
|
+
end
|
41
|
+
|
42
|
+
# @param conn [Faraday::Connection]
|
43
|
+
def setup_authorization(conn)
|
44
|
+
request.auth.if_present do |v|
|
45
|
+
conn.request :authorization, :basic, v.username, v.password
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# @param conn [Faraday::Connection]
|
50
|
+
def setup_follow_redirect(conn)
|
51
|
+
conn.response :follow_redirects if request.follow_redirect?
|
52
|
+
end
|
53
|
+
|
54
|
+
# @param conn [Faraday::Connection]
|
55
|
+
def setup_gzip(conn)
|
56
|
+
conn.request :gzip
|
57
|
+
end
|
58
|
+
|
59
|
+
# @param conn [Faraday::Connection]
|
60
|
+
def setup_multipart(conn)
|
61
|
+
if body_with_file?
|
62
|
+
conn.request :multipart, flat_encode: true
|
63
|
+
else
|
64
|
+
conn.request :url_encoded
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# @param conn [Faraday::Connection]
|
69
|
+
def setup_retry(conn)
|
70
|
+
conn.request :retry if request.retry?
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -2,13 +2,11 @@
|
|
2
2
|
|
3
3
|
require 'eac_envs/http/response'
|
4
4
|
require 'eac_ruby_utils/core_ext'
|
5
|
-
require 'faraday'
|
6
|
-
require 'faraday/multipart'
|
7
5
|
|
8
6
|
module EacEnvs
|
9
7
|
module Http
|
10
8
|
class Request
|
11
|
-
BOOLEAN_MODIFIERS = %w[ssl_verify].freeze
|
9
|
+
BOOLEAN_MODIFIERS = %w[follow_redirect retry ssl_verify].freeze
|
12
10
|
COMMON_MODIFIERS = %w[auth body_data response_body_data_proc url verb].freeze
|
13
11
|
HASH_MODIFIERS = %w[header].freeze
|
14
12
|
MODIFIERS = COMMON_MODIFIERS + BOOLEAN_MODIFIERS + HASH_MODIFIERS.map(&:pluralize)
|
@@ -25,24 +23,9 @@ module EacEnvs
|
|
25
23
|
auth(::Struct.new(:username, :password).new(username, password))
|
26
24
|
end
|
27
25
|
|
28
|
-
# @return [
|
29
|
-
def
|
30
|
-
::
|
31
|
-
::Faraday::Connection.new(faraday_connection_options) do |conn|
|
32
|
-
if body_with_file?
|
33
|
-
conn.request :multipart, flat_encode: true
|
34
|
-
else
|
35
|
-
conn.request :url_encoded
|
36
|
-
end
|
37
|
-
auth.if_present { |v| conn.request :authorization, :basic, v.username, v.password }
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
# @return [Hash]
|
42
|
-
def faraday_connection_options
|
43
|
-
{
|
44
|
-
request: { params_encoder: Faraday::FlatParamsEncoder }, ssl: { verify: ssl_verify? }
|
45
|
-
}
|
26
|
+
# @return [EacEnvs::Http::Request::BodyFields]
|
27
|
+
def body_fields
|
28
|
+
@body_fields ||= ::EacEnvs::Http::Request::BodyFields.new(body_data)
|
46
29
|
end
|
47
30
|
|
48
31
|
# @return [Faraday::Response]
|
@@ -65,20 +48,11 @@ module EacEnvs
|
|
65
48
|
|
66
49
|
private
|
67
50
|
|
68
|
-
def body_fields
|
69
|
-
@body_fields ||= ::EacEnvs::Http::Request::BodyFields.new(body_data)
|
70
|
-
end
|
71
|
-
|
72
|
-
# @return [Boolean]
|
73
|
-
def body_with_file?
|
74
|
-
body_fields.with_file?
|
75
|
-
end
|
76
|
-
|
77
51
|
def sanitized_body_data
|
78
52
|
body_fields.to_h || body_data
|
79
53
|
end
|
80
54
|
|
81
|
-
require_sub __FILE__
|
55
|
+
require_sub __FILE__, require_dependency: true
|
82
56
|
end
|
83
57
|
end
|
84
58
|
end
|
@@ -9,6 +9,7 @@ require 'json'
|
|
9
9
|
module EacEnvs
|
10
10
|
module Http
|
11
11
|
class Response < ::StandardError
|
12
|
+
COMMON_HEADERS = %w[Content-Type].freeze
|
12
13
|
HEADER_LINE_PARSER = /\A([^:]+):(.*)\z/.to_parser do |m|
|
13
14
|
[m[1].strip, m[2].strip]
|
14
15
|
end
|
@@ -54,10 +55,9 @@ module EacEnvs
|
|
54
55
|
hash_search(headers, name)
|
55
56
|
end
|
56
57
|
|
58
|
+
# @return [Hash<String, String>]
|
57
59
|
def headers
|
58
|
-
performed.
|
59
|
-
.map { |header_line| HEADER_LINE_PARSER.parse!(header_line) }
|
60
|
-
.to_h
|
60
|
+
performed.headers.to_hash
|
61
61
|
end
|
62
62
|
|
63
63
|
def link(rel)
|
@@ -86,6 +86,12 @@ module EacEnvs
|
|
86
86
|
"URL: #{url}\nStatus: #{status}\nBody:\n\n#{body_str}"
|
87
87
|
end
|
88
88
|
|
89
|
+
COMMON_HEADERS.each do |header_key|
|
90
|
+
define_method header_key.underscore do
|
91
|
+
header(header_key)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
89
95
|
private
|
90
96
|
|
91
97
|
def body_data_from_application_json
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'eac_envs/http/request'
|
4
|
+
require 'eac_ruby_utils/core_ext'
|
5
|
+
require 'webrick'
|
6
|
+
|
7
|
+
module EacEnvs
|
8
|
+
module Http
|
9
|
+
module Rspec
|
10
|
+
class EchoServer
|
11
|
+
class RequestProcessor
|
12
|
+
REQUEST_TO_DATA = {
|
13
|
+
ssl: :ssl?,
|
14
|
+
method: :request_method,
|
15
|
+
uri: :unparsed_uri,
|
16
|
+
body: :body
|
17
|
+
}.freeze
|
18
|
+
|
19
|
+
common_constructor :request, :response
|
20
|
+
|
21
|
+
def perform
|
22
|
+
response.body = response_body
|
23
|
+
response.header['Content-type'] = 'application/json'
|
24
|
+
response.status = 200
|
25
|
+
end
|
26
|
+
|
27
|
+
def request_data
|
28
|
+
REQUEST_TO_DATA.map { |k, v| [k, request.send(v)] }.to_h.merge(headers: request_headers)
|
29
|
+
end
|
30
|
+
|
31
|
+
def request_headers
|
32
|
+
request.header.map { |k, v| [k, v.first] }.to_h
|
33
|
+
end
|
34
|
+
|
35
|
+
def response_body
|
36
|
+
::JSON.generate(request_data)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'eac_envs/http/request'
|
4
|
+
require 'eac_envs/http/rspec/echo_server/request_processor'
|
5
|
+
require 'eac_ruby_utils/core_ext'
|
6
|
+
require 'webrick'
|
7
|
+
|
8
|
+
module EacEnvs
|
9
|
+
module Http
|
10
|
+
module Rspec
|
11
|
+
class EchoServer
|
12
|
+
class WebrickServlet < WEBrick::HTTPServlet::AbstractServlet
|
13
|
+
::EacEnvs::Http::Request.lists.verb.each_value do |verb|
|
14
|
+
define_method "do_#{verb.to_s.upcase}" do |request, response|
|
15
|
+
::EacEnvs::Http::Rspec::EchoServer::RequestProcessor.new(request, response).perform
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'addressable'
|
4
|
+
require 'eac_ruby_utils/core_ext'
|
5
|
+
require 'webrick'
|
6
|
+
require 'webrick/https'
|
7
|
+
|
8
|
+
module EacEnvs
|
9
|
+
module Http
|
10
|
+
module Rspec
|
11
|
+
class EchoServer
|
12
|
+
HOSTNAME = 'localhost'
|
13
|
+
SCHEMES = {
|
14
|
+
http: { Port: 8080 },
|
15
|
+
https: { Port: 8443, SSLEnable: true, SSLCertName: [['CN', HOSTNAME]] }
|
16
|
+
}.freeze
|
17
|
+
|
18
|
+
class << self
|
19
|
+
SCHEMES.each do |scheme, webrick_options|
|
20
|
+
# @return [::EacEnvs::Http::Rspec::EchoServer]
|
21
|
+
define_method scheme do
|
22
|
+
new(scheme, webrick_options)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
common_constructor :scheme, :webrick_options
|
28
|
+
|
29
|
+
def on_active(&block)
|
30
|
+
servlet = webrick_servlet
|
31
|
+
::Thread.abort_on_exception = true
|
32
|
+
servlet_thread = ::Thread.new { servlet.start }
|
33
|
+
begin
|
34
|
+
sleep 0.001 while servlet.status != :Running
|
35
|
+
block.call
|
36
|
+
ensure
|
37
|
+
servlet.shutdown
|
38
|
+
servlet_thread.join
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def root_url
|
43
|
+
::Addressable::URI.new(
|
44
|
+
scheme: scheme.to_s,
|
45
|
+
host: ::EacEnvs::Http::Rspec::EchoServer::HOSTNAME,
|
46
|
+
port: webrick_options.fetch(:Port)
|
47
|
+
)
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
# @return [EacEnvs::Http::Rspec::EchoServer::WebrickServlet]
|
53
|
+
def webrick_servlet
|
54
|
+
r = ::WEBrick::HTTPServer.new(webrick_options)
|
55
|
+
r.mount '/', ::EacEnvs::Http::Rspec::EchoServer::WebrickServlet
|
56
|
+
r
|
57
|
+
end
|
58
|
+
|
59
|
+
require_sub __FILE__
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: eac_envs-http
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Put here the authors
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-04-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: eac_fs
|
@@ -58,6 +58,34 @@ dependencies:
|
|
58
58
|
- - ">="
|
59
59
|
- !ruby/object:Gem::Version
|
60
60
|
version: 2.7.4
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: faraday-follow_redirects
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0.3'
|
68
|
+
type: :runtime
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0.3'
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: faraday-gzip
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - "~>"
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0.1'
|
82
|
+
type: :runtime
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - "~>"
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0.1'
|
61
89
|
- !ruby/object:Gem::Dependency
|
62
90
|
name: faraday-multipart
|
63
91
|
requirement: !ruby/object:Gem::Requirement
|
@@ -79,33 +107,33 @@ dependencies:
|
|
79
107
|
- !ruby/object:Gem::Version
|
80
108
|
version: 1.0.4
|
81
109
|
- !ruby/object:Gem::Dependency
|
82
|
-
name:
|
110
|
+
name: faraday-retry
|
83
111
|
requirement: !ruby/object:Gem::Requirement
|
84
112
|
requirements:
|
85
113
|
- - "~>"
|
86
114
|
- !ruby/object:Gem::Version
|
87
|
-
version: '
|
88
|
-
type: :
|
115
|
+
version: '2.1'
|
116
|
+
type: :runtime
|
89
117
|
prerelease: false
|
90
118
|
version_requirements: !ruby/object:Gem::Requirement
|
91
119
|
requirements:
|
92
120
|
- - "~>"
|
93
121
|
- !ruby/object:Gem::Version
|
94
|
-
version: '
|
122
|
+
version: '2.1'
|
95
123
|
- !ruby/object:Gem::Dependency
|
96
|
-
name:
|
124
|
+
name: aranha-parsers
|
97
125
|
requirement: !ruby/object:Gem::Requirement
|
98
126
|
requirements:
|
99
127
|
- - "~>"
|
100
128
|
- !ruby/object:Gem::Version
|
101
|
-
version: '0.
|
129
|
+
version: '0.17'
|
102
130
|
type: :development
|
103
131
|
prerelease: false
|
104
132
|
version_requirements: !ruby/object:Gem::Requirement
|
105
133
|
requirements:
|
106
134
|
- - "~>"
|
107
135
|
- !ruby/object:Gem::Version
|
108
|
-
version: '0.
|
136
|
+
version: '0.17'
|
109
137
|
- !ruby/object:Gem::Dependency
|
110
138
|
name: eac_ruby_gem_support
|
111
139
|
requirement: !ruby/object:Gem::Requirement
|
@@ -138,7 +166,12 @@ files:
|
|
138
166
|
- lib/eac_envs/http/request/body_field.rb
|
139
167
|
- lib/eac_envs/http/request/body_field_value.rb
|
140
168
|
- lib/eac_envs/http/request/body_fields.rb
|
169
|
+
- lib/eac_envs/http/request/faraday_connection.rb
|
141
170
|
- lib/eac_envs/http/response.rb
|
171
|
+
- lib/eac_envs/http/rspec.rb
|
172
|
+
- lib/eac_envs/http/rspec/echo_server.rb
|
173
|
+
- lib/eac_envs/http/rspec/echo_server/request_processor.rb
|
174
|
+
- lib/eac_envs/http/rspec/echo_server/webrick_servlet.rb
|
142
175
|
- lib/eac_envs/http/version.rb
|
143
176
|
homepage:
|
144
177
|
licenses: []
|