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