public void OutputParametersReturnsReflectedHttpParameters() { List <Type> types = new List <Type>(); TestDataAssert.Execute( TestData.RepresentativeValueAndRefTypeTestDataCollection, (type, obj) => { if (!types.Contains(type)) { types.Add(type); for (int i = 2; i <= 17; i++) { if (types.Count - i < 0) { break; } Type[] typeArray = types.Skip(types.Count - i).ToArray(); HttpOperationHandler genericHandler = GetGenericHandlerForTypes(typeArray); for (int j = 0; j < genericHandler.OutputParameters.Count; j++) { HttpParameter parameter = genericHandler.OutputParameters[j]; Assert.AreEqual("output", parameter.Name, "The HttpParameter.Name should have been 'input'."); Assert.AreEqual(typeArray.Last(), parameter.Type, "The HttpParameter.Type should have been the last type from the array."); } } } }); }
public void CanWriteAs2ReturnsTrue() { SMediaTypeFormatter formatter = new SMediaTypeFormatter() { CallBase = true }; foreach (string mediaType in HttpTestData.LegalMediaTypeStrings) { formatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue(mediaType)); } TestDataAssert.Execute( TestData.RepresentativeValueAndRefTypeTestDataCollection, (type, obj) => { MediaTypeHeaderValue matchedMediaType = null; SObjectContent objectContent = new SObjectContent(type, obj) { CallBase = true }; objectContent.Headers.ContentType = formatter.SupportedMediaTypes[0]; Assert.IsTrue(formatter.CanWriteAs(type, new HttpResponseMessage() { Content = objectContent }, out matchedMediaType), string.Format("CanWriteAs should have returned true for '{0}'.", type)); }); }
public void PostAndReadAsXml() { IEnumerable <TestData> testData = TestData.RepresentativeValueAndRefTypeTestDataCollection; TestDataVariations variations = TestDataVariations.AllNonInterfaces; XmlMediaTypeFormatter formatter = new XmlMediaTypeFormatter(); MediaTypeFormatter[] formatters = new MediaTypeFormatter[] { formatter }; TestDataAssert.Execute( testData, variations, "ObjectContent serializing using the Xml DataContractSerializer failed.", (type, obj) => { GenericWebService service = GenericWebService.GetServiceInstance(type, obj.GetType()); HttpServiceHostAssert.Execute( service, CreateXmlSerializerPostRequest(type, obj, XmlMediaType), (response) => { Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "Response status code should have been a 200."); formatter.SetSerializer(type, new DataContractSerializer(type, new Type[] { obj.GetType() })); string stringContent = response.Content.ReadAsString(); object readObj = response.Content.ReadAs(type, formatters); TestDataAssert.AreEqual(obj, readObj, string.Format("Failed to round trip type '{0}'. Content was:\r\n{1}.", type.Name, stringContent)); }); }); }
public void Constructor4() { TestDataAssert.Execute( TestData.RepresentativeValueAndRefTypeTestDataCollection, (type, obj) => { foreach (HttpStatusCode statusCode in HttpTestData.AllHttpStatusCodes) { foreach (IEnumerable <MediaTypeFormatter> formatterCollection in HttpTestData.AllFormatterCollections) { MediaTypeFormatter[] formatters = formatterCollection.ToArray(); HttpResponseMessage response = GenericTypeAssert.InvokeConstructor <HttpResponseMessage>( httpResponseMessageOfTType, type, new Type[] { type, typeof(HttpStatusCode), typeof(IEnumerable <MediaTypeFormatter>) }, new object[] { obj, statusCode, formatters }); Assert.AreEqual(statusCode, response.StatusCode, "StatusCode was not set."); GenericTypeAssert.IsCorrectGenericType <HttpResponseMessage>(response, type); ObjectContentAssert.IsCorrectGenericType(response.Content as ObjectContent, type); ObjectContentAssert.ContainsFormatters(response.Content as ObjectContent, formatters); } } }); }
public void CanReadTypeReturnsExpectedValues() { TestJsonMediaTypeFormatter formatter = new TestJsonMediaTypeFormatter(); TestDataAssert.Execute( TestData.RepresentativeValueAndRefTypeTestDataCollection, (type, obj) => { bool isSerializable = IsTypeSerializableWithJsonSerializer(type, obj); bool canSupport = formatter.CanReadTypeProxy(type); // If we don't agree, we assert only if the DCJ serializer says it cannot support something we think it should if (isSerializable != canSupport && isSerializable) { Assert.Fail(string.Format("CanReadType returned wrong value for '{0}'.", type)); } // Ask a 2nd time to probe whether the cached result is treated the same canSupport = formatter.CanReadTypeProxy(type); if (isSerializable != canSupport && isSerializable) { Assert.Fail(string.Format("2nd CanReadType returned wrong value for '{0}'.", type)); } }); }
public void CanReadTypeReturnsTrue() { SMediaTypeFormatter formatter = new SMediaTypeFormatter() { CallBase = true }; string[] legalMediaTypeStrings = HttpTestData.LegalMediaTypeStrings.ToArray(); foreach (string mediaType in legalMediaTypeStrings) { formatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue(mediaType)); } TestDataAssert.Execute( TestData.RepresentativeValueAndRefTypeTestDataCollection, (type, obj) => { foreach (string mediaType in legalMediaTypeStrings) { SStringContent content = new SStringContent("data") { CallBase = true }; content.Headers.ContentType = new MediaTypeHeaderValue(mediaType); Assert.IsTrue(formatter.CanReadAs(type, content), string.Format("CanReadType should have returned true for '{0}'.", type)); } }); }
public void HandleConvertsStringValuesReturnedFromOnHandle() { TestDataAssert.Execute( HttpTestData.ConvertableValueTypes, TestDataVariations.AllSingleInstances, "Handle failed", (type, obj) => { Type convertType = obj.GetType(); if (HttpParameterAssert.CanConvertToStringAndBack(convertType)) { HttpParameter hpd = new HttpParameter("aName", convertType); HttpParameter[] parameters = new HttpParameter[] { hpd }; SHttpOperationHandler handler = new SHttpOperationHandler(); handler.OnGetInputParameters01 = () => parameters; handler.OnGetOutputParameters01 = () => parameters; handler.OnHandleObjectArray = (oArray) => new object[] { obj.ToString() }; object[] result = handler.Handle(new object[] { obj }); Assert.IsNotNull(result, "Null result returned from Handle."); Assert.AreEqual(1, result.Length, "Handle returned wrong length array."); Assert.AreEqual(convertType, result[0].GetType(), "Value did not convert to right type."); Assert.AreEqual(obj.ToString(), result[0].ToString(), "Object did not convert to the right value."); } }); }
public void Constructor3() { TestDataAssert.Execute( TestData.RepresentativeValueAndRefTypeTestDataCollection, (type, obj) => { foreach (HttpMethod httpMethod in HttpTestData.AllHttpMethods) { foreach (Uri uri in TestData.UriTestData) { foreach (IEnumerable <MediaTypeFormatter> formatterCollection in HttpTestData.AllFormatterCollections) { MediaTypeFormatter[] formatters = formatterCollection.ToArray(); HttpRequestMessage request = GenericTypeAssert.InvokeConstructor <HttpRequestMessage>( httpRequestMessageOfTType, type, new Type[] { type, typeof(HttpMethod), typeof(Uri), typeof(IEnumerable <MediaTypeFormatter>) }, new object[] { obj, httpMethod, uri, formatters }); GenericTypeAssert.IsCorrectGenericType <HttpRequestMessage>(request, type); Assert.AreEqual(uri, request.RequestUri, "Uri property was not set."); Assert.AreEqual(httpMethod, request.Method, "Method property was not set."); ObjectContentAssert.IsCorrectGenericType(request.Content as ObjectContent, type); ObjectContentAssert.ContainsFormatters(request.Content as ObjectContent, formatters); } } } }); }
public void Test06() { TestUtils.DoTest(() => { var dirPair = GetDirPair(); TestDataAssert.DirectoriesAreEqual(dirPair.Expected, dirPair.Actual); }); }
public void ConvertTtoT() { TestDataAssert.Execute( HttpTestData.ConvertableValueTypes, (type, obj) => { Type convertType = obj == null ? type : obj.GetType(); HttpParameterValueConverter converter = HttpParameterValueConverter.GetValueConverter(convertType); object actualObj = converter.Convert(obj); TestDataAssert.AreEqual(obj, actualObj, string.Format("Conversion failed for {0}.", convertType.Name)); }); }
public void IsAssignableFromParameterReturnsTrueForAllValueTypes() { TestDataAssert.Execute( HttpTestData.ConvertableValueTypes, TestDataVariations.All, "ValueConverter failed", (type, obj) => { Type convertType = obj.GetType(); HttpParameter hpd = new HttpParameter("aName", convertType); Assert.IsTrue(hpd.IsAssignableFromParameter(convertType), string.Format("IsAssignableFrom({0}) was false.", convertType.Name)); }); }
public void IsAssignableFromParameterReturnsFalseForReferenceTypeToValueType() { TestDataAssert.Execute( HttpTestData.ConvertableValueTypes, TestDataVariations.AllSingleInstances, "ValueConverter failed", (type, obj) => { Type convertType = obj.GetType(); HttpParameter hpd = new HttpParameter("aName", convertType); Assert.IsFalse(hpd.IsAssignableFromParameter(typeof(PocoType)), string.Format("IsAssignableFrom({0}) was true.", convertType.Name)); }); }
public void ConvertObjectContentOfTtoT() { ObjectContentAssert.ExecuteForEachObjectContent( HttpTestData.RepresentativeValueAndRefTypeTestDataCollection, TestDataVariations.All, (objectContent, type, obj) => { Type convertType = obj == null ? type : obj.GetType(); HttpParameterValueConverter converter = HttpParameterValueConverter.GetValueConverter(convertType); object actualValue = converter.Convert(objectContent); TestDataAssert.AreEqual(obj, actualValue, "Convert failed to return T from ObjectContent<T>."); }); }
public void ConvertHttpResponseMessageOfTtoT() { ObjectContentAssert.ExecuteForEachHttpResponseMessage( HttpTestData.RepresentativeValueAndRefTypeTestDataCollection, TestDataVariations.All, (response, type, obj) => { Type convertType = obj == null ? type : obj.GetType(); HttpParameterValueConverter converter = HttpParameterValueConverter.GetValueConverter(convertType); object actualValue = converter.Convert(response); TestDataAssert.AreEqual(obj, actualValue, string.Format("Convert from HttpResponseMessage<T> to T failed for {0}.", convertType)); }); }
public void GetValueConverterReturnsConverterForAllValueTypes() { TestDataAssert.Execute( HttpTestData.ConvertableValueTypes, TestDataVariations.AllSingleInstances | TestDataVariations.AsNullable, "GetValueConverter failed", (type, obj) => { Type convertType = obj.GetType(); HttpParameterValueConverter converter = HttpParameterValueConverter.GetValueConverter(convertType); Assert.IsNotNull(converter, "GetValueConverter returned null."); Assert.AreEqual(convertType, converter.Type, "Converter Type was not correct."); }); }
public void Constructor() { TestDataAssert.Execute( TestData.RepresentativeValueAndRefTypeTestDataCollection, (type, obj) => { HttpRequestMessage request = GenericTypeAssert.InvokeConstructor <HttpRequestMessage>( httpRequestMessageOfTType, type); GenericTypeAssert.IsCorrectGenericType <HttpRequestMessage>(request, type); Assert.IsNotNull(request.Content, "default contructor should have set Content."); }); }
public void IsAssignableFromParameterOfTReturnsTrueForAllValueTypes() { TestDataAssert.Execute( HttpTestData.ConvertableValueTypes, TestDataVariations.All, "ValueConverter failed", (type, obj) => { Type convertType = obj.GetType(); HttpParameter hpd = new HttpParameter("aName", convertType); bool result = (bool)GenericTypeAssert.InvokeGenericMethod(hpd, isAssignableFromParameterOfTMethodName, convertType /*, new Type[0], new object[0]*/); Assert.IsTrue(result, string.Format("IsAssignableFromParameter<{0}>() was false.", convertType.Name)); }); }
public void Constructor3ThrowsWithNullFormatters() { TestDataAssert.Execute( TestData.RepresentativeValueAndRefTypeTestDataCollection, (type, obj) => { ExceptionAssert.ThrowsArgumentNull <TargetInvocationException>( "formatters", () => GenericTypeAssert.InvokeConstructor <HttpResponseMessage>( httpResponseMessageOfTType, type, new Type[] { type, typeof(IEnumerable <MediaTypeFormatter>) }, new object[] { obj, null })); }); }
public void ValueConverterReturnsConverterForAllValueTypes() { TestDataAssert.Execute( HttpTestData.ConvertableValueTypes, TestDataVariations.All, "ValueConverter failed", (type, obj) => { Type convertType = obj.GetType(); HttpParameter hpd = new HttpParameter("aName", convertType); HttpParameterValueConverter converter = hpd.ValueConverter; Assert.IsNotNull("ValueConverter returned null."); Assert.AreEqual(convertType, converter.Type, "ValueConverter was made for wrong type."); }); }
public void OutputParameterAreCreatedAllValueAndReferenceTypes() { TestDataAssert.Execute( HttpTestData.RepresentativeValueAndRefTypeTestDataCollection, TestDataVariations.All, "OutputParameters all types failed.", (type, obj) => { Type convertType = obj == null ? type : obj.GetType(); HttpParameter hpd = new HttpParameter("x", convertType); Type expectedType = typeof(HttpRequestMessage <>).MakeGenericType(convertType); HttpParameter expectedContentParameter = new HttpParameter("x", expectedType); RequestContentHandler handler = new RequestContentHandler(hpd, Enumerable.Empty <MediaTypeFormatter>()); HttpParameterAssert.ContainsOnly(handler.OutputParameters, expectedContentParameter, string.Format("Failed to initialize content parameter for {0}.", convertType.Name)); }); }
public void ConvertNullableOfTtoT() { TestDataAssert.Execute( HttpTestData.ConvertableValueTypes, TestDataVariations.AsNullable, "Nullable<T> to T failed.", (type, obj) => { Type nonNullableType = obj.GetType(); Assert.IsNull(Nullable.GetUnderlyingType(nonNullableType), "Test error: did not expect nullable object instance."); Assert.AreEqual(nonNullableType, Nullable.GetUnderlyingType(type), "Test error: expected only nullable types."); HttpParameterValueConverter converter = HttpParameterValueConverter.GetValueConverter(type); object actualValue = converter.Convert(obj); TestDataAssert.AreEqual(obj, actualValue, "Convert failed on Nullable<T> to T."); }); }
public void Constructor1() { TestDataAssert.Execute( TestData.RepresentativeValueAndRefTypeTestDataCollection, (type, obj) => { HttpRequestMessage request = GenericTypeAssert.InvokeConstructor <HttpRequestMessage>( httpRequestMessageOfTType, type, new Type[] { type }, new object[] { obj }); GenericTypeAssert.IsCorrectGenericType <HttpRequestMessage>(request, type); ObjectContentAssert.IsCorrectGenericType(request.Content as ObjectContent, type); }); }
public void InputParameterAreCreatedAllValueAndReferenceTypes() { TestDataAssert.Execute( HttpTestData.RepresentativeValueAndRefTypeTestDataCollection, TestDataVariations.All, "InputParameters for all types failed.", (type, obj) => { Type convertType = obj == null ? type : obj.GetType(); HttpParameter hpd = new HttpParameter("x", convertType); Type expectedType = typeof(HttpResponseMessage <>).MakeGenericType(convertType); HttpParameter expectedContentParameter = new HttpParameter("x", expectedType); ResponseContentHandler handler = new ResponseContentHandler(hpd, Enumerable.Empty <MediaTypeFormatter>()); HttpParameterAssert.Contains(handler.InputParameters, HttpParameter.RequestMessage, "Failed to initialize input parameters for RequestMessage."); HttpParameterAssert.Contains(handler.InputParameters, hpd, "Failed to initialize input parameter."); }); }
public void Constructor() { TestDataAssert.Execute( TestData.RepresentativeValueAndRefTypeTestDataCollection, (type, obj) => { foreach (HttpStatusCode statusCode in HttpTestData.AllHttpStatusCodes) { HttpResponseMessage response = GenericTypeAssert.InvokeConstructor <HttpResponseMessage>( httpResponseMessageOfTType, type, statusCode); GenericTypeAssert.IsCorrectGenericType <HttpResponseMessage>(response, type); Assert.IsNotNull(response.Content, "default contructor should have set Content."); Assert.AreEqual(statusCode, response.StatusCode, "StatusCode was not set."); } }); }
private void DoTest() { TestUtils.DoTest(() => { var dirPair = GetDirPair(); try { TestDataAssert.DirectoriesAreEqual(dirPair.Expected, dirPair.Actual); } catch (AssertionException ex) { Assert.AreEqual(GetExpectedMessage(dirPair), ex.Message, "AreEqual(ex.Message, Test01ExpectedMessage)"); return; } Assert.Fail("Expected exception wasn't thrown."); }); }
public void ReadFromStreamAsyncRoundTripsWriteToStreamAsync() { TestJsonMediaTypeFormatter formatter = new TestJsonMediaTypeFormatter(); HttpContentHeaders contentHeaders = new StringContent(string.Empty).Headers; TestDataAssert.Execute( TestData.RepresentativeValueAndRefTypeTestDataCollection, (type, obj) => { bool canSerialize = IsTypeSerializableWithJsonSerializer(type, obj) && HttpTestData.CanRoundTrip(type); if (canSerialize) { object readObj = null; StreamAssert.WriteAndRead( (stream) => TaskAssert.Succeeds(formatter.WriteToStreamAsync(type, obj, stream, contentHeaders, /*transportContext*/ null)), (stream) => readObj = TaskAssert.SucceedsWithResult(formatter.ReadFromStreamAsync(type, stream, contentHeaders))); TestDataAssert.AreEqual(obj, readObj, "Failed to round trip object."); } }); }
public void Constructor2() { TestDataAssert.Execute( TestData.RepresentativeValueAndRefTypeTestDataCollection, (type, obj) => { foreach (HttpStatusCode statusCode in HttpTestData.AllHttpStatusCodes) { HttpResponseMessage response = GenericTypeAssert.InvokeConstructor <HttpResponseMessage>( httpResponseMessageOfTType, type, new Type[] { type, typeof(HttpStatusCode) }, new object[] { obj, statusCode }); GenericTypeAssert.IsCorrectGenericType <HttpResponseMessage>(response, type); ObjectContentAssert.IsCorrectGenericType(response.Content as ObjectContent, type); Assert.AreEqual(statusCode, response.StatusCode, "StatusCode was not set."); } }); }
public void Constructor2() { TestDataAssert.Execute( TestData.RepresentativeValueAndRefTypeTestDataCollection, (type, obj) => { foreach (IEnumerable <MediaTypeFormatter> formatterCollection in HttpTestData.AllFormatterCollections) { MediaTypeFormatter[] formatters = formatterCollection.ToArray(); HttpRequestMessage request = GenericTypeAssert.InvokeConstructor <HttpRequestMessage>( httpRequestMessageOfTType, type, new Type[] { type, typeof(IEnumerable <MediaTypeFormatter>) }, new object[] { obj, formatters }); GenericTypeAssert.IsCorrectGenericType <HttpRequestMessage>(request, type); ObjectContentAssert.IsCorrectGenericType(request.Content as ObjectContent, type); ObjectContentAssert.ContainsFormatters(request.Content as ObjectContent, formatters); } }); }
public void InputParametersReturnsReflectedHttpParameters() { List <Type> types = new List <Type>(); TestDataAssert.Execute( TestData.RepresentativeValueAndRefTypeTestDataCollection, (type, obj) => { if (!types.Contains(type)) { types.Add(type); for (int i = 2; i <= 17; i++) { if (types.Count - i < 0) { break; } Type[] typeArray = types.Skip(types.Count - i).ToArray(); HttpOperationHandler genericHandler = GetGenericHandlerForTypes(typeArray); for (int j = 0; j < genericHandler.InputParameters.Count; j++) { HttpParameter parameter = genericHandler.InputParameters[j]; Assert.AreEqual(typeArray[j], parameter.Type, "The HttpParameter.Type should have been the same type as from the array."); if (i == 2) { Assert.AreEqual("input", parameter.Name, "The HttpParameter.Name should have been 'input'."); } else { string expectedName = "input" + (j + 1).ToString(); Assert.AreEqual(expectedName, parameter.Name, string.Format("The HttpParameter.Name should have been '{0}'.", expectedName)); } } } } }); }
/// <summary> /// Creates an instance of the generic <see cref="HttpRequestMessage"/> for every value /// in the given <paramref name="testDataCollection"/> and invokes the <paramref name="codeUnderTest"/>. /// </summary> /// <param name="testDataCollection">The collection of test data.</param> /// <param name="flags">The test variations.</param> /// <param name="codeUnderTest">The code to invoke with each <see cref="HttpRequestMessage"/>.</param> public static void ExecuteForEachHttpRequestMessage(IEnumerable <TestData> testDataCollection, TestDataVariations flags, Action <HttpRequestMessage, Type, object> codeUnderTest) { Assert.IsNotNull(testDataCollection, "testDataCollection cannot be null."); Assert.IsNotNull(codeUnderTest, "codeUnderTest cannot be null."); TestDataAssert.Execute( testDataCollection, flags, "Failed in ExecuteForEachHttpRequestMessage.", (type, obj) => { Type convertType = obj == null ? type : obj.GetType(); HttpRequestMessage request = (HttpRequestMessage)GenericTypeAssert.InvokeConstructor( typeof(HttpRequestMessage <>), convertType, new Type[] { convertType }, new object[] { obj }); codeUnderTest(request, type, obj); }); }