Rails Rest API External Testing - ruby-on-rails

I am building a REST Web Service layer on top of a Rails app that will be used by an Iphone application. The response format is XML.
I would like to build some acceptance tests that should be external to the rails stack (and should test everything, including the http server). The test scenarios are quite complex, involving the process of searching/posting/reviewing an order. What would be the best solution to accomplish this?
a. Ruby script using curl/curb to fetch the request and Hpricot to parse the request
b. Selenium
c. ..
It would also be nice that those tests could be used as integration tests (therefore, run on every git commit). What integration solution would you recommend?
a. Integrity
b. CruiseControl
c. something else

I've used three approaches over this last few years
Active-resource
I found this to be too concerned with looking like active-record to be a great solution. In some cases I had to patch parts of it to work as I'd like a REST client to behave.
Rest-client
This gem is very good - well documented and does works as expected. I combined this with my own simple DSL and it's worked out better than a generic testing framework
XML over HTTP
I use this for performance testing. Very flexible but the learning curve is higher than Rest-client. If you go down this approach you could use the Net::HTTP core class or the HTTParty gem (I haven't tried this but it looks great>
A really good resource is this Net::HTTP cheat-sheet
For ad-hoc testing I've also found the Rest Client add-in for Firefox very useful.

Use selenium-rc in ruby mode and you'll be a happy camper. Webrat/Cucumber already do this for you so you can just put that in a second project and run the tests that way, all you'll have to do is override the host (so instead of localhost you'll be using your domain).
As to CI I'm afraid I don't know the best one.

you can't possibly mean mks integrity...if so, the answer is anything but. CC is a good CI tool. really good.

Related

Splitting up a Rails and React app into separate Docker containers?

I have an app with a Rails backend and a React frontend. I am deploying it in Docker containers: one for the app, one for postgres, and one as a data volume container. I have it working, but the app image file is huge (3Gb!) and takes a long time to build.
I'd love a way to split it up. The React app needs a bunch of Node packages, but only for development; once it's all webpack-ed the React app is essentially static files. And the Rails app doesn't need Node at all.
I don't need all the development-time tooling in the production image, but as it is, I feel like I need to have it all in the same codebase so I can (eventually) set up a CI/CD environment that can build the app and run all the tests. Is there a way to do this such that I'd have a container for the React/Node app and a container for Rails, and connect them at runtime?
I think that you may have found the answer to your question already - split the code bases.
We all have some kind of knee-jerk reflex to want to keep everything in a project in the same repo. It feels safe. Dealing with separate repos seems quite scary, but so does not moshing CSS and JS into HTML for most beginners.
I feel like I need to have it all in the same codebase so I can
(eventually) set up a CI/CD environment that can build the app and run
all the tests
Well that would be nice - however testing javascript through Ruby or automated browsers is painfully slow. You end up with a "fast" suite of unit tests and a slow "suite" of integration tests that take 15+ minutes.
So whats the alternative?
Your API and your SPA application (angular) actually do very different things.
The API takes HTTP requests and poops out JSON. It runs on a Ruby on Rails server and talks with a database and even other API's.
You would do integration tests of you API by sending HTTP requests and testing the response.
Your API should not really care if the request comes from a Fuzzle widget and renders a happy face or not. Its not the API's job.
RSpec.describe 'Pets API' do
let!(:pet) { create(:pet) }
let(:json) { JSON.parse(response.body) }
describe 'GET /pets' do
get '/pets'
expect(json["name"]).to eq pet.name
end
end
The SPA server basically just needs to serve static HTML and just enough javascript to get stuff rolling.
A docker container seems almost overkill here - you just want a nginx server behind a reverse proxy or a load balancer as you're only serving up one thing.
You should have tests written in javascript that either mock out the API server or talk to a fake API server. If you really have to you could automate a browser and let it talk to test version of the API.
Your SPA will most likely have its own JS based toolkit and build process, and most importantly - its own test suite.
Of course this highly opinionated, but think about it - both projects will benefit from having their own infrastructure and a clear focus. Especially the API part which can end up really strange if you start building it around a user interface.
You can take a look to my rails react project at github.com/yovasx2/aquacontrol
Don't forget to start and fork it

JSON API integration testing framework that is technology agnostic

I am developing in a micro services architecture, currently each service is developed in ruby.
One of the advantages of decoupling services is a future ability to refactor a service from ruby to another technology, let's say Node.js
When I will do this refactor some time in future, I would want my integration tests to keep functioning.
Ideally, I would want to develop the integration tests in rspec (ruby), and to keep them functioning on a non-rails server via HTTP.
Is that possible with rspec?
Which tool can provide this requirement?
RSpec is a behavior-driven development (BDD) framework for the Ruby programming language. It is used for unit testing. It can;t be integrated with another tech stack
However, If it is all about doing the integration testing for your web services, I think Cucumber is something that will help you to achieve the same.
Cucumber as a tool is technology agnostic. However, you will have to design your integration tests in such a way that any helper libraries to execute or write the testcases could be easily replaced with the libraries of other language when you are changing you tech. stack for the webservices/test cases.
As long as you test the endpoints exposed by the webservice and not how they are implemented , they should keep functioning.
You could achieve that by writing your integration tests in spock which is a beautiful BDD framework in groovy, using that you can hit the endpoints like
GET /cars ---> Check for the list of cars
POST /cars ---> Add a car
GET /cars/1 ---> Get a specific car details
PUT /cars/1 ---> Edit a specific car
Now when you are testing like this, it doesn't matter what the implementation is coz you are always testing the interface.

PayPal integration. C# POST vs. WSDL

I got the PayPal integration working well using plain old HTTP POST using C# & .NET 2/3.5. I also get all transaction details in the response.
So, if I want to use WSDL (SOAP), will there be any advantage? (you can assume I know how to use web services)
Also, are there any examples on a complete C# project using this method? I already looked at http://www.codeproject.com/Articles/42894/Introduction-to-PayPal-for-C-ASP-NET-developers
but that page tells very little about how to make a fully functional transaction using WSDL.
Any ideas?
Or "if it ain't broke, don't fix it"
thanks!
I really don't work from WSDL's very often because I'm primarily a PHP developer. I have worked with them in Adobe Flex, though, and I gotta say I liked what I saw.
Basically, I was able to hook the WSDL up in Flex (which you can do in other IDE's as well) and it automatically gave me access to all of the API calls in the system. I could see all of the possible requests (functions) available to the web service as well as how to build them without even referring to much documentation.
When building HTTP requests directly (NVP/XML/JSON/etc) you gotta refer to documentation quite a bit to see how to build the request, and there's typically more trial and error involved, too, until you get things working. The WSDL helps you get around that, although, in reality you'll probably still be referring quite a bit to documentation.
The WSDL/SOAP tools I've used with PHP don't work nearly as nice as Flex or Visual Studio from what I've seen, so I typically stick to custom class libraries that build my requests for me. when I'm working in other platforms that utilize WSDL's a little nicer, though, I definitely prefer it.

Test integration between two rails apps

I am developing a system composed of two different rails applications (server and client) which communicate via rest web services.
I have tests for each app individually, but I would like to add some test for the integration between the two platforms, to assert that one creates a request compatible with what the other is expecting.
Any hints would be appreciated.
I have a similar architecture and we are using VCR in order to record all server side responses and avoid make requests always. It could turn annoying and right now I'm looking for a way to clean data from server after every request
I think VCR could be a good start point in order to test integration between your apps.
You can find documentation here -> Relish Docs
I think there could be several approaches here, depending what you have implemented..
If the client Rails app has user interface, try to write Selenium tests to perform the integration test in your local dev environment or a staging environment that has both deployed. (not ideal if the interface is still a prototype, changing frequently...)
Maybe part of the client can be written as a Ruby gem (e.g. the communication rest api is a ruby gem). When the server app in testing environment, the server Rails app can use the Client gem to run integration test, i.e. call the module function, the same function is used by client. The client Rails app can also use the gem to make requests to the server. Here's a good guide to start migrating some of your reusable code to rubygem.

Receiving REST response on local machine

I use a web service to convert files. The service returns the converted file as an HTTP POST, along with identifier data. My app receives the response, updates its database and saves the file to the appropriate location.
At least that's the idea, but how do I develop and test this on a local machine? Since it isn't publicly facing, I can't provide a directive URL. What's the best way to handle this? I want to keep the process as clean as possible, and the only ideas I can come up with have seemed excessively kludgey.
Given how common REST API development is, I assume there are well-established best practices for this. Any help appreciated.
The solution will change a bit depending on which server your using.
But the generally accepted method is using the loopback address: 127.0.0.1 in place of a fully qualified domain name. Your server may need to be reconfigured to listen on this IP address, but that's usually a trivial fix.
example: http://127.0.0.1/path/to/resource.html
You can use curl or even your browser if your application has a proper frontend. There are many other similar tools to test this from a command line, and each language has a set of libraries for establishing http connections and transferring data along them.
If your machine isn't accessible to the service you are using, then your only option would really be to build a local implementation of the service that will exercise your API. A rake task that sends the POST with the file and the info would be a nice thing so you could start your rails app locally, and then kick off the task with some params to run your application through its paces.
This is the case any time you are trying to develop a system that can't connect to a required resource during development. You need to build a development harness of sorts so that you can exercise all the different types of actions the external service will call on your application.
This certainly won't be easy or straight forward, especially if your interface to this external service is complicated. Be sure to have your test cases send bad POSTs to your application so that you are sure you handle both what you expect, and what you don't.
Also make sure that you do some integration testing with the actual service before you "go-live" with the application. Hopefully you can deploy to an external server that the web service will be able to access in order to test. Amazon's EC2 hosting environment would let you set up a server very quickly, run your tests, and then shut down without much cost at all.
You have 2 options:
Set up dynamic dns and expose your app to the outside world. This only works if you have full control over your network.
Use something like webrat to fake the posts to your app. Since it's only 1 request, this seems pretty trivial.
Considering that you should be writing automated tests for this, I'd go with #2. I used to do #1 when developing facebook apps since there was far to many requests to mock them all out with webrat.
If your question is about testing, why don't you use mocks to fake the server? It's more elegant than using Webrat, and easier to deploy (you only have one app instead of an app and a test environment).
More info about mocks http://blog.floehopper.org/presentations/lrug-mock-objects-2007-07-09/
You've got some info about mocks with Rspec here http://rspec.info/documentation/mocks/

Resources