Browsermob Proxy + Selenium + Ruby setup giving 550 response - ruby-on-rails

So I am looking at trying to integrate Browsermob into a Ruby project so i can edit http responses.
I have been following the setup with Selenium instructions from the Github and another article I found about performance testing - which I also want to do.
For this, I have gone with the code from the performance testing article but the results I am getting are the same.
The code is
require 'selenium-webdriver'
require 'browsermob/proxy'
require 'rspec-expectations'
include RSpec::Matchers
require 'json'
def configure_proxy
proxy_binary = BrowserMob::Proxy::Server.new('my/path/to/browsermob-proxy-2.1.4/bin/browsermob-proxy')
proxy_binary.start
proxy_binary.create_proxy
end
def browser_profile
browser_profile = Selenium::WebDriver::Firefox::Profile.new
browser_profile.proxy = #proxy.selenium_proxy
browser_profile
end
def setup
#proxy = configure_proxy
#driver = Selenium::WebDriver.for :firefox, profile: browser_profile
end
def teardown
#driver.quit
#proxy.close
end
def capture_traffic
#proxy.new_har
yield
#proxy.har
end
def run
setup
har = capture_traffic { yield }
#har_file = "./selenium_#{Time.now.strftime("%m%d%y_%H%M%S")}.har"
har.save_to #har_file
teardown
end
run do
#driver.get 'http://the-internet.herokuapp.com/dynamic_loading/2'
#driver.find_element(css: '#start button').click
Selenium::WebDriver::Wait.new(timeout: 8).until do
#driver.find_element(css: '#finish')
end
end
performance_results = JSON.parse `yslow --info basic --format json #{#har_file}`
performance_grade = performance_results["o"]
performance_grade.should be > 90
Now, the problem I get is that as soon as I try to run this code (isolated away from my project or even within it) I get:
(.rvm/gems/ruby-2.5.3/gems/rest-client-2.0.2/lib/restclient/abstract_response.rb:220:in 'rescue in exception_with_response': HTTP status code 550 (RestClient::RequestFailed)
Does anyone know why I would be getting this? I understand that a 550 is an action not taken code but I'm confused as to why I would be getting this?
Any help would be VERY much appreciated!

Related

AWS lambda API ruby responding differently on postman

I am trying to execute some code using AWS lambda using ruby. The same code works well when using configured test event at the lambda function in AWS account, but when I send the same request using postman, it responds with 502 Bad Gateway response code.
When I watch cloudwatch logs, it gives an error message that is not possible on the code that is run.
require "payment_notification_response.rb"
Dir["payment_gateway/*.rb"].each {|file| require file }
class PaymentNotificationRequest
attr_accessor :headers, :body, :query_params, :http_method, :payment_gateway
PAYMENT_GATEWAYS = [::PaymentGateway]
def initialize(event)
self.headers = event["headers"] rescue {}
self.body = JSON.parse(event['body'].to_json, symbolize_names: true) rescue {}
self.query_params = event['queryStringParameters'] rescue {}
self.http_method = event["requestContext"]["http"]["method"] rescue {}
self.payment_gateway = self.find_payment_gateway_by_request
end
def find_payment_gateway_by_request
PAYMENT_GATEWAYS.each do |pg|
return pg if pg.new(self).is_the_requester?
end
end
def process
return self.payment_gateway.new(self).process
end
end
Below error is triggered at cloudwatch
{
"errorMessage": "undefined method `new' for [PaymentGateway]:Array",
"errorType": "Function<NoMethodError>",
"stackTrace": [
"/var/task/payment_notification_request.rb:23:in `process'",
"/var/task/lambda_function.rb:7:in `lambda_handler'"
]
}
How is this possible? Can anyone please help?

How to get output of dig <www.domain.com> +short using ruby code?

How do I get the output of a command like 'dig www.mlb.com +short' using Ruby? I realize I can simply do a system command via backticks, but is there a library that allows me to get the output without a system command?
dig www.mlb.com +short
Output:
www.mlb.com.edgekey.net.
e5991.dscg.akamaiedge.net.
104.77.230.52
I have used Resolv and DnsRuby, and can only seem to get one of the entries above. For example:
require 'dnsruby'
include Dnsruby
res = Resolver.new
ret = res.query('www.mlb.com', Types.CNAME)
ret.answer.map {|x| x.rdata.to_s}
only gives me the first line in the output
www.mlb.com.edgekey.net
should add the gem 'dnsruby'
gem 'dnsruby'
and bundle install
This is a sameple model that I created
class Dig
require 'dnsruby'
include Dnsruby
#Dig.single_resolvers
def self.single_resolvers
res = Resolver.new
res.single_resolvers.each {|s| print "Server address : #{s.server}\n"}
end
#Dig.query(url: 'www.mlb.com', kind: 'mx')
def self.query(url: 'www.mlb.com', kind: nil)
res = Resolver.new
if kind.nil?
ret = res.query(url) # Defaults to A record
else
ret = res.query(url, kind) # Defaults to A record
end
ret.answer
end
end

rails wisper under test

I have a project which used wisper https://github.com/krisleech/wisper to provide publisher and subscribers functionalities.
The gem works perfectly under development and production modes. However, when I try to add some tests for them (rake test:integration), the newly added tests refused to work. The publisher (maybe also the listener) in the tests mode stopped working anymore.
Core::Request.subscribe(Listener::Studentlistener, async: true)
Core::Request.subscribe(Listener::Tutorlistener, async: true)
I used the sidekiq as a async backend, i used wisper-sidekiq gem to handle the async requests, not sure if this would be the problem?
,puma as the server, MRI ruby 2.0.0
Do I have to a set up something in order for the test to run?
it "Student can get latest status after looking for xxx tutor" do
post api_v1_students_request_look_for_xxx_tutor_path,
{ subject: 'nothing' },
{ "AUTHORIZATION" => "xxx"}
expect(response).to be_success
get api_v1_students_status_path, nil,
{ "AUTHORIZATION" => "xxx"}
expect(response).to be_success
json_response = JSON.parse(response.body)
expect(json_response['state']).to eq('matching')
end
The listener should receive the publishing between these two posts and update the state to be "matching". However, now when I run rspec the test failed because the publisher never publish anything and hence the state is not updated correctly.
Even the authors are relying on some mocking/stubbing in the integrations tests, so that might be the correct way.
class MyCommand
include Wisper::Publisher
def execute(be_successful)
if be_successful
broadcast('success', 'hello')
else
broadcast('failure', 'world')
end
end
end
describe Wisper do
it 'subscribes object to all published events' do
listener = double('listener')
expect(listener).to receive(:success).with('hello')
command = MyCommand.new
command.subscribe(listener)
command.execute(true)
end
https://github.com/krisleech/wisper/blob/master/spec/lib/integration_spec.rb

Feedjira: VCR not recording cassetes

I have a very simple controller that grabs some data from rss using Feedjira. I want to test this controller by recording the RSS response. Here is the controller code:
def index
#news = Feedjira::Feed.fetch_and_parse URI.encode("http://news.google.com/news/feeds?q=\"#{query}\"&output=rss")
end
and my spec test:
it "should assign news feed", :vcr do
get :index
assigns(:news).entries.size.should == 6
assigns(:news).entries[0].title.should == "First item title"
end
and code for vcd config:
VCR.configure do |c|
c.cassette_library_dir = Rails.root.join("spec", "vcr")
c.hook_into :fakeweb
c.ignore_localhost = true
end
RSpec.configure do |c|
c.treat_symbols_as_metadata_keys_with_true_values = true
c.around(:each, :vcr) do |example|
name = example.metadata[:full_description].split(/\s+/, 2).join("/").underscore.gsub(/[^\w\/]+/, "_")
options = example.metadata.slice(:record, :match_requests_on).except(:example_group)
VCR.use_cassette(name, options) { example.call }
end
end
For some unknown reason, the VCR cassete is not being recorded in this specific test. All other tests that use web calls are working, but with this one with Feedjira it seems that vcr does not detects the network calls. Why?
According to Feedjira's home page, it uses curb, not Net::HTTP to make HTTP requests:
An important goal of Feedjira is speed - fetching is fast by using
libcurl-multi through the curb gem.
VCR can only use FakeWeb to hook into Net::HTTP requests. To hook into curb requests, you'll need to use hook_into :webmock instead.
As of this commit in Feedjira 2.0, Feedjira uses faraday, which means you can follow the testing guide in the Faraday readme or use VCR.
Feedjira uses VCR internally now too.
Example
For example you could use vcr in an rspec example like this,
it 'fetches and parses the feed' do
VCR.use_cassette('success') do
feed = Feedjira::Feed.fetch_and_parse 'http://feedjira.com/blog/feed.xml'
expect(feed.last_modified).to eq('Fri, 07 Oct 2016 14:37:00 GMT')
end
end

Test download of pdf with rspec and pdfkit

I am developing a rails 3.2 application with which users can download pdfs. I enjoy test driven development a lot using rspec and shoulda matchers, but I'm at a loss with this one.
I have the following code inside my controller:
def show_as_pdf
#client = Client.find(params[:client_id])
#invoice = #client.invoices.find(params[:id])
PDFKit.configure do |config|
config.default_options = {
:footer_font_size => "6",
:encoding => "UTF-8",
:margin_top=>"1in",
:margin_right=>"1in",
:margin_bottom=>"1in",
:margin_left=>"1in"
}
end
pdf = PDFKit.new(render_to_string "invoices/pdf", layout: false)
invoice_stylesheet_path = File.expand_path(File.dirname(__FILE__) + "/../assets/stylesheets/pdfs/invoices.css.scss")
bootstrap_path = File.expand_path(File.dirname(__FILE__) + "../../../vendor/assets/stylesheets/bootstrap.min.css")
pdf.stylesheets << invoice_stylesheet_path
pdf.stylesheets << bootstrap_path
send_data pdf.to_pdf, filename: "#{#invoice.created_at.strftime("%Y-%m-%d")}_#{#client.name.gsub(" ", "_")}_#{#client.company.gsub(" ", "_")}_#{#invoice.number.gsub(" ", "_")}", type: "application/pdf"
return true
end
This is fairly simple code, all it does is configure my PDFKit and download the generated pdf. Now I want to test the whole thing, including:
Assignment of instance variables (easy, of course, and that works)
The sending of data, i.e. the rendering of the pdf => And this is where I'm stuck
I have tried the following:
controller.should_receive(:send_data)
but that gives me
Failure/Error: controller.should_receive(:send_data)
(#<InvoicesController:0x007fd96fa3e580>).send_data(any args)
expected: 1 time
received: 0 times
Does anyone know of a way to test that the pdf is actually downloaded/sent? Also, what more things do you see that should be tested for good test coverage? E.g., testing for the data type, i.e. application/pdf, would be nice.
Thanks!
Not sure why you're getting that failure but you could instead test the response headers:
response_headers["Content-Type"].should == "application/pdf"
response_headers["Content-Disposition"].should == "attachment; filename=\"<invoice_name>.pdf\""
You asked for advice regarding better test coverage. I thought I'd recommend this: https://www.destroyallsoftware.com/screencasts. These screencasts have had a huge impact on my understanding of test-driven development -- highly recommended!
I recommend using the pdf-inspector gem for writing specs for PDF related Rails actions.
Here's an exemplary spec (which assumes the Rails #report action writes data about a Ticket model in the generated PDF):
describe 'GET /report.pdf' do
it 'returns downloadable PDF with the ticket' do
ticket = FactoryGirl.create :ticket
get report_path, format: :pdf
expect(response).to be_successful
analysis = PDF::Inspector::Text.analyze response.body
expect(analysis.strings).to include ticket.state
expect(analysis.strings).to include ticket.title
end
end

Resources