class RailsTwirp::IntegrationTest

Constants

DEFAULT_HOST
Response

Attributes

controller[R]
host[W]
host![W]
mount_path[W]
mount_path![W]
request[R]
response[R]

Public Class Methods

new(name) click to toggle source
Calls superclass method
# File lib/rails_twirp/testing/integration_test.rb, line 12
def initialize(name)
  super
  reset!
  @before_rpc = []
end

Public Instance Methods

app() click to toggle source
# File lib/rails_twirp/testing/integration_test.rb, line 60
def app
  RailsTwirp.test_app
end
before_rpc(&block) click to toggle source
# File lib/rails_twirp/testing/integration_test.rb, line 41
def before_rpc(&block)
  @before_rpc << block
end
host() click to toggle source
# File lib/rails_twirp/testing/integration_test.rb, line 18
def host
  @host || DEFAULT_HOST
end
https!(value = true) click to toggle source
# File lib/rails_twirp/testing/integration_test.rb, line 28
def https!(value = true)
  @https = value
end
https?() click to toggle source
# File lib/rails_twirp/testing/integration_test.rb, line 24
def https?
  @https
end
reset!() click to toggle source
# File lib/rails_twirp/testing/integration_test.rb, line 32
def reset!
  @request = nil
  @response = nil
  @host = nil
  @host = nil
  @https = false
  @mount_path = "/twirp"
end
rpc(service, rpc, request, headers: nil) click to toggle source
# File lib/rails_twirp/testing/integration_test.rb, line 45
def rpc(service, rpc, request, headers: nil)
  @request = request

  env = build_rack_env(service, rpc, request, headers)
  @before_rpc.each do |hook|
    hook.call(env)
  end

  status, headers, body = app.call(env)
  @response = decode_rack_response(service, rpc, status, headers, body)
  set_controller_from_rack_env(env)

  @response
end

Private Instance Methods

build_rack_env(service, rpc, request, headers) click to toggle source
# File lib/rails_twirp/testing/integration_test.rb, line 66
def build_rack_env(service, rpc, request, headers)
  env = {
    "CONTENT_TYPE" => request_content_type,
    "HTTPS" => https? ? "on" : "off",
    "HTTP_HOST" => host,
    "PATH_INFO" => "#{@mount_path}/#{service.service_full_name}/#{rpc}",
    "REQUEST_METHOD" => "POST",
    "SERVER_NAME" => host,
    "SERVER_PORT" => https? ? "443" : "80",
    "rack.url_scheme" => https? ? "https" : "http"
  }
  if headers.present?
    http_request = ActionDispatch::Request.new(env)
    http_request.headers.merge! headers
  end

  input_class = service.rpcs[rpc][:input_class]
  env["rack.input"] = StringIO.new(Twirp::Encoding.encode(request, input_class, request_content_type))
  env
end
decode_rack_response(service, rpc, status, headers, body) click to toggle source
# File lib/rails_twirp/testing/integration_test.rb, line 91
def decode_rack_response(service, rpc, status, headers, body)
  body = body.join # body is an Enumerable

  if status === 200
    output_class = service.rpcs[rpc][:output_class]
    Twirp::Encoding.decode(body, output_class, headers["Content-Type"])
  else
    Twirp::Client.error_from_response(Response.new(status, body, headers))
  end
end
request_content_type() click to toggle source
# File lib/rails_twirp/testing/integration_test.rb, line 87
def request_content_type
  Twirp::Encoding::PROTO
end
set_controller_from_rack_env(env) click to toggle source
# File lib/rails_twirp/testing/integration_test.rb, line 102
def set_controller_from_rack_env(env)
  @controller = ActionDispatch::Request.new(env).controller_class
end