Monday, 10 March 2025

Can Traffic Parrot return binary files in responses?

 "Can an HTTP response in Traffic Parrot contain a binary file?" - software developer working for an e-commerce company.

Yes.

The simplest way to create a mock in Traffic Parrot that returns a binary file in the response is to create that mock by doing a recording. That assumes you have a real service that you can record.

If you don't have a real service that returns the binary file in the response, you can create a mapping manually. To do that:

  1. Create a file in trafficparrot-x.y.z/mappings directory called, for example mapping-dummy-file.json
  2. The mapping-dummy-file.json should contain the following (notice the reference to body-dummy-file.pdf): 
    {
      "id" : "5d6a8f32-914c-4b67-ae15-f2c9d7b3e091",
      "name" : "mapping-dummy-file.json",
      "request" : {
        "url" : "/dummy.pdf",
        "method" : "GET"
      },
      "response" : {
        "status" : 200,
        "bodyFileName" : "body-dummy-file.pdf",
        "headers" : {
          "Content-type" : "application/pdf"
        }
      }
    }
  3. Create the binary response body file body-dummy-file.pdf in trafficparrot-x.y.z/__files. You can use any PDF file just make sure the filename is specified in the mapping json file in bodyFileName attribute
  4. Now if you visit http://localhost:8081/dummy.pdf then a PDF binary file will be returned. 

Monday, 24 February 2025

Traffic Parrot 5.52.0 released, what's new?

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

Features

  • Import OpenAPI files from the openapi configuration directory on startup
  • Enable in trafficparrot.properties by setting:
    trafficparrot.openapi.import.on.startup=true

Fixes

  • Library upgrades to fix OWASP issues

Changes

  • Upgraded bundled JRE from 8u402 to 21.0.6
  • OpenAPI imports performed via the UI will override the previous import rather than duplicating mappings
  • Downgraded to WireMock 2 request matchers and Handlebars response templating helpers due to performance issues with WireMock 3

Tuesday, 18 February 2025

How to Mock Twilio SDK Calls With Traffic Parrot

Current flow

RabbitMQ -> SpringBoot + Twilio SDK -> Twilio API

Desired flow with Traffic Parrot

RabbitMQ -> SpringBoot + Twilio SDK -> Traffic Parrot -> Twilio API

Details

  • Using Twilio REST API v2010
  • Endpoint: /Accounts/{account_sid}/Messages.json
  • OpenAPI spec: https://raw.githubusercontent.com/twilio/twilio-oai/main/spec/json/twilio_api_v2010.json

Question

How can we intercept and mock Twilio SDK calls using Traffic Parrot since the SDK doesn't accept a configurable URL?

Answer

Setup steps:

  1. Update your code to send requests to Traffic Parrot instead of real Twilio APIs, see sample code below
  2. Run Traffic Parrot on port 8081
  3. Configure recorder to forward requests to api.twilio.com
  4. Record sample responses from Twilio
  5. Switch recorder to replay mode
  6. Use the mock responses in your tests
// 1. Create a custom HTTP client that redirects Twilio requests to Traffic Parrot
public class TrafficParrotHttpClient extends NetworkHttpClient {
    private static final String TWILIO_API = "https://api.twilio.com";
    private static final String TRAFFIC_PARROT = "http://localhost:8081";
    
    @Override
    public Response makeRequest(Request request) {
        // Rewrite URL to point to Traffic Parrot
        String rewrittenUrl = request.getUrl().replace(TWILIO_API, TRAFFIC_PARROT);
        
        // Create new request with rewritten URL while preserving auth and other properties
        Request rewrittenRequest = new Request(request.getMethod(), rewrittenUrl)
            .setAuth(request.getUsername(), request.getPassword())
            .setHeaders(request.getHeaders());
            
        return super.makeRequest(rewrittenRequest);
    }
}

// 2. Configure Twilio client to use custom HTTP client
TwilioRestClient client = new TwilioRestClient.Builder("YOUR_ACCOUNT_SID", "YOUR_AUTH_TOKEN")
    .httpClient(new TrafficParrotHttpClient())
    .build();

// 3. Set as default client
Twilio.setRestClient(client);

// 4. Use Twilio SDK normally - calls will be redirected to Traffic Parrot
Message message = Message.creator(
    new PhoneNumber("+1555555555"),
    new PhoneNumber("+1555555556"), 
    "Hello from Traffic Parrot!")
    .create();

Monday, 13 January 2025

Retrieving Filenames from Binary HTTP Requests in Traffic Parrot

 We got a question recently from one of our prospects.

"When a file is sent as binary data rather than form-data, is it possible to retrieve the filename in a response in Traffic Parrot?" - Software Developer working for a French company

If you also have this issue, please send the trafficparrot.log to support@trafficparrot.com so we can see how your file is being sent to Traffic Parrot in the HTTP request.

There are many ways a filename could be included in a HTTP request, and it is hard to guess which one your company uses.

For example, here is one way you could extract it from a header for a binary request:

{{ regex request.headers.Content-Disposition 'filename=\"([^\"]+)\"' 'g' }}

Here is how you could test it:

curl -X POST http://localhost:8081/hello -H "Content-Type: application/octet-stream" -H "Content-Disposition: attachment; filename=\"helloworld.png\"" --data-binary @helloworld.png -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-Type: application/octet-stream
> Content-Disposition: attachment; filename="helloworld.png"
> Content-Length: 70509
>
* We are completely uploaded and fine
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Matched-Stub-Id: 8c4695d4-f94e-4ba8-9793-ee5dcbfba6b5
< 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.png



Saturday, 11 January 2025

Traffic Parrot 5.51.1 released, what's new?

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

Features

Changes

Monday, 30 December 2024

Solving QA Environment Availability Issues with Partial Pass-Through JMS IBM MQ Mocks

Testing teams often face common QA environment challenges:

  • Environment downtime
  • Intermittent errors from backend systems
  • Test data setup takes weeks
  • Teams conflict over shared test data
  • Limited test scenarios
While the long-term solution involves stabilizing shared environments and implementing testing pyramids, here's a quick fix using Traffic Parrot's partial pass-through mocking capabilities.



We implemented this solution for a mobile onboarding application connecting to IBM MQ middleware. By creating mock queues alongside production queues, we enabled:

  • Simultaneous mocked and real responses based on test data
  • Automated test data setup and reset
  • Extended test coverage for previously unavailable scenarios
  • Reduced environment dependencies
 Key Implementation Steps:
  • Create additional mock queues
  • Reconfigure client to use mock queues
  • Set up pass-through mocks in Traffic Parrot

The beauty of this approach? You maintain real system integration while having the flexibility to mock specific scenarios. For example, we successfully mocked unsupported mobile service types while passing through all other requests to the actual backend.

Want to try it yourself? Check out Traffic Parrot's demo applications on GitHub, which have step-by-step implementation guides.

Friday, 27 December 2024

Partial passthrough and proxy HTTP mocks

Are you struggling with unstable QA environments, conflicting test data, or time-consuming setups?

Meet partial HTTP mocks using Traffic Parrot.

Here's how it works: Traffic Parrot acts as a smart proxy between your app and backend systems. Regular requests go to the real system, while special test cases use predefined mock data.

In our demo, a financial app gets real stock prices for Apple and Google but uses mock data for test scenarios like unlisted companies.

The result? Always available environments, stable responses, and quick test data setup - all without changing your core systems.

Tuesday, 10 December 2024

Using Handlebars for Date Comparisons in Response Logic in Traffic Parrot

 We received a question recently from one of our prospects.

"I wanted to know about the advanced usage of dynamic rules. How do I add scripting for my response logic? I need some date comparisons for calculating some fields values, is it possible to with scripting or handlebars?" - Software Developer working for a gaming company.

You can add scripting to your response login with scripts or handlebars. Here is the documentation on how to do that: https://trafficparrot.com/documentation/5.50.x/dynamic.html  

Handbars should be enough for this simple case of comparing dates.

For example, you can add this into your response body like on the screenshot displayed below:

{{#if (gt (date (parseDate request.query.SomeDate format='dd-MM-yyyy')) (date (now)))}}
Response if the defined date is after now
{{else}}
Response if the defined date is not after now
{{/if}}

This code compares SomeDate request query parameter with today's date and returns a different response based on that comparison. 

Here is an example result:

support@support-pcs:/optf/git$ curl http://localhost:8081/hello?SomeDate=01-01-2024
Response if the defined date is not after now
support@support-pcs:/optf/git$ curl http://localhost:8081/hello?SomeDate=01-01-2025
Response if the defined date is after now


Friday, 6 December 2024

How to configure Traffic Parrot log level at runtime in Docker

We got a question from one of our customers about how to configure the log level at runtime when using Traffic Parrot in Docker.
How to pass environment variables to Docker
The Dockerfile CMD command can be modified to accept environment variables as follows:
ENV GUI_HTTP_PORT=8080
ENV VS_HTTP_PORT=8081
CMD exec ./start-foreground.sh \
    trafficparrot.gui.http.port=$GUI_HTTP_PORT \
    trafficparrot.virtualservice.http.port=$VS_HTTP_PORT
How to set the logging level dynamically with Docker environment variables
First the Dockerfile can be configured with an environment variable:
ENV LOG_LEVEL=ERROR
This can be then be configured for use:
  • trafficparrotserver.log4j.properties can be configured with:
    log4j.rootLogger=${LOG_LEVEL},file,stdout
    And the Dockerfile with:
    CMD exec ./start-foreground.sh -DLOG_LEVEL=$LOG_LEVEL
  • trafficparrotserver.log4j2.xml can be configured with:
    <Root level="${env:LOG_LEVEL}">
  • trafficparrotserver.logback.xml can be configured with:
    <root level="${LOG_LEVEL}">

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

Saturday, 12 October 2024

Traffic Parrot 5.49.0 released, what's new?

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

Features

  • Added support for the JWT Extension for WireMock
  • JWT usage examples, typically mocked via an /oauth/token endpoint:
    {{jwt maxAge='12 days'}}
    {{jwt exp=(parseDate '2041-02-23T21:22:23Z')}}
    {{jwt nbf=(parseDate '2019-02-23T21:22:23Z')}}
    {{jwt iss='https://issuer.trafficparrot.com/'}}
    {{jwt aud='https://audience.trafficparrot.com/'}}
    {{jwt sub='subject'}}
    {{jwt alg='RS256'}}
    {{jwt
        customBoolClaim=true
        customIntClaim=23
        customStringClaim='example@x.y.z'
        customDateClaim=(parseDate '2024-01-02T03:04:05Z')
    }}
  • JSON Web Key Set (JWKS) usage example, typically mocked via an /.well-known/jwks.json endpoint:
    {{jwks}}
  • Settings also visible via http://localhost:8080/api/http/__admin/settings as
    {
      "settings" : {
        "extended" : {
          "jwt" : {
            "hs256Secret" : "...",
            "rs256PublicKeyId" : "...",
            "rs256PublicKey" : "-----BEGIN RSA PUBLIC KEY-----\n...\n-----END RSA PUBLIC KEY-----\n",
            "rs256PrivateKey" : "-----BEGIN RSA PRIVATE KEY-----\n...\n-----END RSA PRIVATE KEY-----\n"
          }
        }
      }
    }
    
  • Enable in trafficparrot.properties by setting:
    trafficparrot.http.jwt.enabled=true

Fixes

  • Library upgrades to fix OWASP issues

Thursday, 27 June 2024

Traffic Parrot 5.48.0 released, what's new?

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

Features

  • New Thrift API to send a request by mapping id:
    curl -v http://localhost:8080/thrift/management/sendThriftMessage -d send-mapping-id=fc7ee4ad-e247-49a8-8528-af20845adde9 -d send-to-host-port=localhost:5562
  • Thrift message recordings are now in JSON format
  • New {{ executeProcess '/bin/bash' '-c' 'echo hello' }} helper to execute external processes

Fixes

  • Fixed a Thrift issue when the same method name is defined in super and subclass
  • Fixed a Thrift issue with methods that are prefixes/suffixes of other methods

Changes

  • Thrift compiler binary must be version 0.10.0 for backwards compatibility reasons