[opensuse-ruby] How to test REST API
Hi, We've found out that our RoR-based application can crash if broken XML is sent as a request to the application REST API. I've tried to create a test case that simulates sending XML - as found on several forums and howtos - by using @request.env['RAW_POST_DATA'] but this variable seems to be ignored during the test. See the attached file, please. I've tested that the application behaves differently if the very same XML is POSTed to REST API by curl. See the command below (XML sent to server is different here): curl -H 'Accept: application/xml' -H 'Content-type: application/xml' -u '$user:$password' --data-binary '<xml/&' --http-request POST https://$server:443/$path How can I test it? What's wrong with my testcase that the RAW_POST_DATA entry is ignored? Thanks in advance for any help :) Bye Lukas -- Lukas Ocilka, Appliances Department, Novell Inc.
* Lukas Ocilka <lukas.ocilka@suse.cz> [Apr 12. 2010 16:19]:
Hi,
We've found out that our RoR-based application can crash if broken XML is sent as a request to the application REST API.
I've tried to create a test case that simulates sending XML - as found on several forums and howtos - by using @request.env['RAW_POST_DATA'] but this variable seems to be ignored during the test. See the attached file, please.
Hmm, what makes you think RAW_POST_DATA gets ignored ? How does the receiving end (CustomersController) look like ? According to the Rails documentation, the request.body() is delivered as a StringIO. So you need request.body.read() to access its contents. Klaus --- SUSE LINUX Products GmbH, GF: Markus Rex, HRB 16746 (AG Nürnberg) -- To unsubscribe, e-mail: opensuse-ruby+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-ruby+help@opensuse.org
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Dne 12.4.2010 18:14, Klaus Kaempf napsal(a):
* Lukas Ocilka <lukas.ocilka@suse.cz> [Apr 12. 2010 16:19]:
Hi,
We've found out that our RoR-based application can crash if broken XML is sent as a request to the application REST API.
I've tried to create a test case that simulates sending XML - as found on several forums and howtos - by using @request.env['RAW_POST_DATA'] but this variable seems to be ignored during the test. See the attached file, please.
Hmm, what makes you think RAW_POST_DATA gets ignored ?
- From what I've seen, the RAW_POST_DATA should be used as the request input processed by the Rails core, not by the controller itself. Controller should work independently on the used method: testcase / curl. So, maybe I'm wrong here. If I send the very same XML via curl --data-binary, it works correctly.
How does the receiving end (CustomersController) look like ?
A common rails-generated controller, nothing special. It doesn't read the request itself.
According to the Rails documentation, the request.body() is delivered as a StringIO. So you need request.body.read() to access its contents.
That's why I'm asking how to test the REST API. I, of course, know how to test controller with correct data, e.g., using a hash of params, but this time I need to create a test-case that sends incorrect data (broken XML) to the application REST API. The application has to render a exception-catch page which I want to have tested. Thx Lukas -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.12 (GNU/Linux) Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org/ iD8DBQFLxD0kVSqMdRCqTiwRAo/2AJ4o0eyvRYk0HLSgabSeqPu0A7hXbACdGvIe l1BVQFmTxpeUBSMGqyoyTJY= =ZyVI -----END PGP SIGNATURE----- -- To unsubscribe, e-mail: opensuse-ruby+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-ruby+help@opensuse.org
* Lukas Ocilka <lukas.ocilka@suse.cz> [Apr 13. 2010 11:42]:
Hmm, what makes you think RAW_POST_DATA gets ignored ?
- From what I've seen, the RAW_POST_DATA should be used as the request input processed by the Rails core, not by the controller itself.
Right. And my initial tests support that.
If I send the very same XML via curl --data-binary, it works correctly.
I still do not know what "works correctly" means for you. What does your receiving controller look like and how does it parse the request ? Klaus --- SUSE LINUX Products GmbH, GF: Markus Rex, HRB 16746 (AG Nürnberg) -- To unsubscribe, e-mail: opensuse-ruby+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-ruby+help@opensuse.org
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Dne 13.4.2010 11:55, Klaus Kaempf napsal(a):
* Lukas Ocilka <lukas.ocilka@suse.cz> [Apr 13. 2010 11:42]:
Hmm, what makes you think RAW_POST_DATA gets ignored ?
- From what I've seen, the RAW_POST_DATA should be used as the request input processed by the Rails core, not by the controller itself.
Right. And my initial tests support that.
Not until you read them from the @request manually or at least not in the test environment. Pepa has, kind-of, confirmed that.
If I send the very same XML via curl --data-binary, it works correctly.
I still do not know what "works correctly" means for you. What does your receiving controller look like and how does it parse the request ?
Works correctly => E.g., :create creates a new customer. Using RAW_POST_DATA does not create it and reports that all the required fields were missing. Anyway, solved using a different method. All I wanted is to test whether we correctly catch an application exception in the RoR layers. I used mocha: - --- cut --- Customer.any_instance.stubs(:initialize).raises(Exception, '[EXPECTED ERROR]...') - --- cut --- Which throws an exception while creating the new Customer object at the beginning of the 'create' function. Thx && Bye Lukas -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.12 (GNU/Linux) Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org/ iD8DBQFLxHKaVSqMdRCqTiwRAkOPAKCB4H5bWB7HhzzXsDbqGWY6xc9cSQCeK170 lyE5B+nK6LmXv1E8CsSd8oA= =lDjG -----END PGP SIGNATURE----- -- To unsubscribe, e-mail: opensuse-ruby+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-ruby+help@opensuse.org
Lukas Ocilka write:
Dne 13.4.2010 11:55, Klaus Kaempf napsal(a):
* Lukas Ocilka <lukas.ocilka@suse.cz> [Apr 13. 2010 11:42]:
Hmm, what makes you think RAW_POST_DATA gets ignored ?
- From what I've seen, the RAW_POST_DATA should be used as the request input processed by the Rails core, not by the controller itself.
Right. And my initial tests support that.
Not until you read them from the @request manually or at least not in the test environment. Pepa has, kind-of, confirmed that.
Some interesting details which I found and clearly description what is intended with test. At first goal is to catch exception throws by ParamsParser middleware. It is prerequest parser which change request in xml to hash - http://github.com/rails/rails/blob/v2.3.5/actionpack/lib/action_controller/p... and http://github.com/rails/rails/blob/v2.3.5/actionpack/lib/action_controller/m... Problem is that functional test simulate request in final state, so it doesn't call this middleware and just pass hash to it. It fills RAW_POST_DATA just to simulate request, but internally doesn't use it and pass hash. Thats reason why it doesn't take affect - http://github.com/rails/rails/blob/2-3-stable/actionpack/lib/action_controll... So for me it looks the easiest way to test this behavior, that we mock functionalTest simulation and it throw same exception as came from that middleware. Josef
If I send the very same XML via curl --data-binary, it works correctly.
I still do not know what "works correctly" means for you. What does your receiving controller look like and how does it parse the request ?
Works correctly => E.g., :create creates a new customer. Using RAW_POST_DATA does not create it and reports that all the required fields were missing.
Anyway, solved using a different method. All I wanted is to test whether we correctly catch an application exception in the RoR layers. I used mocha: --- cut --- Customer.any_instance.stubs(:initialize).raises(Exception, '[EXPECTED ERROR]...') --- cut --- Which throws an exception while creating the new Customer object at the beginning of the 'create' function.
Thx && Bye Lukas
-- Josef Reidinger YaST team maintainer of perl-Bootloader, YaST2-Repair, parts of webyast -- To unsubscribe, e-mail: opensuse-ruby+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-ruby+help@opensuse.org
participants (3)
-
Josef Reidinger
-
Klaus Kaempf
-
Lukas Ocilka