Skip to content

Conversation

mexmirror
Copy link
Contributor

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": "AdeleV@M365x214355.onmicrosoft.com",
          "mobilePhone": null,
          "officeLocation": "18/2111",
          "preferredLanguage": "en-US",
          "surname": "Vance",
          "userPrincipalName": "AdeleV@M365x214355.onmicrosoft.com",
          "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
Contributor Author

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

@waldekmastykarz
Copy link
Collaborator

Thanks for reporting the issue and the fix! We'll review it asap.

@waldekmastykarz waldekmastykarz added bug Something isn't working pr-bugfix Fixes a bug labels Oct 7, 2025
Copy link
Collaborator

@waldekmastykarz waldekmastykarz left a comment

Choose a reason for hiding this comment

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

Perfect! Nothing to add 👏

@waldekmastykarz waldekmastykarz merged commit 6af73eb into dotnet:main Oct 7, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pr-bugfix Fixes a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants