---
title: "Signature Matching | Speedscale Docs"
description: "Implement signature matching with proxymock to create realistic service mocks that accurately simulate external APIs and databases for your testing environment"
canonical: "https://docs.speedscale.com/proxymock/how-it-works/signature/"
---

[Documentation index](https://docs.speedscale.com/proxymock/llms.txt) | [Markdown](https://docs.speedscale.com/proxymock/how-it-works/signature/index.md) | [Canonical page](https://docs.speedscale.com/proxymock/how-it-works/signature/)

# Signature Matching

Most non-trivial applications rely on external systems like 3rd party APIs or databases to function. This is especially true for apps that utilize microservices architectures. [Mocks](https://docs.speedscale.com/reference/glossary/#mock) can be generated by hand using **proxymock** or automatically by Speedscale Enterprise. Examples of services that could be mocked include the Google API, the AWS API postgres or another team's HTTP REST endpoint.

<a id="how-does-it-work"></a>

## How does it work?

Service mocks are generally made available over the network and are accessible to your app in a test environment or on the local desktop. If your service mocks are working properly, your service under test ([SUT](https://docs.speedscale.com/reference/glossary/#sut)) will believe it is actually running in production because the mock is convincing. A realistic service mock must simulate the following components:

*   data that is similar to production
*   realistic response time (not too fast, not too slow)
*   accurate response sequencing for non-idempotent requests
*   continuously updated tokens like authentication, etc

<a id="how-does-it-know-which-response-to-return"></a>

## How does it know which response to return?

**proxymock** uses a special text string called a _signature_ to differentiate between different requests. When the service under test (SUT) sends a request to **proxymock**, selective nuggets of information are extracted from the request and added to the signature. Typically this includes things like HTTP Query Parameters or MySQL SQL statements. Once this signature is generated, it is matched against the library of signatures generated from the previously recorded traffic. When the incoming signature matches a previously recorded one, the related response is returned.

The sequence of events goes like this:

1.  Traffic is recorded/imported to **proxymock**
2.  You create a Snapshot containing traffic covering a specific timeframe and filter criteria.
3.  The **proxymock** analyzer combines the traffic into a set of signature/response pairs. This is like a compiler which builds source files into a working program.
4.  The **proxymock** service mocking engine receives a message from the SUT and creates a signature.
5.  The request signature is compared against all known request signatures in the Snapshot.
6.  A response is returned if the signature is recognized or a 404 if it is unknown.
7.  If multiple requests have the same signature but different responses, **proxymock** will cycle through the responses in order.

Fundamentally, the signature is a hash map of key=value pairs. The objective of adding transforms to the signature is to make it match with a particular response found in the response. The service mock engine will apply the transforms to the incoming request and based on those modifications it will attempt to find a match in the snapshot. So the purpose of this exercise is to modify miss or passthrough requests individually using transforms until they match an existing response in the snapshot.

**proxymock**\` will automatically generate signatures for common protocols such as HTTP and Postgres. Modification of these signatures is possible and covered in this section. By adding new requests to your traffic snapshot you effectively add new service mock known responses. During environment replication, **proxymock** will return the response matching what was recorded in the original environment.

tip

You may notice an `instance` field in the signature. This is used to differentiate between multiple requests that are identical. If you modify the instance, you cause the service mock to return this response the nth time this signature is received.

Take the following HTTP request as an example:

```text
POST /foo-platform/pdt/foo/management HTTP/1.1
Content-Type: application/xml; charset=ISO-8859-1

<my request body>
```

This HTTP request would generate a signature similar to the following:

```markdown
  http:host: localhost
  http:method: POST
  http:url: /foo-platform/pdt/foo/management,
  http:requestBodyJSON: G15IHJlcXVlc3QgYm9keT4=,
```

Signatures are essentially hash maps of key=value pairs.

<a id="what-kind-of-dependencies-can-be-automatically-mocked"></a>

## What kind of dependencies can be automatically mocked?

**proxymock** automatically mocks any technology on our [supported technology page](https://docs.speedscale.com/reference/technology-support/). We've created baselines that work in most situations but you can customize as described below.

<a id="what-happens-if-my-requests-are-non-idempotent-aka-the-response-changes-after-each-request"></a>

## What happens if my requests are non-idempotent (aka the response changes after each request)?

When the recording holds more than one response for the same signature, **proxymock** takes turns: the first matching request gets the first recorded response, the next request gets the second, and after the last one it starts again from the first. Each signature keeps its own turn.

To pick one of them at random instead, start the mock server with `--response-selection random`. A response that was recorded more times is picked more often. The default is `round-robin`.

To add a response by hand, copy an [RRPair](https://docs.speedscale.com/reference/glossary/#rrpair), change its response, and keep the signature the same. Start the mock server with `--mock-reload-interval 1s` and it picks up edited or added RRPair files without a restart.

Speedscale Enterprise automatically handles changing responses and is smart enough to deal with many common patterns without human intervention. The fancypants Speedscale Enterprise product incorporates a learning model that mutates based on the protocol and specific API type being monitored. Even if Speedscale has never seen your API before there is a good probability of it being able to automatically mock your dependency without training. Yes that is technically a plug for the paid product but handling this use case without automation is likely to make you sad so don't say we didn't warn you.

<a id="can-a-mock-respond-as-slowly-as-the-real-dependency"></a>

## Can a mock respond as slowly as the real dependency?

Yes. By default a mock answers immediately, which is fast for tests but hides timeouts, retries and other slow-path code. `--mock-timing` makes each mocked HTTP response wait:

*   `none`, the default, answers immediately.
*   `recorded` waits as long as the original response took, using the latency stored in each recorded RRPair.
*   A multiplier scales the recorded latency: `5x` is five times slower than recorded, and `.25x` is four times faster.

```shell
proxymock mock --in ./proxymock/recorded-<timestamp> --mock-timing recorded
```

A wait is capped at 30 seconds. Native gRPC calls replay their recorded message timing whatever `--mock-timing` says, unless you pass `--grpc-streaming-immediate`. Other mocked protocols, such as SQL, Redis and Kafka, answer immediately.

To change the wait for one response, edit `duration` or `elapsed` in its Markdown RRPair, as described in the [RRPair format](https://docs.speedscale.com/proxymock/how-it-works/rrpair-format/). A `latency` effect from a [chaos rule](https://docs.speedscale.com/concepts/chaos/) changes the wait: a fixed value such as `2s` replaces the mock timing, a factor such as `3x` multiplies it, so it has no effect with `--mock-timing none`, and a range such as `100ms-2s` adds a random amount on top.

The `mock_server_start` MCP tool takes the same options as `mock-timing`, `response-selection` and `mock-reload-interval`. The full flag list is in the [CLI reference](https://docs.speedscale.com/reference/proxymock-cli-reference/#mock).

<a id="how-do-i-change-the-signature-of-a-request"></a>

## How do I change the signature of a request?

The signature of a request can be changed directly in the [RRPair](https://docs.speedscale.com/reference/glossary/#rrpair) file.

For example, let's say you have this signature:

```markdown
http:host is api.example.com
http:method is POST
http:queryparams is limit=10
http:url is /v1/users
```

But you would like to mock a request being sent to the mock server with the query parameters `limit=50&offset=10`. You can change the signature in the file to:

```markdown
http:host is api.example.com
http:method is POST
http:queryparams is limit=50&offset=10
http:url is /v1/users
```

Now when that request is sent to the mock server the response from this file will be returned.

tip

After modifying the request section of an RRPair file use the `proxymock files update-mocks` command to reset the signature to match.

<a id="what-if-proxymock-cannot-automatically-mock-my-dependency"></a>

## What if **proxymock** cannot automatically mock my dependency?

You can request new protocol support in the Speedscale [community Slack](https://slack.speedscale.com/). Otherwise, you will need to provide your own replica of the dependency. Typically this involves writing a custom mock server using a framework like [Mockito](https://site.mockito.org/) or [gomock](https://github.com/golang/mock). For databases this gets a lot more involved and will require database replication tools.

<a id="what-are-some-of-the-downsides-of-service-mocking"></a>

## What are some of the downsides of service mocking?

Custom built service mocks tend to fall out of date quickly because the data or the API contract changes. For this reason we recommend regenerating your mocks automatically using **proxymock** every sprint (if possible).
