Thursday, 21 November 2024

How to Increment Order Numbers in Dynamic Responses

 We just got a question from one of our customers.

"How do I add +1 here? (jsonPath request.body '$.orderNumber')" - Software Developer working for a Spanish company

You can use the math helper, for example:

{{math (jsonPath request.body '$.orderNumber') '+' 1}}

Wednesday, 20 November 2024

Implementing Controlled Chaos: How to Simulate API Failures with Time-Based and Random Error Responses

We just got a question from one of our prospects:

"How can I implement conditional error responses in my API mocks? Specifically, I want to:

  • Return an error every 100th API call
  • Return an error based on system time (e.g., every 10 minutes)
For a specific endpoint like 'hello':
  • Return a normal JSON response for most requests
  • Return an error response after every 1000 requests
The error responses should work seamlessly with my existing mock responses. What's the best way to implement this kind of controlled failure testing?" - Software Developer 

He is a sample of how you can return an error for a single mock whenever the minute is in the 10s (10, 20, 30,...), and every 100 requests randomly return an error as well.
This is for a single mock. You can also do it for all mocks or selected mocks, depending on your requirements, with a different solution, etc., which we are happy to share.
So please share your business requirement or testing scenario you are looking for with support@trafficparrot.com 

Paste this in the response body (see screenshot attached):
{{#with (now format='mm') as |currentMinute|}}
{{#with (randomInteger 1 101) as |requestId|}}
{{#if (or
    (eq (math currentMinute '%' 10)  "0")
    (eq (math requestId '%' 100) "0")
)}}{{modifyResponse 'statusCode' 503}}
{
    "error": "Service temporarily unavailable",
    "details": "Error triggered by time (10 min interval) or simulated request count",
    "currentMinute": {{currentMinute}},
    "requestId": {{requestId}}
}
{{else}}
{
    "normalResponse": "This is your regular JSON response",
    "timestamp": "{{now format='yyyy-MM-dd\'T\'HH:mm:ss.SSSXXX'}}",
    "requestId": {{requestId}}
}
{{/if}}
{{/with}}
{{/with}}

You can test the randomness by running:
support@support-pcs:~/tmp$ for i in {1..1000}; do echo "Request $i"  && curl -v http://localhost:8081/hello >> test_random.log 2>&1 && echo -e "\n---" >> test_random.log; done
support@support-pcs:~/tmp$ grep  "Service temporarily unavailable" test_random.log | wc -l
10
support@support-pcs:~/tmp$ grep -B 20 "Service temporarily unavailable" test_random.log | head -n 20
> GET /hello HTTP/1.1
> Host: localhost:8081
> User-Agent: curl/7.81.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 503 Service Unavailable
< Matched-Stub-Id: 8c4695d4-f94e-4ba8-9793-ee5dcbfba6b5
< Matched-Stub-Name: hello-8c4695d4-f94e-4ba8-9793-ee5dcbfba6b5.json
< Content-Length: 201
< Server: Jetty(9.4.56.v20240826)
<
{ [201 bytes data]
100   201  100   201    0     0  54046      0 --:--:-- --:--:-- --:--:-- 67000
* Connection #0 to host localhost left intact

You can test the time error response by running:
support@support-pcs:~/tmp$ while true; do current_time=$(date +"%H:%M:%S"); echo "Time: $current_time" && curl -i http://localhost:8081/hello && echo -e "\n-------------------\n" && sleep 10; done



Tuesday, 19 November 2024

Extracting Filename from HTTP Request Body in Traffic Parrot Response Template

We got a question yesterday from one of our prospects.

"Is it possible to get a filename from a HTTP request body byte array? Can you send me an example?" - Software Developer evaluating Traffic Parrot

Yes! The simplest way to do this is using a regexp. For example, create a mapping where the response body is (see attached screenshot for details):

{{ regex request.body 'filename=\"([^\"]+)\"' 'g' }}

You can then test it like this for example:

support@support-pcs:~/Downloads$ curl -X POST http://localhost:8081/hello -H "Content-Type: multipart/form-data" -F "file=@helloworld.pdf" -v

Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying 127.0.0.1:8081...
* Connected to localhost (127.0.0.1) port 8081 (#0)
> POST /hello HTTP/1.1
> Host: localhost:8081
> User-Agent: curl/7.81.0
> Accept: */*
> Content-Length: 7497
> Content-Type: multipart/form-data; boundary=------------------------1417eab3cd6fdb0a
>

* We are completely uploaded and fine
* Mark bundle as not supporting multiuse

< HTTP/1.1 200 OK
< Matched-Stub-Id: 8c4695d4-f94e-4ba8-9793-ee5dcbfba6b
< Matched-Stub-Name: hello-8c4695d4-f94e-4ba8-9793-ee5dcbfba6b5.json
< Vary: Accept-Encoding, User-Agent
< Content-Length: 41
< Server: Jetty(9.4.56.v20240826)
<
Request contains a file: helloworld.pdf



Thursday, 7 November 2024

Traffic Parrot 5.50.1 released, what's new?

We have just released version 5.50.1. Here is a list of the changes that came with the release:

Features

Fixes

  • Library upgrades to fix OWASP issues