diff options
author | youssefm <youssefm@microsoft.com> | 2012-09-22 02:04:12 +0400 |
---|---|---|
committer | youssefm <youssefm@microsoft.com> | 2012-09-22 02:49:35 +0400 |
commit | eb498c77fd23e71c86845441c2cadfab36dd116c (patch) | |
tree | 5d6406177d172ab72c38df1c2c97bc164736eee3 | |
parent | 03c8721af64344d9fa1960c4037706e6e04b8fb3 (diff) |
[OData] Throw NotSupportedException if a caller tries to use ODataMediaTypeFormatter to write a client request
4 files changed, 29 insertions, 0 deletions
diff --git a/src/System.Web.Http.OData/OData/Formatter/ODataMediaTypeFormatter.cs b/src/System.Web.Http.OData/OData/Formatter/ODataMediaTypeFormatter.cs index 4c309ce2..7ea293ec 100644 --- a/src/System.Web.Http.OData/OData/Formatter/ODataMediaTypeFormatter.cs +++ b/src/System.Web.Http.OData/OData/Formatter/ODataMediaTypeFormatter.cs @@ -238,6 +238,11 @@ namespace System.Web.Http.OData.Formatter throw Error.ArgumentNull("writeStream"); } + if (Request == null) + { + throw Error.NotSupported(SRResources.WriteToStreamAsyncMustHaveRequest); + } + HttpContentHeaders contentHeaders = content == null ? null : content.Headers; return TaskHelpers.RunSynchronously(() => { diff --git a/src/System.Web.Http.OData/Properties/SRResources.Designer.cs b/src/System.Web.Http.OData/Properties/SRResources.Designer.cs index 42577390..5ef39551 100644 --- a/src/System.Web.Http.OData/Properties/SRResources.Designer.cs +++ b/src/System.Web.Http.OData/Properties/SRResources.Designer.cs @@ -914,5 +914,14 @@ namespace System.Web.Http.OData.Properties { return ResourceManager.GetString("WriteObjectNotSupported", resourceCulture); } } + + /// <summary> + /// Looks up a localized string similar to The OData formatter does not support writing client requests. This formatter instance must have an associated request.. + /// </summary> + internal static string WriteToStreamAsyncMustHaveRequest { + get { + return ResourceManager.GetString("WriteToStreamAsyncMustHaveRequest", resourceCulture); + } + } } } diff --git a/src/System.Web.Http.OData/Properties/SRResources.resx b/src/System.Web.Http.OData/Properties/SRResources.resx index 40940609..d23f41aa 100644 --- a/src/System.Web.Http.OData/Properties/SRResources.resx +++ b/src/System.Web.Http.OData/Properties/SRResources.resx @@ -402,4 +402,7 @@ <data name="FunctionNotSupportedOnEnum" xml:space="preserve"> <value>The '{0}' function cannot be applied to an enumeration-typed argument.</value> </data> + <data name="WriteToStreamAsyncMustHaveRequest" xml:space="preserve"> + <value>The OData formatter does not support writing client requests. This formatter instance must have an associated request.</value> + </data> </root>
\ No newline at end of file diff --git a/test/System.Web.Http.OData.Test/OData/Formatter/ODataMediaTypeFormatterTests.cs b/test/System.Web.Http.OData.Test/OData/Formatter/ODataMediaTypeFormatterTests.cs index 8bf79b9b..51158bb2 100644 --- a/test/System.Web.Http.OData.Test/OData/Formatter/ODataMediaTypeFormatterTests.cs +++ b/test/System.Web.Http.OData.Test/OData/Formatter/ODataMediaTypeFormatterTests.cs @@ -139,6 +139,18 @@ namespace System.Web.Http.OData.Formatter } [Fact] + public void WriteToStreamAsync_ThrowsNotSupported_WithoutRequest() + { + var builder = new ODataConventionModelBuilder(); + builder.EntitySet<Customer>("Customers"); + var formatter = new ODataMediaTypeFormatter(builder.GetEdmModel()); + + Assert.Throws<NotSupportedException>( + () => formatter.WriteToStreamAsync(typeof(Customer), new Customer(), new MemoryStream(), content: null, transportContext: null), + "The OData formatter does not support writing client requests. This formatter instance must have an associated request."); + } + + [Fact] public void ODataFormatter_DefaultPatchKeyMode_Is_Ignore() { ODataMediaTypeFormatter formatter = new ODataMediaTypeFormatter(); |