How do you access data from a VCR cassette for testing? - ruby

I'm using the VCR gem and I've figured out how to record an API request to a cassette in JSON format. Now how do I access the information in the body of the response from that cassette?

You can use the following code to access the raw_cassette_bytes method which returns all the data from the VCR cassette:
VCR.use_cassette('path/to/cassette') do |cassette|
puts cassette.send(:raw_cassette_bytes)
end
I have used this when trying to make sure that the ERB template for a VCR cassette looks correct and is generated correctly.

Related

Ruby tests with spec and capybara xml post

I am trying to figure out how to send a xml http request (validated by request.xhr?) using page.driver.post() method.
Have you any suggestions? Running out of ideas :(.
Thanks a lot
If you're doing XML http requests then you're doing API testing which you really shouldn't be using Capybara for.
I got it, it is possible to pass it like this.
page.driver.post("/",{"HTTP_X_REQUESTED_WITH" => "XMLHttpRequest"})

How to read data sent by HTTParty in Sinatra

I am trying to have two separate Ruby programs to communicate, one with the Sinatra gem, and another with the HTTParty gem. What I am attempting to do is post data to the Sinatra program from a HTTP post request in the other program.
This is the code that sends the data.
HTTParty.post('https://notgivingawaymydomain/post_data', {something: foo})
However, I don't know how to receive the data on the other end. I've tried a few things I researched on the internet but none seem to work.
EDIT
My code on the other end is really nothing special at the moment, but I'll put what I've been trying.
post '/post_data' do
#not sure how to access the hash that my post request sent here
end
The post data is available in the params object inside your Sinatra route:
post '/post_data' do
data = params["something"] # => my_value is now 'foo' in this example
#... rest of code
end
Sinatra only parses the data if it is application/x-www-form-urlencoded (which it is in this case) or multipart/form-data. If you wanted to POST another type (e.g. JSON) you would need to parse the request body yourself using request.body:
post '/json_data'
# request.body is an IO object
data = JSON.parse(request.body.read)
end
Be sure to get the syntax of your HTTParty call correct. You need to specify the body: key of the options:
HTTParty.post('http://localhost:4567/post_data', body: {something: "foo"})
if you want to post data via HTTP you need a web-server on the receiving end.
sinatra describes itself as Sinatra is a DSL for quickly creating web applications in Ruby with minimal effort
so in itself it is not a webserver, but it can be run through a webserver to process the data that you post via HTTP. it is bundled with a webserver, so you can start it right away.
if you look at the sinatra README it says
It is recommended to also run gem install thin, which Sinatra will pick up if available.
thin is a reliable webserver that is recommended to run sinatra web applications.

Request only the header in http call

Is there a ruby library, with which I can request the web server to return only the header response and no content? This will help me speed up a script in which all I care is the response code.
I am now using this
Net::HTTP.get_response(URI(url))
but the server generates all the assert files and so on, which I do not want.
You might use rest-client gem and in particular head method.
http://www.rubydoc.info/github/rest-client/rest-client/RestClient#head-class_method
This can be done using 'Net::HTTP::Head' in the net-http library. other libraries also support it, just remember to look for the HEAD method instead of the Get method

Rails: Stub client-side ajax call in feature spec

I'm writing an rspec feature spec to test a file upload page that sends files directly from the client to S3 using the s3_direct_upload gem. Instead of hitting S3, I'd like to stub out that link and return the correct response so I can test the behavior after a failed/successful ajax request.
I think my issue lies in the fact that I can't use WebMock to stub requests, since the request isn't fired from the server (it's happening on the client).
Changing the test type/framework
I could add a new testing dependency like Jasmine or Sinon and test the js/page directly, but that still leaves me with feature specs that are trying to hit S3.
Changing the url
All of the bucket/path information for the link to S3 is bundled with the gem, so I would have to patch it in the test env, which I'd like to avoid.
Question
How I can run a feature spec that stubs out a client-side ajax request?

How to mock aws-sdk gem?

I have some code that uploads a file to Amazon S3, using the aws-sdk gem. Apparently it does an HTTP put to upload the file.
Is there a good way to mock this functionality of the aws-sdk gem?
I tried using Webmock, but the aws-sdk gem seems to do a get latest/meta-data/iam/security-credentials/ first. It seems that using Webmock may not be the best way to mock this functionality.
Working in RSpec.
If you're using version 2 of the aws-sdk gem try adding:
Aws.config.update(stub_responses: true)
to your RSpec.configure block (usually found in your rails_helper.rb file)
While the above works, it will return empty responses if you don't further specify response content - not necessarily valid, but stubbed.
You can generate and return stubbed response data from a named operation:
s3 = Aws::S3::Client.new
s3.stub_data(:list_buckets)
#=> #<struct Aws::S3::Types::ListBucketsOutput buckets=[], owner=#<struct Aws::S3::Types::Owner display_name="DisplayName", id="ID">>
In addition to generating default stubs, you can provide data to apply to the response stub.
s3.stub_data(:list_buckets, buckets:[{name:'aws-sdk'}])
#=> #<struct Aws::S3::Types::ListBucketsOutput buckets=[#<struct Aws::S3::Types::Bucket name="aws-sdk", creation_date=nil>], owner=#<struct Aws::S3::Types::Owner display_name="DisplayName", id="ID">>
For more details refer to: http://docs.aws.amazon.com/sdkforruby/api/Aws/ClientStubs.html
There are a lot of ways to mock requests in the AWS SDK for Ruby. Trevor Rowe recently posted an article on using the SDK's native support for object stubbing, which does not require any external dependencies like Webmock. You can also use tools like VCR (link will send you to another blog post) to build cacheable integration tests; this way you can test against the live service when you want accuracy and avoid hitting network when you want speed.
Regarding the get request on latest/meta-data/iam/security-credentials/, this happens because the SDK is trying to look up credentials, and, if none are provided, it will check if you are running on an EC2 instance as a last resort, causing the SDK to make an extra HTTP request. You can avoid this check by simply providing bogus static credentials, though if you are using something like VCR, you will want to provide valid credentials for the first run. You can read about how to provide static credentials in another blog post that Trevor wrote on credential management (this should also be in the developer guide and SDK documentation).

Resources