Skip to content

SftpClient.OpenAsync should return Stream for ease of mocking #1691

Closed

Description

@doxxx

SftpClient.OpenAsync currently returns an SftpFileStream which cannot be mocked by NSubstitute or even faked because it has private/internal constructors.

It would be nice if OpenAsync was instead defined as returning Stream, even though it actually returns a SftpFileStream. The only non-Stream public member that I can see is the Timeout property. If it's absolutely vital that this property is exposed, maybe define an ISftpFileStream interface instead? Or the timeout could be passed to OpenAsync.

Activity

  1. waldemarsson commented on Sep 30, 2025

    @waldemarsson

    Any update on this? Trying to write unit tests for my integration and am stuck because I can't mock OpenAsync

  2. waldemarsson commented on Oct 3, 2025

    @waldemarsson

    As a workaround I just created a proxy service that wraps ISftpClient.

    internal class SftpClientProxy : IDisposable
    {
        private readonly ISftpClient _sftpClient;
    
        public SftpClientProxy(ISftpClient sftpClient) => _sftpClient = sftpClient;
    
        public virtual async Task ConnectAsync(CancellationToken cancellationToken) =>
            await _sftpClient.ConnectAsync(cancellationToken);
    
        public virtual async Task<Stream> OpenAsync(string path, CancellationToken cancellationToken) =>
            await _sftpClient.OpenAsync(path, FileMode.Open, FileAccess.Read, cancellationToken);
    
        public void Dispose() => _sftpClient.Dispose();
    }
    
  3. fkubicek commented on Oct 31, 2025

    @fkubicek

    I also ran into this when trying to mock. @doxxx describes it perfectly. I would consider the option of adding ISftpFileStream because, IMO, it would be nicer to mock than returning Stream. But anything is better than the current state.

  4. doxxx commented on Oct 31, 2025

    @doxxx
    Author

    I took a look at this. It is not simple.

    There's a lot of assumptions in SSH.NET that the type returned by OpenAsync (and all the other similar methods) extends Stream. Not to mention all of the existing user code that could assume that these methods return a type that extends Stream. It would be a significantly breaking change to use an interface type instead.

    Changing the return type to Stream however is much easier. Still requires a bunch of changes here and there, mostly in test code, which specifies the expected return type exactly, but easily fixed. The only drawback to this option is that you lose access to the Timeout property. That said, none of the test code actually accesses this property...

  5. doxxx commented on Nov 6, 2025

    @doxxx
    Author

    As @Rob-Hague said in #1725, the wrapper solution by @waldemarsson is the best option.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions