rails_twirp 0.13.1 → 0.13.2
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 +4 -4
- data/.github/workflows/test.yml +21 -0
- data/CHANGELOG.md +3 -0
- data/bin/test +5 -0
- data/lib/rails_twirp/testing/integration_test.rb +106 -0
- data/lib/rails_twirp/version.rb +1 -1
- data/rails_twirp.gemspec +1 -1
- data/test/dummy/Rakefile +6 -0
- data/test/dummy/app/assets/config/manifest.js +2 -0
- data/test/dummy/app/assets/images/.keep +0 -0
- data/test/dummy/app/assets/stylesheets/application.css +15 -0
- data/test/dummy/app/channels/application_cable/channel.rb +4 -0
- data/test/dummy/app/channels/application_cable/connection.rb +4 -0
- data/test/dummy/app/controllers/application_controller.rb +2 -0
- data/test/dummy/app/controllers/application_twirp_controller.rb +7 -0
- data/test/dummy/app/controllers/concerns/.keep +0 -0
- data/test/dummy/app/controllers/dummy_controller.rb +5 -0
- data/test/dummy/app/controllers/pings_controller.rb +40 -0
- data/test/dummy/app/controllers/testmod/nested/other_controller.rb +10 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/helpers/random_helper.rb +5 -0
- data/test/dummy/app/javascript/packs/application.js +15 -0
- data/test/dummy/app/jobs/application_job.rb +7 -0
- data/test/dummy/app/mailers/application_mailer.rb +4 -0
- data/test/dummy/app/models/application_record.rb +3 -0
- data/test/dummy/app/models/concerns/.keep +0 -0
- data/test/dummy/app/views/dummy/rpc_name_check.pb.pbbuilder +3 -0
- data/test/dummy/app/views/layouts/application.html.erb +15 -0
- data/test/dummy/app/views/layouts/mailer.html.erb +13 -0
- data/test/dummy/app/views/layouts/mailer.text.erb +1 -0
- data/test/dummy/app/views/pings/ping_template.pb.pbbuilder +1 -0
- data/test/dummy/bin/generate +3 -0
- data/test/dummy/bin/rails +4 -0
- data/test/dummy/bin/rake +4 -0
- data/test/dummy/bin/setup +33 -0
- data/test/dummy/config/application.rb +23 -0
- data/test/dummy/config/boot.rb +5 -0
- data/test/dummy/config/cable.yml +10 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +76 -0
- data/test/dummy/config/environments/production.rb +120 -0
- data/test/dummy/config/environments/test.rb +59 -0
- data/test/dummy/config/initializers/application_controller_renderer.rb +8 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +8 -0
- data/test/dummy/config/initializers/content_security_policy.rb +28 -0
- data/test/dummy/config/initializers/cookies_serializer.rb +5 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +6 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +4 -0
- data/test/dummy/config/initializers/permissions_policy.rb +11 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +33 -0
- data/test/dummy/config/puma.rb +43 -0
- data/test/dummy/config/routes.rb +3 -0
- data/test/dummy/config/storage.yml +34 -0
- data/test/dummy/config/twirp/routes.rb +22 -0
- data/test/dummy/config.ru +6 -0
- data/test/dummy/lib/assets/.keep +0 -0
- data/test/dummy/log/.keep +0 -0
- data/test/dummy/proto/api.proto +30 -0
- data/test/dummy/proto/api_pb.rb +29 -0
- data/test/dummy/proto/api_twirp.rb +25 -0
- data/test/dummy/public/404.html +67 -0
- data/test/dummy/public/422.html +67 -0
- data/test/dummy/public/500.html +66 -0
- data/test/dummy/public/apple-touch-icon-precomposed.png +0 -0
- data/test/dummy/public/apple-touch-icon.png +0 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy_test.rb +10 -0
- data/test/other_controller_test.rb +9 -0
- data/test/ping_controller_test.rb +114 -0
- data/test/rails_twirp_test.rb +7 -0
- data/test/test_helper.rb +16 -0
- metadata +71 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 46c49b6df28547639b8c56395dfb5c9f327a4ec4b0c2b73b14cdf6b6594f1e17
|
4
|
+
data.tar.gz: f232e0c6f6eac4d4034050cf6cc38a8c6bdfa6a77b260fe54a2d4473a0512350
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e788cdd4ca43c5524ecc34f3dd43dbbbf7081689e0154133def3fcefd92955a46a3aa5abe280a44b51faef30b630f651bdcdbb00b5cade1194e83d969ddfac95
|
7
|
+
data.tar.gz: 1bb844a44698a1a9d1c85f80a555191f8ce5d43a8212e5629b4bdd657b3f908a37b6b50a65d1f28913015bfb477d984c40a09a6a4ba3bf86b20f0da4914c89ae
|
@@ -0,0 +1,21 @@
|
|
1
|
+
name: Ruby Test
|
2
|
+
on: push
|
3
|
+
|
4
|
+
jobs:
|
5
|
+
test:
|
6
|
+
runs-on: ubuntu-latest
|
7
|
+
steps:
|
8
|
+
- name: Checkout code
|
9
|
+
uses: actions/checkout@v3
|
10
|
+
|
11
|
+
- name: Setup Ruby
|
12
|
+
uses: ruby/setup-ruby@v1
|
13
|
+
with:
|
14
|
+
ruby-version: 3.0.0
|
15
|
+
bundler-cache: true
|
16
|
+
|
17
|
+
- name: Run tests
|
18
|
+
run: bin/test
|
19
|
+
|
20
|
+
- name: Run standardrb
|
21
|
+
run: bundle exec standardrb
|
data/CHANGELOG.md
CHANGED
data/bin/test
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
require "twirp/encoding"
|
2
|
+
|
3
|
+
module RailsTwirp
|
4
|
+
class IntegrationTest < ActiveSupport::TestCase
|
5
|
+
DEFAULT_HOST = "www.example.com"
|
6
|
+
Response = Struct.new(:status, :body, :headers)
|
7
|
+
|
8
|
+
attr_reader :response, :request, :controller
|
9
|
+
attr_writer :mount_path
|
10
|
+
alias_method :mount_path!, :mount_path=
|
11
|
+
|
12
|
+
def initialize(name)
|
13
|
+
super
|
14
|
+
reset!
|
15
|
+
@before_rpc = []
|
16
|
+
end
|
17
|
+
|
18
|
+
def host
|
19
|
+
@host || DEFAULT_HOST
|
20
|
+
end
|
21
|
+
attr_writer :host
|
22
|
+
alias_method :host!, :host=
|
23
|
+
|
24
|
+
def https?
|
25
|
+
@https
|
26
|
+
end
|
27
|
+
|
28
|
+
def https!(value = true)
|
29
|
+
@https = value
|
30
|
+
end
|
31
|
+
|
32
|
+
def reset!
|
33
|
+
@request = nil
|
34
|
+
@response = nil
|
35
|
+
@host = nil
|
36
|
+
@host = nil
|
37
|
+
@https = false
|
38
|
+
@mount_path = "/twirp"
|
39
|
+
end
|
40
|
+
|
41
|
+
def before_rpc(&block)
|
42
|
+
@before_rpc << block
|
43
|
+
end
|
44
|
+
|
45
|
+
def rpc(service, rpc, request, headers: nil)
|
46
|
+
@request = request
|
47
|
+
|
48
|
+
env = build_rack_env(service, rpc, request, headers)
|
49
|
+
@before_rpc.each do |hook|
|
50
|
+
hook.call(env)
|
51
|
+
end
|
52
|
+
|
53
|
+
status, headers, body = app.call(env)
|
54
|
+
@response = decode_rack_response(service, rpc, status, headers, body)
|
55
|
+
set_controller_from_rack_env(env)
|
56
|
+
|
57
|
+
@response
|
58
|
+
end
|
59
|
+
|
60
|
+
def app
|
61
|
+
RailsTwirp.test_app
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def build_rack_env(service, rpc, request, headers)
|
67
|
+
env = {
|
68
|
+
"CONTENT_TYPE" => request_content_type,
|
69
|
+
"HTTPS" => https? ? "on" : "off",
|
70
|
+
"HTTP_HOST" => host,
|
71
|
+
"PATH_INFO" => "#{@mount_path}/#{service.service_full_name}/#{rpc}",
|
72
|
+
"REQUEST_METHOD" => "POST",
|
73
|
+
"SERVER_NAME" => host,
|
74
|
+
"SERVER_PORT" => https? ? "443" : "80",
|
75
|
+
"rack.url_scheme" => https? ? "https" : "http"
|
76
|
+
}
|
77
|
+
if headers.present?
|
78
|
+
http_request = ActionDispatch::Request.new(env)
|
79
|
+
http_request.headers.merge! headers
|
80
|
+
end
|
81
|
+
|
82
|
+
input_class = service.rpcs[rpc][:input_class]
|
83
|
+
env["rack.input"] = StringIO.new(Twirp::Encoding.encode(request, input_class, request_content_type))
|
84
|
+
env
|
85
|
+
end
|
86
|
+
|
87
|
+
def request_content_type
|
88
|
+
Twirp::Encoding::PROTO
|
89
|
+
end
|
90
|
+
|
91
|
+
def decode_rack_response(service, rpc, status, headers, body)
|
92
|
+
body = body.join # body is an Enumerable
|
93
|
+
|
94
|
+
if status === 200
|
95
|
+
output_class = service.rpcs[rpc][:output_class]
|
96
|
+
Twirp::Encoding.decode(body, output_class, headers["Content-Type"])
|
97
|
+
else
|
98
|
+
Twirp::Client.error_from_response(Response.new(status, body, headers))
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def set_controller_from_rack_env(env)
|
103
|
+
@controller = ActionDispatch::Request.new(env).controller_instance
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
data/lib/rails_twirp/version.rb
CHANGED
data/rails_twirp.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.summary = "Integrate Twirp into Rails"
|
10
10
|
spec.license = "MIT"
|
11
11
|
|
12
|
-
spec.files = `git ls-files
|
12
|
+
spec.files = `git ls-files`.split("\n")
|
13
13
|
|
14
14
|
spec.add_dependency "rails", ">= 6.1.3"
|
15
15
|
spec.add_dependency "twirp", ">= 1.9", "< 1.11"
|
data/test/dummy/Rakefile
ADDED
File without changes
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
|
10
|
+
* files in this directory. Styles in this file should be added after the last require_* statement.
|
11
|
+
* It is generally better to create a new file per style scope.
|
12
|
+
*
|
13
|
+
*= require_tree .
|
14
|
+
*= require_self
|
15
|
+
*/
|
File without changes
|
@@ -0,0 +1,40 @@
|
|
1
|
+
class PingsController < ApplicationTwirpController
|
2
|
+
before_action :respond_error, only: :before_error
|
3
|
+
|
4
|
+
def ping
|
5
|
+
response = RPC::DummyAPI::PingResponse.new(double_name: request.name * 2)
|
6
|
+
self.response_body = response
|
7
|
+
end
|
8
|
+
|
9
|
+
def ping_render
|
10
|
+
url = rails_twirp_engine_url
|
11
|
+
response = RPC::DummyAPI::PingResponse.new(double_name: "#{url} #{helpers.does_this_work(request.name)}")
|
12
|
+
render pb: response
|
13
|
+
end
|
14
|
+
|
15
|
+
def ping_template
|
16
|
+
@double_name = request.name * 2
|
17
|
+
end
|
18
|
+
|
19
|
+
def error_response
|
20
|
+
error :unauthenticated, "You are not authenticated!!"
|
21
|
+
end
|
22
|
+
|
23
|
+
def raise_error
|
24
|
+
# This error is rescued in ApplicationTwirpController
|
25
|
+
raise ActiveRecord::RecordNotFound, "Not found"
|
26
|
+
end
|
27
|
+
|
28
|
+
def uncaught_raise
|
29
|
+
raise StandardError, "Uncaught"
|
30
|
+
end
|
31
|
+
|
32
|
+
def before_error
|
33
|
+
# This error won't be reached because of the before_action
|
34
|
+
raise NotImplementedError
|
35
|
+
end
|
36
|
+
|
37
|
+
def respond_error
|
38
|
+
error :malformed, "yOuR ReQuEsT Is mAlFoRmEd"
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file. JavaScript code in this file should be added after the last require_* statement.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= require rails-ujs
|
14
|
+
//= require activestorage
|
15
|
+
//= require_tree .
|
@@ -0,0 +1,7 @@
|
|
1
|
+
class ApplicationJob < ActiveJob::Base
|
2
|
+
# Automatically retry jobs that encountered a deadlock
|
3
|
+
# retry_on ActiveRecord::Deadlocked
|
4
|
+
|
5
|
+
# Most jobs are safe to ignore if the underlying records are no longer available
|
6
|
+
# discard_on ActiveJob::DeserializationError
|
7
|
+
end
|
File without changes
|
@@ -0,0 +1,15 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>Dummy</title>
|
5
|
+
<meta name="viewport" content="width=device-width,initial-scale=1">
|
6
|
+
<%= csrf_meta_tags %>
|
7
|
+
<%= csp_meta_tag %>
|
8
|
+
|
9
|
+
<%= stylesheet_link_tag 'application', media: 'all' %>
|
10
|
+
</head>
|
11
|
+
|
12
|
+
<body>
|
13
|
+
<%= yield %>
|
14
|
+
</body>
|
15
|
+
</html>
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= yield %>
|
@@ -0,0 +1 @@
|
|
1
|
+
pb.double_name @double_name
|
data/test/dummy/bin/rake
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require "fileutils"
|
3
|
+
|
4
|
+
# path to your application root.
|
5
|
+
APP_ROOT = File.expand_path("..", __dir__)
|
6
|
+
|
7
|
+
def system!(*args)
|
8
|
+
system(*args) || abort("\n== Command #{args} failed ==")
|
9
|
+
end
|
10
|
+
|
11
|
+
FileUtils.chdir APP_ROOT do
|
12
|
+
# This script is a way to set up or update your development environment automatically.
|
13
|
+
# This script is idempotent, so that you can run it at any time and get an expectable outcome.
|
14
|
+
# Add necessary setup steps to this file.
|
15
|
+
|
16
|
+
puts "== Installing dependencies =="
|
17
|
+
system! "gem install bundler --conservative"
|
18
|
+
system("bundle check") || system!("bundle install")
|
19
|
+
|
20
|
+
# puts "\n== Copying sample files =="
|
21
|
+
# unless File.exist?('config/database.yml')
|
22
|
+
# FileUtils.cp 'config/database.yml.sample', 'config/database.yml'
|
23
|
+
# end
|
24
|
+
|
25
|
+
puts "\n== Preparing database =="
|
26
|
+
system! "bin/rails db:prepare"
|
27
|
+
|
28
|
+
puts "\n== Removing old logs and tempfiles =="
|
29
|
+
system! "bin/rails log:clear tmp:clear"
|
30
|
+
|
31
|
+
puts "\n== Restarting application server =="
|
32
|
+
system! "bin/rails restart"
|
33
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require_relative "boot"
|
2
|
+
|
3
|
+
require "rails/all"
|
4
|
+
|
5
|
+
require "rails_twirp/engine"
|
6
|
+
|
7
|
+
# Require the gems listed in Gemfile, including any gems
|
8
|
+
# you've limited to :test, :development, or :production.
|
9
|
+
Bundler.require(*Rails.groups)
|
10
|
+
|
11
|
+
module Dummy
|
12
|
+
class Application < Rails::Application
|
13
|
+
config.load_defaults Rails::VERSION::STRING.to_f
|
14
|
+
|
15
|
+
# Configuration for the application, engines, and railties goes here.
|
16
|
+
#
|
17
|
+
# These settings can be overridden in specific environments using the files
|
18
|
+
# in config/environments, which are processed later.
|
19
|
+
#
|
20
|
+
# config.time_zone = "Central Time (US & Canada)"
|
21
|
+
# config.eager_load_paths << Rails.root.join("extras")
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# SQLite. Versions 3.8.0 and up are supported.
|
2
|
+
# gem install sqlite3
|
3
|
+
#
|
4
|
+
# Ensure the SQLite 3 gem is defined in your Gemfile
|
5
|
+
# gem 'sqlite3'
|
6
|
+
#
|
7
|
+
default: &default
|
8
|
+
adapter: sqlite3
|
9
|
+
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
|
10
|
+
timeout: 5000
|
11
|
+
|
12
|
+
development:
|
13
|
+
<<: *default
|
14
|
+
database: db/development.sqlite3
|
15
|
+
|
16
|
+
# Warning: The database defined as "test" will be erased and
|
17
|
+
# re-generated from your development database when you run "rake".
|
18
|
+
# Do not set this db to the same as development or production.
|
19
|
+
test:
|
20
|
+
<<: *default
|
21
|
+
database: db/test.sqlite3
|
22
|
+
|
23
|
+
production:
|
24
|
+
<<: *default
|
25
|
+
database: db/production.sqlite3
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require "active_support/core_ext/integer/time"
|
2
|
+
|
3
|
+
Rails.application.configure do
|
4
|
+
# Settings specified here will take precedence over those in config/application.rb.
|
5
|
+
|
6
|
+
# In the development environment your application's code is reloaded any time
|
7
|
+
# it changes. This slows down response time but is perfect for development
|
8
|
+
# since you don't have to restart the web server when you make code changes.
|
9
|
+
config.cache_classes = false
|
10
|
+
|
11
|
+
# Do not eager load code on boot.
|
12
|
+
config.eager_load = false
|
13
|
+
|
14
|
+
# Show full error reports.
|
15
|
+
config.consider_all_requests_local = true
|
16
|
+
|
17
|
+
# Enable/disable caching. By default caching is disabled.
|
18
|
+
# Run rails dev:cache to toggle caching.
|
19
|
+
if Rails.root.join("tmp", "caching-dev.txt").exist?
|
20
|
+
config.action_controller.perform_caching = true
|
21
|
+
config.action_controller.enable_fragment_cache_logging = true
|
22
|
+
|
23
|
+
config.cache_store = :memory_store
|
24
|
+
config.public_file_server.headers = {
|
25
|
+
"Cache-Control" => "public, max-age=#{2.days.to_i}"
|
26
|
+
}
|
27
|
+
else
|
28
|
+
config.action_controller.perform_caching = false
|
29
|
+
|
30
|
+
config.cache_store = :null_store
|
31
|
+
end
|
32
|
+
|
33
|
+
# Store uploaded files on the local file system (see config/storage.yml for options).
|
34
|
+
config.active_storage.service = :local
|
35
|
+
|
36
|
+
# Don't care if the mailer can't send.
|
37
|
+
config.action_mailer.raise_delivery_errors = false
|
38
|
+
|
39
|
+
config.action_mailer.perform_caching = false
|
40
|
+
|
41
|
+
# Print deprecation notices to the Rails logger.
|
42
|
+
config.active_support.deprecation = :log
|
43
|
+
|
44
|
+
# Raise exceptions for disallowed deprecations.
|
45
|
+
config.active_support.disallowed_deprecation = :raise
|
46
|
+
|
47
|
+
# Tell Active Support which deprecation messages to disallow.
|
48
|
+
config.active_support.disallowed_deprecation_warnings = []
|
49
|
+
|
50
|
+
# Raise an error on page load if there are pending migrations.
|
51
|
+
config.active_record.migration_error = :page_load
|
52
|
+
|
53
|
+
# Highlight code that triggered database queries in logs.
|
54
|
+
config.active_record.verbose_query_logs = true
|
55
|
+
|
56
|
+
# Debug mode disables concatenation and preprocessing of assets.
|
57
|
+
# This option may cause significant delays in view rendering with a large
|
58
|
+
# number of complex assets.
|
59
|
+
config.assets.debug = true
|
60
|
+
|
61
|
+
# Suppress logger output for asset requests.
|
62
|
+
config.assets.quiet = true
|
63
|
+
|
64
|
+
# Raises error for missing translations.
|
65
|
+
# config.i18n.raise_on_missing_translations = true
|
66
|
+
|
67
|
+
# Annotate rendered view with file names.
|
68
|
+
# config.action_view.annotate_rendered_view_with_filenames = true
|
69
|
+
|
70
|
+
# Use an evented file watcher to asynchronously detect changes in source code,
|
71
|
+
# routes, locales, etc. This feature depends on the listen gem.
|
72
|
+
# config.file_watcher = ActiveSupport::EventedFileUpdateChecker
|
73
|
+
|
74
|
+
# Uncomment if you wish to allow Action Cable access from any origin.
|
75
|
+
# config.action_cable.disable_request_forgery_protection = true
|
76
|
+
end
|