Skip to content

Conversation

mexmirror
Copy link

Description

In version 1.2.0 when configuring the MockResponsePlugin, it is not possible to return a mocked response for a GET request (or any request without a body).

Steps to reproduce

Use a mock.json example from https://learn.microsoft.com/en-us/microsoft-cloud/dev/dev-proxy/how-to/mock-responses. For example:

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v1.0.0/mockresponseplugin.mocksfile.schema.json",
  "mocks": [
    {
      "request": {
        "url": "https://graph.microsoft.com/v1.0/users/*"
      },
      "response": {
        "body": {
          "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity",
          "businessPhones": ["+1 425 555 0109"],
          "displayName": "Adele Vance",
          "givenName": "Adele",
          "jobTitle": "Product Marketing Manager",
          "mail": "[email protected]",
          "mobilePhone": null,
          "officeLocation": "18/2111",
          "preferredLanguage": "en-US",
          "surname": "Vance",
          "userPrincipalName": "[email protected]",
          "id": "87d349ed-44d7-43e1-9a83-5f2406dee5bd"
        }
      }
    }
  ]
}

Sending a request to this configured URL with "curl" causes the plugin to fail to process the request and just pass the request to the actual host.

 req   ╭ GET https://graph.microsoft.com/v1.0/users/exampleupn
 time  │ 10/6/2025 1:07:58 PM +00:00
 trce  │ MockResponsePlugin: BeforeRequestAsync called
 trce  │ MockResponsePlugin: ReplacePlaceholders called
 fail  │ An error occurred in a plugin
 pass  ╰ Passed through

Issue

This is caused by an Titanium.Web.Proxy.Exceptions.BodyNotFoundException in the first if-statement in the ReplacePlaceholders method of the MockResponsePlugin class, when trying to read the property BodyString on the request object.

Fix

To prevent processing a body that is not present, the condition that causes the exception can be replaced with !response.HasBody, as it semantically means the same.

@mexmirror mexmirror requested a review from a team as a code owner October 6, 2025 13:23
@Copilot Copilot AI review requested due to automatic review settings October 6, 2025 13:23
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a BodyNotFoundException that occurs when the MockResponsePlugin processes GET requests or other requests without a body. The issue prevents the plugin from returning mocked responses for requests that don't have a request body.

Key Changes

  • Replace the problematic request.BodyString is null check with !request.HasBody to avoid accessing the body property when no body exists

@mexmirror
Copy link
Author

Hello :) This is one of my first contributions to Open-Source Software. Let me know if there is something I need to change.

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.

1 participant