コード例 #1
0
ファイル: BrowserFileTest.cs プロジェクト: sbauer/AspNetCore
    public void OpenReadStream_ReturnsStreamWhoseDisposalReleasesTheJSObject()
    {
        // Arrange: JS runtime that always returns a specific mock IJSStreamReference
        var jsRuntime         = new Mock <IJSRuntime>(MockBehavior.Strict);
        var jsStreamReference = new Mock <IJSStreamReference>();

        jsRuntime.Setup(x => x.InvokeAsync <IJSStreamReference>(It.IsAny <string>(), It.IsAny <CancellationToken>(), It.IsAny <object[]>()))
        .Returns(ValueTask.FromResult(jsStreamReference.Object));

        // Arrange: InputFile
        var inputFile = new InputFile {
            JSRuntime = jsRuntime.Object
        };
        var file = new BrowserFile {
            Owner = inputFile, Size = 5
        };
        var stream = file.OpenReadStream();

        // Assert 1: IJSStreamReference isn't disposed yet
        jsStreamReference.Verify(x => x.DisposeAsync(), Times.Never);

        // Act
        _ = stream.DisposeAsync();

        // Assert: IJSStreamReference is disposed now
        jsStreamReference.Verify(x => x.DisposeAsync());
    }
コード例 #2
0
ファイル: BrowserFileTest.cs プロジェクト: sbauer/AspNetCore
    public void OpenReadStream_ThrowsIfFileSizeIsLargerThanAllowedSize()
    {
        // Arrange
        var file = new BrowserFile {
            Size = 100
        };

        // Act & Assert
        var ex = Assert.Throws <IOException>(() => file.OpenReadStream(80));

        Assert.Equal("Supplied file with size 100 bytes exceeds the maximum of 80 bytes.", ex.Message);
    }
コード例 #3
0
ファイル: BrowserFileTest.cs プロジェクト: sbauer/AspNetCore
    public async Task OpenReadStream_ReturnsStreamWhoseDisposalReleasesTheJSObject_ToleratesDisposalException()
    {
        // Arrange: JS runtime that always returns a specific mock IJSStreamReference whose disposal throws
        var jsRuntime         = new Mock <IJSRuntime>(MockBehavior.Strict);
        var jsStreamReference = new Mock <IJSStreamReference>();

        jsRuntime.Setup(x => x.InvokeAsync <IJSStreamReference>(It.IsAny <string>(), It.IsAny <CancellationToken>(), It.IsAny <object[]>()))
        .Returns(ValueTask.FromResult(jsStreamReference.Object));
        jsStreamReference.Setup(x => x.DisposeAsync()).Throws(new InvalidTimeZoneException());

        // Arrange: InputFile
        var inputFile = new InputFile {
            JSRuntime = jsRuntime.Object
        };
        var file = new BrowserFile {
            Owner = inputFile, Size = 5
        };
        var stream = file.OpenReadStream();

        // Act/Assert. Not throwing is success here.
        await stream.DisposeAsync();
    }
コード例 #4
0
        protected async Task HandleValidSubmit()
        {
            if (BrowserFile != null)
            {
                Logger.LogWarning("Upload using InputFile + HttpClient.");

                var buffer = new byte[BrowserFile.Size];
                await BrowserFile.OpenReadStream().ReadAsync(buffer);

                File.FileName = BrowserFile.Name;
                var res = await FileService.UploadFile(File, buffer);

                NavManager.NavigateTo($"/files/edit/{res.Id}");
            }
            else
            {
                Logger.LogWarning("Upload using JavaScript Interop.");

                var dotNetObj = DotNetObjectReference.Create(this);
                await JSRuntime.InvokeVoidAsync("interop.uploadFile", FileService.GetUploadUrl(), await FileService.GetAccessToken(), File, dotNetObj);
            }
        }