Skip to content

Temporary Partial Mock Support.#82

Open
MertNYuksel wants to merge 3 commits intomainfrom
feature/partial-mock
Open

Temporary Partial Mock Support.#82
MertNYuksel wants to merge 3 commits intomainfrom
feature/partial-mock

Conversation

@MertNYuksel
Copy link
Member

Add partial mock support with RFC 9535 JSONPath and RFC 6902 JSON Patch

Introduces PartialMockDecider for applying targeted modifications to response JSON using JSONPath queries and JSON Patch operations. Supports wildcard URL/method matching, template variable resolution, and all six RFC 6902 operation types.

Endpoint: /partial-mock
Route: /partial-mock

PUT /partial-mock — Add a partial mock

  • Request body: JSON-encoded PartialMockModel containing deviceId, url, method, mockDomain, and modifications (JSONPath + JSON Patch operations)
  • Returns: 202 Accepted
  • Behavior: Registers a partial mock rule. If a rule with the same (deviceId, url, method) already exists, it replaces it. Future responses matching the rule will have the JSON Patch modifications applied.

DELETE /partial-mock — Remove partial mocks

  • Request body: JSON-encoded PartialMockModel (only deviceId is used)
  • Returns: 202 Accepted
  • Behavior: Removes all partial mock rules for the given deviceId.

Example PUTS:

  1. Add a partial mock — marketing object instrumentation with recursive descent
  curl -X PUT http://127.0.0.1:8008/partial-mock \
    -H "Content-Type: application/json" \
    -d '{
      "deviceId": "iPhone-15-simulator",
      "url": "*",
      "method": "*",
      "mockDomain": "LocalDevelopment",
      "modifications": [
        {
          "path": "$..marketing.*",
          "operations": [
            { "type": "add", "key": "{jsonPath}", "value": "tracked" },
            { "type": "add", "key": "domain", "value": "{domain}" },
            { "type": "add", "key": "requestPath", "value": "{requestPath}" }
          ]
        }
      ]
    }'

Matches all responses (wildcard url + method), finds every child under any marketing object at any depth, and injects the matched node's JSONPath, domain, and request path.

  1. Add a partial mock — replace an existing field
  curl -X PUT http://127.0.0.1:8008/partial-mock \
    -H "Content-Type: application/json" \
    -d '{
      "deviceId": "iPhone-15-simulator",
      "url": "/api/product/detail",
      "method": "GET",
      "mockDomain": "LocalDevelopment",
      "modifications": [
        {
          "path": "$.product",
          "operations": [
            { "type": "replace", "key": "price", "value": 0.01 },
            { "type": "add", "key": "discountLabel", "value": "TEST DISCOUNT" }
          ]
        }
      ]
    }'

Replaces price and adds discountLabel inside $.product for product detail responses.

  1. Add a partial mock — modify all array elements
  curl -X PUT http://127.0.0.1:8008/partial-mock \
    -H "Content-Type: application/json" \
    -d '{
      "deviceId": "iPhone-15-simulator",
      "url": "/api/search",
      "method": "POST",
      "mockDomain": "LocalDevelopment",
      "modifications": [
        {
          "path": "$.results[*]",
          "operations": [
            { "type": "add", "key": "sponsored", "value": true }
          ]
        }
      ]
    }'


Adds "sponsored": true to every item in the results array.

mert.yuksel and others added 2 commits February 17, 2026 18:19
Introduces PartialMockDecider for applying targeted modifications to response
JSON using JSONPath queries and JSON Patch operations. Supports wildcard
URL/method matching, template variable resolution, and all six RFC 6902
operation types. Includes comprehensive unit tests (38 tests) and updates
README with feature documentation.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
}

/// Context about the original HTTP request, passed through for template variable resolution.
struct RequestContext {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can rename it for better meaning


let liveResult = try await proxyRequest(request: request, mockDomain: flags.domain)
var liveResult = try await proxyRequest(request: request, mockDomain: flags.domain)
liveResult.body = await applyPartialMocks(to: liveResult.body, request: request, flags: flags)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we save partial modified mock, or save as original?


final class HandlePartialMock: HTTPHandler {
static var handler: PartialMockHandlerInterface?
private let jsonDecoder = JSONDecoder()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use JSONDecoder.shared

private let logger = Logger(category: "HandlePartialMock")

func handleRequest(_ request: HTTPRequest) async throws -> HTTPResponse {
logger.debug("New request handled")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be better add more context to this log. like path

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants