How to check JSON API?

I am compiling a JSON API, which may or may not be built with Rails. I would like to make sure that the JSON API behaves as expected by testing it from a test client that only communicates via HTTP.

For example, a test client will send a request to a given URL to a test server, and then make sure that the response is a JSON string equal to what the specifications are expected. The response may also be an HTTP response code such as 401.

I am currently running tests with QUnit and jQuery.ajax. It works, but I wonder if there is a better option. Has anyone else done something like this?

Thanks!

+6
source share
10 answers

Check out the python query library. I used it precisely for this purpose, and it is amazing.

http://docs.python-requests.org/en/latest/index.html

+2
source

Any language with a good HTTP library and JSON library should do. Just select your favorite language and test structure and go for it. I would use RSpec and Rack :: Test , especially if the API is implemented in Ruby, but tastes change.

Edit: You mentioned that you want to run tests outside the server. If you are sure of this, use, for example, Net :: HTTP instead of Rack :: Test.

+6
source

Typically, APIs are regular controllers / actions. Thus, you can test this action as a regular controller inside the server application. That I am testing my API actions:

describe Api::AccountsController do describe "GET :index" do it "should be success" do do_get response.should be_success end it "should return list of accounts" do 5.times{ Factory :account } do_get JSON.parse(response.body).size.should == 5 end it "should return id, name and description of each account" do account = Factory :account do_get result = JSON.parse(response.body).first result['account']['id'].should == account.id result['account']['name'].should == account.name result['account']['description'].should == account.description end def do_get get :index, :format => :json end end end 
+3
source

I worked on the JS API for Rails and created a whole set of tests using QUnit and jQuery . I was very pleased with the end result, my goal was to thoroughly test the API and this combination of tools was all I needed .

I am working on a new project that uses Jasmine , its a little more descriptive in how you write tests (which makes business analysts happy), but the end result was almost the same as QUnit.

+1
source

Some seem to have found success with a cucumber (check this similar question)

What is the recommended test method for client JSON api server?

0
source

I totally agree with @mpartel.

Apparently you will need a set of unit tests (no HTTP complexity) to test the logic of your implementation. As soon as you go down this route, you will find that writing tests in one language will be useful.

For example, if you write it in Rails (and use Rack :: Test), you can also unit test to do many things in the simpler Test :: Unit (or RSpec) environment.

0
source

One way to do this is with rspec tests

 describe PersonApiController # GET /person/1.json it "should respond with a person" do person = Person.create(:name => "scott") get :show, :id => person.id, :format => 'json' response.should be_success response.body.should have_selector('name', :content => person.name) end end 

If you want to become even more attractive, you can share the api client through the plugin / gem between your client code and server code, and then initialize it with json, as

 person_client = PersonClient.new(response.body) person_client.name.should eql 'scott' 
0
source

Just write some specifications with Cucumber and / or RSpec to connect to your application and check the result. No need to drag jQuery into it.

0
source

http://www.soapui.org/

functional testing tool.

0
source

I did it! Here's how to do it with vanilla rails. Note the other type of accept mime. You can parse JSON if you want. Comparing like strings was enough.

  test "vote comment up" do # @request.headers["Content-Type"] = "application/json" @request.headers["Accept"] = "application/javascript" post :up, {post_id: 1, id: 1}, {user_id: 5} assert_response :success assert_equal @response.body, '{"error":true,"errorPath":"/users/5/balance","userVotes":1,"totalVotes":0}' end 

If you want to test during development, I use the Client-Client Advanced Client Extension .

0
source

Source: https://habr.com/ru/post/899965/


All Articles