README.markdown in api_matchers-0.0.1 vs README.markdown in api_matchers-0.0.2

- old
+ new

@@ -20,39 +20,40 @@ gem install api_matchers ## Usage -### Have Node Matcher +### Including in RSpec - "{ 'transaction': { 'id': '54', 'status': 'paid' } }".should have_node(:transaction) +To include all this matchers you need to include the APIMatchers::RSpecMatchers module: - "{ 'transaction': { 'id': '54', 'status': 'paid' } }".should have_node(:id).with(54) + RSpec.configure do |config| + config.include APIMatchers::RSpecMatchers + end - "{ 'error': '', 'transaction': { 'id': '55', 'status': 'waiting_payment' } }".should have_node(:error).with('not_authorized') +### Have Node Matcher -If you want to configure to make all **searches inside a root element**, you can do this: +The have_node matcher parse the actual and see if have the expcted node with the expected value. +**The default that have_node will parse is JSON.** - APIMatchers.setup do |config| - config.root_element = :transaction - end + "{ 'transaction': { 'id': '54', 'status': 'paid' } }".should have_node(:transaction) - "{ 'transaction': { 'id': '54', 'status': 'paid' } }".should have_node(:id).with(54) # WILL PASS + "{ 'transaction': { 'id': '54', 'status': 'paid' } }".should have_node(:id).with(54) - "{ 'error': '', 'transaction': { 'id': '55', 'status': 'waiting_payment' } }".should have_node(:error).with('not_authorized') # WILL NOT PASS BECAUSE THE ERROR NODE ISN'T INSIDE THE TRANSACTION NODE + "{ 'error': 'not_authorized', 'transaction': { 'id': '55' } }".should have_node(:error).with('not_authorized') ### HAVE NODE Matcher Configuration -You can configure if you want xml or json(**JSON is the default**): +You can configure if you want xml(**JSON is the default**): APIMatchers.setup do |config| config.content_type = :xml end '<transaction><id>200</id><status>paid</status></transaction>'.should have_node(:status).with('paid') -**Observation: You can use the *have_xml_node* or *have_json_node* if you don't want to configure everytime.** +**If you work with xml and json in the same API, I recommend that you check the have_json_node and have_xml_node matchers.** You can configure the name of the method for example: ## Instead of this response.body.should have_node(:foo) @@ -64,10 +65,19 @@ Then you can use without call the **#body** method: response.should have_node(:foo).with('bar') +### Have JSON Node Matcher + + "{ 'transaction': { 'id': '54', 'status': 'paid' } }".should have_json_node(:id).with(54) + +### Have XML Node Matcher + + "<product><name>gateway</name></product>".should have_xml_node(:name).with('gateway') + + ### Create Resource Matcher This matchers see the HTTP STATUS CODE is equal to 201. response.status.should create_resource @@ -114,16 +124,30 @@ ### Be in XML Matcher This is a matcher that see if the content type is xml: - response.content_type.should be_in_xml + response.headers['Content-Type'].should be_in_xml ### Be in JSON Matcher This is a matcher that see if the content type is in JSON: - response.content_type.should be_in_json + response.headers['Content-Type'].should be_in_json + +### Headers Configuration + +You can configure the name method to call the headers and content type: + + APIMatchers.setup do |config| + config.header_method = :headers + config.header_content_type_key = 'Content-Type' + end + +Then you can use without call the **#headers** calling the **#['Content-Type']** method: + + response.should be_in_json + response.should be_in_xml ### Acknowlegments * Special thanks to Daniel Konishi to contribute in the product that I extracted the matchers to this gem. \ No newline at end of file