private static ResourceVerification GetResourceVerification(string responseFormat, int id, GeographyPropertyValues defaultValues, TestWebRequest request) { Assert.IsTrue(responseFormat == UnitTestsUtil.AtomFormat || responseFormat == UnitTestsUtil.JsonLightMimeType, "Response format {0} not recognized in GetResourceVerification.", responseFormat); DSPResourceSerializerFormat payloadFormat = responseFormat == UnitTestsUtil.AtomFormat ? DSPResourceSerializerFormat.Atom : DSPResourceSerializerFormat.Json; return(new ResourceVerification(request, payloadFormat, id, defaultValues)); }
public ResourceVerification(TestWebRequest request, DSPResourceSerializerFormat payloadFormat, int id, GeographyPropertyValues propertyValues) { this.Request = request; this.Id = id; this.PropertyValues = propertyValues; this.PayloadFormat = payloadFormat; }
private static void ExecuteUpdate(DSPResource resource, int id, TestWebRequest baseRequest, string httpMethod, string preferHeader, bool useBatch, DSPResourceSerializerFormat payloadFormat) { string payload = DSPResourceSerializer.WriteEntity(resource, payloadFormat); bool isPost = httpMethod == "POST"; bool expectedReturnContent = preferHeader == "return=representation" || isPost && preferHeader == null; string uriSuffix = isPost ? String.Empty : String.Format("({0})", id); TestWebRequest request = useBatch ? new InMemoryWebRequest() : baseRequest; request.RequestUriString = String.Format("/{0}s{1}", resource.ResourceType.Name, uriSuffix); request.HttpMethod = httpMethod; request.RequestVersion = "4.0;"; request.RequestMaxVersion = "4.0;"; request.RequestHeaders["Prefer"] = preferHeader; request.Accept = payloadFormat == DSPResourceSerializerFormat.Atom ? "application/atom+xml,application/xml" : UnitTestsUtil.JsonLightMimeType; request.RequestContentType = "application/atom+xml"; request.SetRequestStreamAsText(payload); if (useBatch) { var batchRequest = new BatchWebRequest(); var changeset = new BatchWebRequest.Changeset(); changeset.Parts.Add((InMemoryWebRequest)request); batchRequest.Changesets.Add(changeset); batchRequest.SendRequest(baseRequest); Assert.IsTrue(UnitTestsUtil.IsSuccessStatusCode(baseRequest.ResponseStatusCode), "Unexpected error occurred on batch."); } else { request.SendRequest(); } Assert.IsTrue(UnitTestsUtil.IsSuccessStatusCode(request.ResponseStatusCode), "Unexpected error occurred when sending the request."); if (expectedReturnContent) { // If the request is expected to return content, verify there were no instream errors Exception e = request.ParseResponseInStreamError(); string errorMessage = e != null ? e.Message : string.Empty; Assert.IsNull(e, "Expected no exception, but got the following error", errorMessage); } }
/// <summary> /// Serializes the specified entity resource /// </summary> /// <param name="entity">The entity resource to serialize.</param> /// <param name="format">The format in which to serialize.</param> /// <param name="output">The stream to serialize to.</param> /// <param name="encoding">The text encoding to use for the serialization.</param> public static void WriteEntity(DSPResource entity, DSPResourceSerializerFormat format, Stream output, Encoding encoding) { CreateSerializerAndRun(format, output, encoding, (serializer) => { serializer.WriteEntity(entity); }); }
private static string CreateSerializerAndRunIntoString(DSPResourceSerializerFormat format, Action<DSPResourceSerializer> action) { using (MemoryStream output = new MemoryStream()) { CreateSerializerAndRun(format, output, Encoding.UTF8, action); output.Position = 0; return (new StreamReader(output, Encoding.UTF8)).ReadToEnd(); } }
private static void CreateSerializerAndRun(DSPResourceSerializerFormat format, Stream output, Encoding encoding, Action<DSPResourceSerializer> action) { DSPResourceSerializer serializer = null; if (format == DSPResourceSerializerFormat.Atom) { serializer = new DSPResourceAtomSerializer(output, encoding); } else { Debug.Assert(format == DSPResourceSerializerFormat.Json, "Only ATOM and JSON formats are supported."); serializer = new DSPResourceJsonSerializer(output, encoding); } using (serializer) { action(serializer); } }
public static string WriteProperty(ResourceProperty resourceProperty, object value, DSPResourceSerializerFormat format) { return CreateSerializerAndRunIntoString(format, (serializer) => { serializer.WriteProperty(resourceProperty, value); }); }
public static void WriteProperty(ResourceProperty resourceProperty, object value, DSPResourceSerializerFormat format, Stream output, Encoding encoding) { CreateSerializerAndRun(format, output, encoding, (serializer) => { serializer.WriteProperty(resourceProperty, value); }); }
/// <summary> /// Serializes the specified entity resource into a string /// </summary> /// <param name="entity">The entity resource to serialize.</param> /// <param name="format">The format in which to serialize.</param> /// <returns>The entity serialized as a string</returns> public static string WriteEntity(DSPResource entity, DSPResourceSerializerFormat format) { return CreateSerializerAndRunIntoString(format, (serializer) => { serializer.WriteEntity(entity); }); }