Can't get REST request headers and body content using events

Report your bugs here. We promise to deal with them.

Can't get REST request headers and body content using events

Postby JKowalczyk » 05 Jul 2012 17:12

Hi All,

I'm trying to create a helper class to transform any REST request into a cURL command (very handy for DEVs to reproduce the bug :) );
I'm aiming at writing a code that will generate such cURL commands for each REST request send no matter what test runner was used (test suite, test case, or test step).

Let's start with configuring SoapUI:
  • Firstly, put my CurlGenerator.groovy script in your script library folder, using such directory structure: script_lib_folder/com/yelllabs/soapy/helpers/CurlGenerator.groovy
  • Then, place a custom listener (please find it attached - curl-listeners.xml) in bin/listeners folder.
  • After that add a new event in project properties Events tab:
    Code: Select all
    name: RequestFilter.afterRequest
    event: RequestFilter.afterRequest
  • followed by two lines of code to execute my code:
    Code: Select all
    import com.yelllabs.soapy.helpers.CurlGenerator;
    new CurlGenerator(context, request, log);

    ps. this constructor will automatically print the command, so there's no point in invoking any further method or assigning it to a field.

OK, so now we've all files and config in place and the interesting part starts :)

As you remember I want to generate cURL command no matter how you execute your test (using test suite, test case or test step runner)

Let's start with submitting request by manually running a test step:
When you execute it for the first time, then in the "Script log" tab you should see a cURL command but with missing HTTP headers:
Code: Select all
Thu Jul 05 16:32:49 BST 2012:INFO:REQUEST HEADERS: [:]
Thu Jul 05 16:32:49 BST 2012:INFO:curl -ki -XPOST "https://hostname/api/categories"  -d '{
   "asdasd":"asdasd"
}'


If I submit the same request for the second time, then I'll get what I want, so all the headers and msg body:
Code: Select all
Thu Jul 05 16:33:22 BST 2012:INFO:REQUEST HEADERS: [:]
Thu Jul 05 16:33:22 BST 2012:INFO:curl -ki -XPOST "https://hostname/api/categories"  -H "Host:hostname" -H "Content-Length:22" -H "Accept-Encoding:gzip,deflate" -H "User-Agent:Apache-HttpClient/4.1.1 (java 1.5)" -H "Connection:Keep-Alive" -H "Content-Type:application/json;charset=UTF-8" -H "Accept:application/json" -d '{
   "asdasd":"asdasd"
}'

( I don't know why, but I have a feeling that those headers are coming from the previous request and not from the current one but that just a feeling :) )

Now, let's send that request but by using a test case and test suite runner:
No matter how many times I run, it it'll be always missing all the headers:
Code: Select all
Thu Jul 05 17:00:40 BST 2012:INFO:REQUEST HEADERS: [:]
Thu Jul 05 17:00:40 BST 2012:INFO:curl -ki -XPOST "https://hostname/api/categories"  -d '{
   "asdasd":"asdasd"
}'



Can you help me please with this problem??? :)
I've spend so many hours studying the docs, that I had it enough for next few months ;)

aaaah I almost forgot, sometimes I can't access the msg body too, but I can't reproduce that yet :)

Cheers,
Janusz
Attachments
CurlGenerator.groovy
class that should generate a cURL command using the actual request that was made
(4.31 KiB) Downloaded 263 times
error_log_when_running_a_test_case_or_a_test_suite.txt
here's the error that I always see in "Error Log" tab, when a request is made using test case or test suite runner. It won't appear if I manually execute a REST test step.
(1.33 KiB) Downloaded 249 times
curl-listeners.xml
new event listener placed in bin/listeners
(485 Bytes) Downloaded 263 times
JKowalczyk
soapui pro users
soapui pro users
 
Posts: 13
Joined: 11 May 2009 17:55

Return to soapUI Pro Bugs