It would be a tedious task if you were to test restful services by hand. Of course, there are some browser plugins that can help you with manual testing through a visual interface, such as postman, rest console, but every time the system version is updated, you need to perform a lot of manual testing, which is obviously not very practical. Ruby has a lot of great gem packs that you can use to do this boring work. Restclient is one of my favorite. The RSPEC framework, combined with Ruby, uses Restclient to write very powerful test scripts. If GitHub wants you to test their restful API. The first thing you might want to do is make sure that the endpoint returns your expected response code. Before you start, you need to make sure that you have installed the appropriate gems. The most convenient way is to use Bundler installation:
1 " https://rubygems.org " 2 3 ' rest-client ' 4 ' RSpec ' 5 ' json_expressions '
In cmd (WINDOWS,LINUX/MAC environment please fix it yourself) go to the same level directory where you create the Gemfile file, run the ' bundle ' command
1 rafs-computer:rafael$ bundle 2 using Diff-lcs 1.2.5 3 using Json_expressions 0.8.3 4 using Mime-types 2.3 5 using NETRC 0.7.7 6 using rest-client 1.7.2 7 using RSPEC-SU Pport 3.1.1 8 using Rspec-core 3.1.4 9 using rspec-expectations 3.1.2 using Rspec-mocks 3.1 .2 using RSpec 3.1. 0 using bundler 1.7.3 is complete!
Now let's verify that we get a 200 response from the user terminal:
1Require'RSpec'2Require'rest_client'3 4Describe'GitHub API' Do5 6It'should return information about a user' Do7result = Restclient.get'https://api.github.com/users/rest-client',: Content_Type =: JSON,: Accept + =: JSON8Expect (Result.code). to EQ (200)9 EndTen OneEnd
Executing rspec-f doc filename at the command line
So the return code is right, but how do we know that the JSON returned is also true?
There are several different ways to verify. One way is to parse the JSON in the body (made up of key,value) and then create an assertion for each key you want to check. This can be used, but it requires you to write multiple assertions and is more difficult to maintain. Another method is to compare the data file with a known valid JSON format. You can use the Json_expressions gem package to do this thing. The following example is the same spec file. Added a testcase to validate JSON data.
Prepare a Users.json file first
1 {2 "Login":"rest-client",3 "ID": 2386701,4 "Avatar_url":"https://avatars.githubusercontent.com/u/2386701?v=3",5 "gravatar_id":"",6 "URL":"https://api.github.com/users/rest-client",7 "Html_url":"https://github.com/rest-client",8 "Followers_url":"https://api.github.com/users/rest-client/followers",9 "Following_url":"Https://api.github.com/users/rest-client/following{/other_user}",Ten "Gists_url":"https://api.github.com/users/rest-client/gists{/gist_id}", One "Starred_url":"Https://api.github.com/users/rest-client/starred{/owner}{/repo}", A "Subscriptions_url":"https://api.github.com/users/rest-client/subscriptions", - "Organizations_url":"Https://api.github.com/users/rest-client/orgs", - "Repos_url":"Https://api.github.com/users/rest-client/repos", the "Events_url":"Https://api.github.com/users/rest-client/events{/privacy}", - "Received_events_url":"https://api.github.com/users/rest-client/received_events", - "type":"Organization", - "Site_admin": false, + "name":"rest-client Team", - " Company": null, + "Blog":"", A " Location": null, at "Email": null, - "hireable": false, - "Bio": null, - "Public_repos": 1, - "public_gists": 0, - "followers": 0, in "following": 0, - "Created_at":"2012-09-20t15:01:43z", to "Updated_at":"2015-03-11t19:08:01z" +}
Then write the test case spec file
1Require'RSpec'2Require'rest_client'3Require'Json_expressions/rspec'4 5 6Describe'GitHub API' Do7 8It'should return to asking information about a user' Do9result = Restclient.get'https://api.github.com/users/rest-client',: Content_Type =: JSON,: Accept + =: JSONTenExpect (Result.code). to EQ (200) One End A -It'should return proper data for a user' Do -Expected_data = Json.parse (Io.read ('Users.json')) theresult = Restclient.get'https://api.github.com/users/rest-client',: Content_Type =: JSON,: Accept + =: JSON - expect (result) to Match_json_expression (Expected_data) - End - +End
This Users.json file contains a known response. As you may have guessed, some of these service return values can change very quickly. For example, "Updated_at" is a key that the return value may change frequently. If you just want to verify that the key exists and don't care about its value, you can add the following code to your test case.
1It'should return proper data for a user' Do2Expected_data = Json.parse (Io.read ('Users.json'))#parse the data in the Users.json file as an expected value3result = Restclient.get'https://api.github.com/users/rest-client',: Content_Type =: JSON,: Accept + =: JSON4 #expect (result) to Match_json_expression (Expected_data)5expected_data['Updated_at'] =Wildcard_matcher6End
To learn more about the Json_expression gem, hit the GitHub wiki page.
Rspec+rest-client Testing third-party Web service