-
Notifications
You must be signed in to change notification settings - Fork 497
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Error reason not propagating on some status codes (#753)
* Gateway should return body if available * Ensure error message is present * Test to validate gateway response * Json support * EnsureSuccessStatus tests * Extra lines * changelog
- Loading branch information
1 parent
b101e6c
commit c5038a9
Showing
5 changed files
with
200 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/CosmosExceptionTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
//------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
//------------------------------------------------------------ | ||
|
||
namespace Microsoft.Azure.Cosmos | ||
{ | ||
using System; | ||
using System.IO; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.Cosmos.Common; | ||
using Microsoft.Azure.Cosmos.Routing; | ||
using Microsoft.Azure.Documents; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Moq; | ||
using Newtonsoft.Json; | ||
|
||
[TestClass] | ||
public class CosmosExceptionTests | ||
{ | ||
[TestMethod] | ||
public void EnsureSuccessStatusCode_DontThrowOnSuccess() | ||
{ | ||
ResponseMessage responseMessage = new ResponseMessage(HttpStatusCode.OK); | ||
responseMessage.EnsureSuccessStatusCode(); | ||
} | ||
|
||
[TestMethod] | ||
public void EnsureSuccessStatusCode_ThrowsOnFailure() | ||
{ | ||
ResponseMessage responseMessage = new ResponseMessage(HttpStatusCode.NotFound); | ||
Assert.ThrowsException<CosmosException>(() => responseMessage.EnsureSuccessStatusCode()); | ||
} | ||
|
||
[TestMethod] | ||
public void EnsureSuccessStatusCode_ThrowsOnFailure_ContainsBody() | ||
{ | ||
string testContent = "TestContent"; | ||
using (MemoryStream memoryStream = new MemoryStream()) | ||
{ | ||
StreamWriter sw = new StreamWriter(memoryStream); | ||
sw.Write(testContent); | ||
sw.Flush(); | ||
memoryStream.Seek(0, SeekOrigin.Begin); | ||
|
||
ResponseMessage responseMessage = new ResponseMessage(HttpStatusCode.NotFound) { Content = memoryStream }; | ||
try | ||
{ | ||
responseMessage.EnsureSuccessStatusCode(); | ||
Assert.Fail("Should have thrown"); | ||
} | ||
catch(CosmosException exception) | ||
{ | ||
Assert.IsTrue(exception.Message.Contains(testContent)); | ||
} | ||
} | ||
} | ||
|
||
[TestMethod] | ||
public void EnsureSuccessStatusCode_ThrowsOnFailure_ContainsJsonBody() | ||
{ | ||
string message = "TestContent"; | ||
Error error = new Error(); | ||
error.Code = "code"; | ||
error.Message = message; | ||
string testContent = JsonConvert.SerializeObject(error); | ||
using (MemoryStream memoryStream = new MemoryStream()) | ||
{ | ||
StreamWriter sw = new StreamWriter(memoryStream); | ||
sw.Write(testContent); | ||
sw.Flush(); | ||
memoryStream.Seek(0, SeekOrigin.Begin); | ||
|
||
ResponseMessage responseMessage = new ResponseMessage(HttpStatusCode.NotFound) { Content = memoryStream }; | ||
try | ||
{ | ||
responseMessage.EnsureSuccessStatusCode(); | ||
Assert.Fail("Should have thrown"); | ||
} | ||
catch (CosmosException exception) | ||
{ | ||
Assert.IsTrue(exception.Message.Contains(message)); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters