Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9254387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:27:22+00:00 2026-06-18T11:27:22+00:00

I am writing a unit test for a WebApi controller that reads a POST

  • 0

I am writing a unit test for a WebApi controller that reads a POST body from Request.InputStream. I need to set the inputstream property of HttpContext.Current.Request.InputStream, or set the inputstream’s contents. Here is my unit test code so far, but it keeps throwing exceptions:

var originalStream = HttpContext.Current.Request.InputStream;
Stream newStream = new MemoryStream(ASCIIEncoding.Default.GetBytes("Test String"));

var propInfo = originalStream.GetType().GetProperty("CanWrite");
propInfo.SetValue(originalStream, true);

newStream.CopyTo(originalStream);

propInfo.SetValue(originalStream, false);

I get the following exception on the SetValue line:

ArgumentException: Property set method not found

Am I going about this all wrong? My controller reads the input stream and deserializes it into JSON, so I need to be able to insert data into that stream. I just don’t know how to do it. Many thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:27:23+00:00Added an answer on June 18, 2026 at 11:27 am

    public abstract bool CanWrite { get; }. There is not setter on that property.. hence your error.

    In your example, originalStream will be of type Stream. Wrap it in another stream for your test. You aren’t testing the HttpRequest.InputStream, you’re testing the deserialization …

    var originalStream = new StreamReader(HttpContext.Current.Request.InputStream);
    var content = originalStream.ReadToEnd();
    

    .. etc.

    You may even consider skipping using the request input altogether since you aren’t really testing that.

    EDIT:

    To expand a bit more. You should move this out of the controller action.. do something like the below:

    public class YourController : Controller {
        private readonly IStreamWrapper _streamWrapper;
    
        public YourController(IStreamWrapper wrapper) {
            _streamWrapper = wrapper;
        }
    
        public ActionResult MethodYouAreTesting() {
            var result = _streamWrapper.Process(HttpRequest.InputStream);
        }
    }
    
    public class Tests {
        public void YourTestMethod() {
            var controller = new YourController(new FakeStreamWrapper()); // mock perhaps?
            // Asserts here for the controller action
        }
    
        public void YourWrapperTester() {
            var wrapper = new RealStreamWrapper();
            // test Process method here..
        }
    }
    

    Then you can test the stream reading and the deserialization in isolation.

    Does that make sense or have I made it more complex? :/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a unit test for a Grails controller that renders a domain class
Busy writing a unit test for a controller that produces a view-model that includes
I am writing a unit test and the controller method is throwing an exception
I'm writing a unit test for a Text Scrubber utility that will remove any
I'm writing a unit test for a custom subclass of org.springframework.http.converter.xml.AbstractXmlHttpMessageConverter<T> and I need
I am writing a unit test that needs to access an image file that
I am having some trouble with writing a unit test that checks my custom
I am writing a c# unit test that validates string properties for an ORM
We are writing unit tests for our ASP.NET application that run against a test
I am writing a unit test for a method which fills an object from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.