How to send custom header with rack::test? - ruby

I spent about two days without success trying to send custom headers with Rack::Test. I just can't send any header into my app.
I found a lot of examples in the net with the similar code -- method( address, body, headers ), but for me they don't work at all.
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
# grep rack Gemfile.lock
rack (2.0.4)
rack-protection (2.0.1)
rack
rack-test (1.0.0)
rack (>= 1.0, < 3)
rack-protection (>= 1.5.0)
rack (~> 2.0)
rack-protection (= 2.0.1)
rack-test
The code in app (sinatra):
$log = Logger.new STDERR
class MyApi < Sinatra::Application
before do
$log.debug{ "Headers: #{ headers.keys.inspect }" }
end
get '/offers' do
#... some code
end
post '/offers' do
# .. some another code
end
end
spec/api_spec.rb
RSpec.describe MyApi, '/offers' do
include Rack::Test::Methods
def app
MyApi
end
context 'авторизация' do
it 'правильная - get с токеном' do
get '/offers', nil, {
'X-Auth' => 'some key'
}
$log.debug{ "ENV was: '#{ last_request.env.keys }'" }
end
it 'правильная - post с токеном' do
post '/offers', '', {
'Content-Type' => 'application/json; charset: utf-8',
'X-Auth' => 'some long key'
}
end
end
end
Output contains for both tests:
Headers: ["Content-Type"]
...
ENV was: '["rack.version", "rack.input", "rack.errors",
"rack.multithread", "rack.multiprocess", "rack.run_once",
"REQUEST_METHOD", "SERVER_NAME", "SERVER_PORT", "QUERY_STRING",
"PATH_INFO", "rack.url_scheme", "HTTPS", "SCRIPT_NAME", "CONTENT_LENGTH",
"rack.test", "REMOTE_ADDR", "X-Auth", "HTTP_HOST", "HTTP_COOKIE",
"sinatra.commonlogger", "rack.logger", "rack.request.query_string",
"rack.request.query_hash"]'

This code is working:
get '/offers', nil, { 'X-Auth' => 'long key' }
And this also is correct:
header 'X-Auth', 'some key'
get '/offers'
I changed the manner I look for that header in app:
request.get_header('HTTP_X_AUTH') || request.env['X-Auth']
The first - get_header was triggered when I call my app with curl, the last - request.env - when in tests.

You have to use Rack::Test::Methods#header (which delegates to Rack::Test::Session#header):
it 'правильная - get с токеном' do
header 'X-Auth', 'some key'
get '/offers'
$log.debug{ "ENV was: '#{ last_request.env.keys }'" }
end

Related

WebSocket and EventMachine timeout and error recovery

Using puma, faye-websocket-ruby and eventmachine, I am trying to implement a WebSocket server that is extended to support channels using redis.rb. Each client will supply a channel using a route currently in development as: "/C#{random number}". All of this logic needs to reside in the server, as the clients will be microprocessor-based Python systems that will not support higher-level libraries.
My code was based on ruby-websockets-chat-demo, as a starting point. One major change was to configure it to support multiple channels during WebSocket "on open".
The code is working when run normally. However, often when one client drops, the server hangs until it is restarted. I am trying to resolve that issue, but have not been able to do so so far. Initially, Heroku would throw an H12 timeout. I've implemented rack-timeout. I've tried rescuing timeouts within the server, but those never fire. I've implemented an "on error" event within the server but it never fires. Most often, the server just goes away until restarted. The client should fend for itself, but I need the server to recover and continue.
config.ru:
require './app'
require './middlewares/myserver_backend'
require 'rack-timeout'
use Rack::Timeout, service_timeout: 20, wait_timeout: 30, wait_overtime: 60, service_past_wait: false
use Myserver::MyserverBackend
run Myserver::App
Rack middleware "backend":
%w(faye/websocket thread redis json erb).each { |m| require m }
module Myserver
class MyserverBackend
KEEPALIVE_TIME = ENV['KEEPALIVE_TIME']
def initialize(app)
#app = app
#clients = []
#uri = URI.parse(ENV["REDISCLOUD_URL"])
#redis = Redis.new(host: #uri.host, port: #uri.port, password: #uri.password)
end
def call(env)
begin
if Faye::WebSocket.websocket?(env)
ws = Faye::WebSocket.new(env, nil, {ping: KEEPALIVE_TIME})
ws.on :open do |event|
channel = URI.parse(event.target.url).path[1..URI.parse(event.target.url).path.length]
Thread.new do
redis_sub = Redis.new(host: #uri.host, port: #uri.port, password: #uri.password)
redis_sub.subscribe(channel) do |on|
on.message do |message_channel, message|
puts "MyserverBackend>> Redis message received on channel:#{message_channel}; Message is:#{message};"
#clients.each { |clients_ws, clients_channel| clients_ws.send(message) if clients_channel == message_channel }
end
end
end
#clients << [ws, channel]
#clients.each do |clients_ws, clients_channel|
puts "MyserverBackend>> Client:#{clients_ws.object_id}; Channel:#{clients_channel};"
end
end
ws.on :message do |event|
#clients.each do |clients_ws, clients_channel|
if clients_ws == ws
puts "MyserverBackend>> Websocket message received on channel:#{clients_channel}; Message is:#{event.data};"
#redis.publish(clients_channel, sanitize(event.data))
end
end
end
ws.on :close do |event|
# Close all channels for this client first
# ws gives a channel which we use to identify it here, but we're closing all of those that are open
#clients.each { |clients_ws, clients_channel| #redis.unsubscribe(clients_channel) if clients_ws == ws }
#clients.delete_if { |clients_ws, clients_channel| clients_ws == ws }
channel = URI.parse(event.target.url).path[1..URI.parse(event.target.url).path.length]
puts "MyserverBackend>> Websocket closure for:#{channel}; Event code:#{event.code} Event reason:#{event.reason};"
ws = nil
end
ws.on :error do |event|
puts "Error raised:#{nil}; ws:#{ws.object_id};"
ws.close unless ws.nil?
end
# Return async Rack response
ws.rack_response
else
#app.call(env)
end
rescue Rack::Timeout::RequestTimeoutError, Rack::Timeout::RequestExpiryError => exception
puts "Exception raised:#{exception}; ws:#{ws.object_id};"
ws.close(code=4999, reason=9999) unless ws.nil?
# ensure is executed immediately so it doesn't help...
end
end
private
def sanitize(message)
json = JSON.parse(message)
json.each { |key, value| json[key] = ERB::Util.html_escape(value) }
JSON.generate(json)
end
end
end
The Sinatra "frontend":
# https://github.com/heroku-examples/ruby-websockets-chat-demo
require 'rubygems'
require 'bundler'
require 'sinatra/base'
ENV['RACK_ENV'] ||= 'development'
Bundler.require
$: << File.expand_path('../', __FILE__)
$: << File.expand_path('../lib', __FILE__)
Dir["./lib/*.rb", "./lib/**/*.rb"].each { |file| require file }
env = ENV['OS'] == 'Windows_NT' ? 'development' : ENV['RACK_ENV']
module Myserver
class App < Sinatra::Base
get "/" do
erb :"index.html"
end
get "/assets/js/application.js" do
content_type :js
#scheme = env == "production" ? "wss://" : "ws://"
erb :"application.js"
end
end
end
The test client:
# https://github.com/faye/faye-websocket-ruby/issues/52
# https://github.com/faye/faye-websocket-ruby
%w(bundler/setup faye/websocket eventmachine json).each { |m| require m }
Dir["./lib/*.rb", "./lib/**/*.rb"].each { |file| require file }
class ClientWs
def self.em_run
env = ENV['OS'] == 'Windows_NT' ? 'development' : ENV['RACK_ENV']
EM.run do
uri = 'myserver.herokuapp.com'
#uri = 'localhost' if env == 'development'
channel = "C#{rand(999999999999).to_s}"
url = uri == 'localhost' ? "ws://#{uri}:3000/#{channel}" : "ws://#{uri}/#{channel}"
#ws = Faye::WebSocket::Client.new(url)
start = Time.now
count ||= 0
timer = EventMachine.add_periodic_timer(5+rand(5)) {
count += 1
send({'PING': channel, 'COUNT': count.to_s})
}
#ws.on :open do |event|
puts "{'OPEN':#{channel}}"
ClientWs.send({'OPEN': channel})
end
#ws.on :message do |event|
#ip_address ||= Addrinfo.ip(URI.parse(event.target.url).host).ip_address
begin
parsed = JSON.parse event.data
rescue => e
puts ">>>> [Error! Failed to parse JSON]"
puts ">>>> [#{e.message}]"
puts ">>>> #{event.data}"
end
puts ">> #{#ip_address}:#{channel}:#{event.data};"
end
#ws.on :close do |event|
timer.cancel
stop = Time.now - start
puts "#{stop} seconds;"
p [:close, event.code, event.reason]
ws = nil
ClientWs.em_run
end
end
end
def self.send message
payload = message.is_a?(Hash) ? message : {payload: message}
#ws.send(payload.to_json)
end
end
ClientWs.em_run
The Gemfile.lock:
GEM
remote: https://rubygems.org/
specs:
activesupport (4.2.5.1)
i18n (~> 0.7)
json (~> 1.7, >= 1.7.7)
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
eventmachine (1.2.0.1-x86-mingw32)
faye-websocket (0.10.4)
eventmachine (>= 0.12.0)
websocket-driver (>= 0.5.1)
i18n (0.7.0)
json (1.8.3)
json_pure (1.8.3)
minitest (5.9.0)
multi_json (1.12.1)
oj (2.16.1)
permessage_deflate (0.1.3)
progressbar (0.21.0)
puma (3.4.0)
rack (1.6.4)
rack-protection (1.5.3)
rack
rack-timeout (0.4.2)
rake (11.2.2)
redis (3.3.0)
rollbar (2.11.5)
multi_json
sinatra (1.4.7)
rack (~> 1.5)
rack-protection (~> 1.4)
tilt (>= 1.3, < 3)
thread_safe (0.3.5)
tilt (2.0.5)
tzinfo (1.2.2)
thread_safe (~> 0.1)
websocket-driver (0.6.4)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.2)
PLATFORMS
x86-mingw32
DEPENDENCIES
activesupport (= 4.2.5.1)
bundler
faye-websocket
json_pure
oj (~> 2.16.0)
permessage_deflate
progressbar
puma
rack
rack-timeout
rake
redis (>= 3.2.0)
rollbar
sinatra
RUBY VERSION
ruby 2.2.4p230
BUNDLED WITH
1.12.5
What client sees when attempting to connect to stalled server:
ruby client.rb
20.098119 seconds;
[:close, 1002, "Error during WebSocket handshake: Unexpected response code: 500"]
20.07921 seconds;
[:close, 1002, "Error during WebSocket handshake: Unexpected response code: 500"]
20.075731 seconds;
[:close, 1002, "Error during WebSocket handshake: Unexpected response code: 500"]
config/puma.rb:
env = ENV['OS'] == 'Windows_NT' ? 'development' : ENV['RACK_ENV']
if env.nil? || env == 'development' || env == 'test'
concurrency = 0 # Set to zero to ensure single mode, not clustered mode
max_threads = 1
end
# WEB_CONCURRENCY and RAILS_MAX_THREADS == 1 in Heroku for now.
concurrency ||= (ENV['WEB_CONCURRENCY'] || 2)
max_threads ||= (ENV['RAILS_MAX_THREADS'] || 5)
worker_timeout 15
workers Integer(concurrency)
threads_count = Integer(max_threads)
threads threads_count, threads_count
#preload_app!
rackup DefaultRackup
port ENV['PORT'] || 3000
environment ENV['RACK_ENV'] || 'development'
What I needed to do was complete the server's "on close" event. It needed to clean everything up and then restart itself, which it was not doing.
I don't like this as the final answer, however. The question would be, why is the server closing up shop, terminating and restarting just because a client dropped? Isn't there a cleaner way to sweep away the detritus of a failed client? Follow up: This fix does answer this particular question, in any case, in that completing onclose resolved the stated problem. Further enhancements threaded the client's WebSocket events in addition to the Redis events such that onclose only closes the client and not the server.
The new event is:
ws.on :close do |event|
if #debug
puts "MyserverBackend>> Close entered. Last error:#{$!.class}:#{$!.to_s};Module:#{$0};Line:#{$.};"
$#.each { |backtrace| puts backtrace }
exit
end
#clients.each do |clients_ws, clients_channel|
begin
#redis.unsubscribe(clients_channel)
rescue RuntimeError => exception
unless exception.to_s == "Can't unsubscribe if not subscribed."
raise
end
false
end
end
#clients.delete_if { |clients_ws, clients_channel| clients_ws == ws }
channel = URI.parse(event.target.url).path[1..URI.parse(event.target.url).path.length]
puts "MyserverBackend>> Websocket closure for:#{channel}; Event code:#{event.code} Event reason:#{event.reason};"
ws = nil
app = Myserver::App
myserver = MyserverBackend.new(app)
myserver
end

Sinatra error with dashingio and rest-client

I'm using rest-client in a dashingio job. Since adding code for rest-client I get the following error on dashing start
/Users/emcevoy/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/sinatra-1.4.6/lib/sinatra/base.rb:1595:in `define_method': tried to create Proc object without a block (ArgumentError)
Gemfile
source 'https://rubygems.org'
gem 'dashing'
## Remove this if you don't need a twitter widget.
gem 'twitter', '>= 5.9.0'
myjob.rb
require "rubygems"
require "json"
require "rest-client"
require "requests"
require "pp"
...
def post(theJson)
theUrl = $location + $endpoint.to_str
begin
response = RestClient.post(theUrl, theJson, :content_type => :json)
if(response.code > 204)
$stderr.puts("Failed to post Json to endpoint " + theUrl + "\nReturn code: " + response.code.to_s)
exit 1
end
rescue Errno::ECONNREFUSED
$stderr.puts("Server refusing connection!")
exit 1
end
if(JSON.parse(response).has_key?("error")) then
$stderr.puts("Returned an error...")
$stderr.puts JSON.parse(response)["error"]
exit 1
end
return(response)
end

How to use Slim (or haml) directly with Rack?

In Sinatra, it's pretty easy to render a slim template upon request:
get '/some_request' do
slim :file_name
end
Since Rack expects a class with a .call method which then returns [status, headers, [body]] array, like:
class RequestManager
def call(env)
return [200, {}, ['why am I in an array?']]
end
end
How do I return the rendered slim template to make Rack happy?
e.g. [200, {}, '<html><head></head><!-- you get the idea --></html>']
You can accomplish this by including the haml gem in your config.ru,
then using
require 'haml' # or put haml in your gemfile and start rack with `bundle exec`
# ...
run lambda {|env|
template = File.read(path)
page = Haml::Engine.new(template).render()
}
# ...
to set your page attribute from your template.

Verify not working in Ruby with Selenium::WebDriver

I am just starting to figure how to create unit tests using "test/unit". I copied the code generated by Selenium IDE and paste it into my Ruby test method.
But when running it with Ruby.exe, for some reason it is throwing an error:
Finished tests in 31.835891s, 0.0314 tests/s, 0.0942 assertions/s.
1) Error:
test_method(MyTestClass):
NameError: uninitialized constant Test::Unit::AssertionFailedError
teste-noticia.rb:30:in `rescue in verify'
teste-noticia.rb:29:in `verify'
teste-noticia.rb:42:in `test_method'
1 tests, 3 assertions, 0 failures, 1 errors, 0 skips
Anyone could help me to how assert correctly desired strings? Any good practice is welcome ;-).
Here is the code:
# encoding: utf-8
require "selenium-webdriver"
require "test/unit"
class MyTestClass < Test::Unit::TestCase
def setup
#driver = Selenium::WebDriver.for :firefox
#base_url = "http://www.yoursite.com"
#driver.manage.timeouts.implicit_wait = 30
#verification_errors = []
#wait = Selenium::WebDriver::Wait.new :timeout => 10
end
def teardown
#driver.quit
assert_equal [], #verification_errors
end
def element_present?(how, what)
#driver.find_element(how, what)
true
rescue Selenium::WebDriver::Error::NoSuchElementError
false
end
def verify(&blk)
yield
rescue Test::Unit::AssertionFailedError => ex
#verification_errors << ex
end
#your test methods go here
def test_method
#driver.get(#base_url + "/my-desired-path")
verify { assert_equal "Obama wins and will move U.S. forward", #driver.find_element(:css, "h1").text }
end
end
EDIT
My local gems:
C:\Users\wmj>gem list
*** LOCAL GEMS ***
addressable (2.3.2)
bigdecimal (1.1.0)
childprocess (0.3.6)
ffi (1.1.5 x86-mingw32)
io-console (0.3)
json (1.5.4)
libwebsocket (0.1.5)
minitest (2.5.1)
multi_json (1.3.7)
rake (0.9.2.2)
rdoc (3.9.4)
rubyzip (0.9.9)
selenium-webdriver (2.26.0)
test-unit (2.5.2)
I believe the issue is that you have required the 'minitest' gem, but are trying to use the classes in the 'test-unit' gem. 'Minitest' is installed by default in Ruby 1.9 instead of 'Test-Unit' (which was installed by default in 1.8). Minitest is only partially backwards compatible with Test-Unit.
Possible solutions:
Switch to Minitest:
It is the Test::Unit::AssertionFailedError in the verify method that is causing the exception. You could change it to the minitest equivalent, which appears to be MiniTest::Assertion. So your verify method would become:
def verify(&blk)
yield
rescue MiniTest::Assertion => ex
#verification_errors << ex
end
Use Test-Unit instead of Minitest:
Assuming you have the test-unit gem already installed (gem install test-unit), manually specify that you want to use that gem when doing require 'test/unit':
gem "test-unit"
require "test/unit"

Getting a wrong argument type RSpec::Matchers::Matcher (expected Proc) error in Rspec when testing responses in Rails

I am trying to test my Rails 3.0.9 controller with Rspec 2.6.4 and Webrat 0.7.3. My controller looks like this:
#metrics_controller.rb
class MetricsController < ApplicationController
def show
#metric = Metric.all(:msrun_id => params[:id]).first
end
def index
#metrics = Metric.all
end
end
And my controller spec looks like this:
#metrics_controller_spec.rb
require 'spec_helper'
describe MetricsController do
describe "GET #index" do
it "should be successful" do
get :index
response.should be_success
end
end
describe "GET show" do
it 'contains an overview of a metric' do
get :show, :id => 1
response.should have_selector('title', :content => "Metric Overview")
end
end
end
This looks very similar to other examples I have seen in documentation, but when I run bundle exec rspec spec/controllers/metrics_controller_spec.rb I am getting some strange errors:
1) MetricsController GET #index should be successful
Failure/Error: response.should be_success
TypeError:
wrong argument type RSpec::Matchers::BePredicate (expected Proc)
# ./spec/controllers/metrics_controller_spec.rb:8
2) MetricsController GET show contains an overview of a metric
Failure/Error: response.should have_selector('title')
TypeError:
wrong argument type Webrat::Matchers::HaveSelector (expected Proc)
# ./spec/controllers/metrics_controller_spec.rb:16
It looks like something weird is going on with the response.should method. If I change the first example to something more verbose that doesn't call should on response like this:
response.success?.should == true
then the example works fine, but why would should be expecting a Proc? Any ideas about how I can fix this?
This is not an especially helpful answer, but I will put it in here in case someone else gets stuck on the same thing. I inherited the project from someone else, and they set it up to use both railties and rails. Changing the Gemfile to look like this:
source 'http://rubygems.org'
RAILS_VERSION = '~> 3.0.7'
DM_VERSION = '~> 1.1.0'
gem 'railties', RAILS_VERSION, :require => 'rails'
gem 'activesupport', RAILS_VERSION
gem 'actionpack', RAILS_VERSION
gem 'actionmailer', RAILS_VERSION
gem 'dm-rails', DM_VERSION
gem 'rspec-rails'
#other gems below
Instead of something like this:
source 'http://rubygems.org'
gem 'rails'
gem 'dm-rails', '~> 1.1.0'
gem 'rspec-rails'
#other gems below
along with changing the config/application.rb to require the railties instead of rails seemed to fix it. The key seemed to be using railties instead of all of rails along with dm-rails.

Resources