Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code tabs in stub-metadata.md #199

Merged
merged 2 commits into from
Oct 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions _docs/stub-metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ It is possible to attach arbitrary metadata to stub mappings, which can be later

Data under the `metadata` key is a JSON object (represented in Java by a `Map<String, ?>`). It can be added to a stub mapping on creation.

Java:
{% codetabs %}

{% codetab Java %}

```java
stubFor(get("/with-metadata")
Expand All @@ -25,7 +27,9 @@ stubFor(get("/with-metadata")
));
```

JSON:
{% endcodetab %}

{% codetab JSON %}

```json
{
Expand All @@ -46,19 +50,27 @@ JSON:
}
```

{% endcodetab %}

{% endcodetabs %}

## Search for stubs by metadata

Stubs can be found by matching against their metadata using the same matching strategies as when [matching HTTP requests](../request-matching/).
The most useful matcher for this is `matchesJsonPath`:

Java:
{% codetabs %}

{% codetab Java %}

```java
List<StubMapping> stubs =
findStubsByMetadata(matchingJsonPath("$.singleItem", containing("123")));
```

API:
{% endcodetab %}

{% codetab JSON %}

```json
POST /__admin/mappings/find-by-metadata
Expand All @@ -71,21 +83,29 @@ POST /__admin/mappings/find-by-metadata
}
```

{% endcodetab %}

{% endcodetabs %}

## Remove stubs by metadata

Similarly, stubs with matching metadata can be removed:

Java:
{% codetabs %}

{% codetab Java %}

```java
removeStubsByMetadata(matchingJsonPath("$.singleItem", containing("123")));
```

API:
{% endcodetab %}

{% codetab JSON %}

```json
POST /__admin/mappings/remove-by-metadata

```json
{
"matchesJsonPath" : {
"expression" : "$.singleItem",
Expand All @@ -94,6 +114,10 @@ POST /__admin/mappings/remove-by-metadata
}
```

{% endcodetab %}

{% endcodetabs %}

## Remove request journal events by metadata

See [Removing items from the journal](../verifying#by-criteria)