예제 #1
0
        [Ignore] // Remove Atom
        // [TestMethod, Variation(Description = "Validates the payloads for various top-level errors written using ODataMessageWriter.WriteError().")]
        public void TopLevelODataMessageWriterErrorTest()
        {
            this.CombinatorialEngineProvider.RunCombinations(
                this.CreateODataErrorTestDescriptors(/*throwOnRequest*/ true, /*topLevel*/ true),
                this.WriterTestConfigurationProvider.ExplicitFormatConfigurationsWithIndent,
                (testDescriptor, testConfiguration) =>
            {
                testConfiguration = testConfiguration.Clone();
                testConfiguration.MessageWriterSettings.MessageQuotas.MaxNestingDepth = this.customSetRecursionDepthLimit;

                ODataAnnotatedError error = (ODataAnnotatedError)testDescriptor.PayloadItems.Single();

                TestWriterUtils.WriteAndVerifyTopLevelContent(
                    testDescriptor,
                    testConfiguration,
                    (messageWriter) => messageWriter.WriteError(error.Error, error.IncludeDebugInformation),
                    this.Assert,
                    baselineLogger: this.Logger);
            });
        }
예제 #2
0
        public void DisposeAfterExceptionTest()
        {
            // create a default entry and then set both read and edit links to null to provoke an exception during writing
            ODataResource entry = ObjectModelUtils.CreateDefaultEntry();

            this.Assert.IsNull(entry.EditLink, "entry.EditLink == null");

            this.CombinatorialEngineProvider.RunCombinations(
                new ODataItem[] { entry },
                new bool[] { true, false }, // writeError
                new bool[] { true, false }, // flush
                this.WriterTestConfigurationProvider.ExplicitFormatConfigurations.Where(c => c.IsRequest == false),
                (payload, writeError, flush, testConfiguration) =>
            {
                testConfiguration = testConfiguration.Clone();
                testConfiguration.MessageWriterSettings.SetServiceDocumentUri(ServiceDocumentUri);

                // try writing to a memory stream
                using (var memoryStream = new TestStream())
                    using (var messageWriter = TestWriterUtils.CreateMessageWriter(memoryStream, testConfiguration, this.Assert))
                    {
                        ODataWriter writer = messageWriter.CreateODataWriter(isFeed: false);
                        ODataWriterCoreInspector inspector = new ODataWriterCoreInspector(writer);
                        try
                        {
                            // write the invalid entry and expect an exception
                            TestExceptionUtils.ExpectedException(
                                this.Assert,
                                () => TestWriterUtils.WritePayload(messageWriter, writer, false, entry),
                                ODataExpectedExceptions.ODataException("WriterValidationUtils_EntriesMustHaveNonEmptyId"),
                                this.ExceptionVerifier);

                            this.Assert.IsTrue(inspector.IsInErrorState, "Writer is not in expected 'error' state.");

                            if (writeError)
                            {
                                // now write an error which is the only valid thing to do
                                ODataAnnotatedError error = new ODataAnnotatedError
                                {
                                    Error = new ODataError()
                                    {
                                        Message = "DisposeAfterExceptionTest error message."
                                    }
                                };
                                Exception ex = TestExceptionUtils.RunCatching(() => TestWriterUtils.WritePayload(messageWriter, writer, false, error));
                                this.Assert.IsNull(ex, "Unexpected error '" + (ex == null ? "<none>" : ex.Message) + "' while writing an error.");
                                this.Assert.IsTrue(inspector.IsInErrorState, "Writer is not in expected 'error' state.");
                            }

                            if (flush)
                            {
                                writer.Flush();
                            }
                        }
                        catch (ODataException oe)
                        {
                            if (writeError && !flush)
                            {
                                this.Assert.AreEqual("A writer or stream has been disposed with data still in the buffer. You must call Flush or FlushAsync before calling Dispose when some data has already been written.", oe.Message, "Did not find expected error message");
                                this.Assert.IsTrue(inspector.IsInErrorState, "Writer is not in expected 'error' state.");
                            }
                            else
                            {
                                this.Assert.Fail("Caught an unexpected ODataException: " + oe.Message + ".");
                            }
                        }
                    }
            });
        }
예제 #3
0
        /// <summary>
        /// Writes the collection payload as specified in the <paramref name="testDescriptor"/>.
        /// </summary>
        /// <param name="messageWriter">The message writer.</param>
        /// <param name="writer">The writer to write to.</param>
        /// <param name="flush">True if the stream should be flush before returning; otherwise false.</param>
        /// <param name="testDescriptor">The test descriptor specifying the collection to write.</param>
        internal static void WriteCollectionPayload(ODataMessageWriterTestWrapper messageWriter, ODataCollectionWriter writer, bool flush, CollectionWriterTestDescriptor testDescriptor)
        {
            Debug.Assert(writer != null, "writer != null");
            Debug.Assert(testDescriptor != null, "testDescriptor != null");

            object[] payloadItems     = testDescriptor.PayloadItems;
            int      payloadItemIndex = 0;

            foreach (CollectionWriterTestDescriptor.WriterInvocations invocation in testDescriptor.Invocations)
            {
                switch (invocation)
                {
                case CollectionWriterTestDescriptor.WriterInvocations.StartCollection:
                    ODataCollectionStartSerializationInfo serInfo = null;
                    if (!string.IsNullOrEmpty(testDescriptor.CollectionTypeName))
                    {
                        serInfo = new ODataCollectionStartSerializationInfo();
                        serInfo.CollectionTypeName = testDescriptor.CollectionTypeName;
                    }

                    writer.WriteStart(new ODataCollectionStart {
                        Name = testDescriptor.CollectionName, SerializationInfo = serInfo
                    });
                    break;

                case CollectionWriterTestDescriptor.WriterInvocations.Item:
                    object payloadItem = payloadItems[payloadItemIndex];

                    ODataError error = payloadItem as ODataError;
                    if (error != null)
                    {
                        throw new InvalidOperationException("Expected payload item but found an error.");
                    }

                    writer.WriteItem(payloadItem);
                    payloadItemIndex++;
                    break;

                case CollectionWriterTestDescriptor.WriterInvocations.Error:
                    ODataAnnotatedError error2 = testDescriptor.PayloadItems[payloadItemIndex] as ODataAnnotatedError;
                    if (error2 == null)
                    {
                        throw new InvalidOperationException("Expected an error but found a payload item.");
                    }

                    messageWriter.WriteError(error2.Error, error2.IncludeDebugInformation);
                    payloadItemIndex++;
                    break;

                case CollectionWriterTestDescriptor.WriterInvocations.EndCollection:
                    writer.WriteEnd();
                    break;

                case CollectionWriterTestDescriptor.WriterInvocations.UserException:
                    throw new Exception("User code triggered an exception.");

                default:
                    break;
                }
            }

            if (flush)
            {
                writer.Flush();
            }
        }