Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions sdk/core/Azure.Core/src/Internal/ResponseDebugView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Diagnostics;

namespace Azure
{
internal class ResponseDebugView<T>
{
private readonly Response<T> _response;

public ResponseDebugView(Response<T> response)
{
_response = response;
}

public Response GetRawResponse => _response.GetRawResponse();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice


public T Value => _response.Value;
}
}
5 changes: 5 additions & 0 deletions sdk/core/Azure.Core/src/Response.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,10 @@ public static Response<T> FromValue<T>(T value, Response response)
{
return new ValueResponse<T>(response, value);
}

public override string ToString()
{
return $"Status: {Status}, ReasonPhrase: {ReasonPhrase}";
}
}
}
8 changes: 6 additions & 2 deletions sdk/core/Azure.Core/src/Response{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
// Licensed under the MIT License.

using System.ComponentModel;
using System.Diagnostics;

namespace Azure
{
/// <summary>
/// Represents a result of Azure operation.
/// </summary>
/// <typeparam name="T">The type of returned value.</typeparam>
[DebuggerTypeProxy(typeof(ResponseDebugView<>))]
public abstract class Response<T>
{
/// <summary>
Expand Down Expand Up @@ -37,7 +39,9 @@ public abstract class Response<T>
public override int GetHashCode() => base.GetHashCode();

/// <inheritdoc />
[EditorBrowsable(EditorBrowsableState.Never)]
public override string ToString() => base.ToString();
public override string ToString()
{
return $"Status: {GetRawResponse().Status}, Value: {Value}";
}
}
}
5 changes: 5 additions & 0 deletions sdk/core/Azure.Core/src/Shared/NoBodyResponse{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public override T Value

public override Response GetRawResponse() => _response;

public override string ToString()
{
return $"Status: {GetRawResponse().Status}, Service returned no content";
Comment thread
pakrym marked this conversation as resolved.
}

#pragma warning disable CA1064 // Exceptions should be public
private class ResponseBodyNotFoundException : Exception
#pragma warning restore CA1064 // Exceptions should be public
Expand Down
30 changes: 30 additions & 0 deletions sdk/core/Azure.Core/tests/ResponseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ public void ValueObtainedFromCast()
Assert.AreEqual("test_name", value.Name);
}

[Test]
public void ToStringsFormatsStatusAndValue()
{
var response = Response.FromValue(new TestPayload("test_name"), response: new MockResponse(200));

Assert.AreEqual("Status: 200, Value: Name: test_name", response.ToString());
}

[Test]
public void ToStringsFormatsStatusAndResponsePhrase()
{
var response = new MockResponse(200, "Phrase");

Assert.AreEqual("Status: 200, ReasonPhrase: Phrase", response.ToString());
}

[Test]
public void ValueThrowsIfUnspecified()
{
Expand Down Expand Up @@ -63,6 +79,15 @@ public void ValueThrowsFromCastIfUnspecified()
Assert.True(throws);
}

[Test]
public void ToStringsFormatsStatusAndMessageForNoBodyResponse()
{
var response = new NoBodyResponse<TestPayload>(new MockResponse(200));

Assert.AreEqual("Status: 200, Service returned no content", response.ToString());
}


internal class TestPayload
{
public string Name { get; }
Expand All @@ -71,6 +96,11 @@ public TestPayload(string name)
{
Name = name;
}

public override string ToString()
{
return $"Name: {Name}";
}
}
}

Expand Down
2 changes: 0 additions & 2 deletions sdk/core/Azure.Core/tests/TestFramework/MockResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ public MockResponse(int status, string reasonPhrase = null)

public bool IsDisposed { get; private set; }

public override string ToString() => $"{Status}";

public void SetContent(byte[] content)
{
ContentStream = new MemoryStream(content);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ public void SetLiveResponse(Response live, bool throwOnFailure)
}
}

public override string ToString()
{
return _live == null ? "Status: NotSent, the batch has not been submitted yet" : base.ToString();
}

// We directly forward the entire Response interface to LiveResponse
#region forward Response members to Live
/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Azure.Core;
using Azure.Core.Testing;
using Azure.Storage.Blobs.Specialized;
using NUnit.Framework;

namespace Azure.Storage.Blobs.Test
{
public class DelayedResponseTests
{
[Test]
public void DelayedResponseToStringDoesntThrow()
{
DelayedResponse delayedResponse = new DelayedResponse(new HttpMessage(new MockRequest(), new ResponseClassifier()));
Assert.AreEqual("Status: NotSent, the batch has not been submitted yet", delayedResponse.ToString());
}

[Test]
public void CompletedDelayedResponseToStringCallsBase()
{
DelayedResponse delayedResponse = new DelayedResponse(new HttpMessage(new MockRequest(), new ResponseClassifier()));
delayedResponse.SetLiveResponse(new MockResponse(200, "Yay"), false);
Assert.AreEqual("Status: 200, ReasonPhrase: Yay", delayedResponse.ToString());
}
}
}