/// <summary> /// Populates <paramref name="target"/> with static and <paramref name="source"/> values /// as defined by <paramref name="fieldMap"/>. /// </summary> /// <param name="source">The application from which to get field values.</param> /// <param name="fieldMap">A definition of field mappings.</param> /// <param name="target">The target object to populate with mapped key/values.</param> internal void Map(Application source, MappedFieldList fieldMap, JObject target) { foreach (MappedField map in fieldMap) { switch (map.MapType) { case MapType.Value: case MapType.PrivateValue: target.Add(map.Target, map.Source); break; case MapType.Field: object tokenValue = source.GetTokenValue(map.Source); if (tokenValue == null) { target.Add(map.Target, string.Empty); } else if (tokenValue is IEnumerable<object>) { target.Add(map.Target, JArray.FromObject(tokenValue)); } else { target.Add(map.Target, tokenValue.ToString()); } break; default: throw new InvalidOperationException(string.Format(ExceptionMessages.InvalidMapType, map.MapType)); } } }
public void DontTimeout() { const int configureTimeoutSeconds = 3; const int serverWaitSeconds = 2; MappedFieldList fieldList = new MappedFieldList { new MappedField { Target = REQUEST_TIMEOUT_KEY, MapType = MapType.Value, Source = serverWaitSeconds.ToString() } }; WebhookWorkflowTransitionAction action = new WebhookWorkflowTransitionAction { ServiceEndpointId = "1", RequestFieldMap = fieldList }; EventActionWebhookResponseHandler handler = new EventActionWebhookResponseHandler(new HttpEndpointCommunicator(new EmptyHttpEndpointCommunicationLogger(), configureTimeoutSeconds)) { Endpoints = this.Endpoints }; EventResult result = handler.Handle(null, action, new Application(), new PageList(), this.getApplicationAccessFn); Assert.IsTrue(result.Processed); }
public void MapArrayCorrectType() { string json = AssemblyResourceReader.ReadAsString("Test_Data.States.json"); JToken token = JToken.Parse(json); MappedFieldList fieldList = new MappedFieldList(); JToken mapped = new HttpEndpointResponseMapper().MapToken(token, null, fieldList, null); Assert.AreEqual(JTokenType.Array, mapped.Type); }
/// <summary> /// Updates blank <see cref="MapType.PrivateValue"/> mappings in <paramref name="list"/> /// with the real value from <paramref name="source"/>. /// </summary> /// <param name="list">The <see cref="MappedFieldList"/> that acts as the this instance.</param> /// <param name="source">The existing <see cref="MappedFieldList"/> that contains the real values.</param> public static void RestorePrivateValues(this MappedFieldList list, MappedFieldList source) { IEnumerable<MappedField> privateData = list.Where(m => m.MapType == MapType.PrivateValue && string.IsNullOrEmpty(m.Source)); foreach (MappedField fieldMap in privateData) { MappedField existingFieldMap = source.FirstOrDefault(m => m.MapType == MapType.PrivateValue && m.Target == fieldMap.Target); fieldMap.Source = existingFieldMap != null ? existingFieldMap.Source : fieldMap.Source; } }
public void TestInitialize() { this.FieldList = new MappedFieldList { new MappedField { MapType = MapType.Content, Target = "Foo", Source = "Bar" }, new MappedField { MapType = MapType.Value, Target = "Baz", Source = "Qux" } }; this.DoTestInitialize(); }
/// <summary> /// Creates and returns a GET request. /// </summary> /// <param name="baseUrl">The base URL, to which QueryString parameters will be appended.</param> /// <param name="application">The application.</param> /// <param name="requestFieldMap">The list of request fields to map.</param> /// <param name="obfuscatedRequestBody">The request body.</param> /// <returns>A new <see cref="HttpWebRequest" /> that uses the GET method.</returns> private HttpWebRequest CreateGetRequest(string baseUrl, Application application, MappedFieldList requestFieldMap, out string obfuscatedRequestBody) { Dictionary<string, string> parameterData = this.GetHttpVariableRequestContent(application, requestFieldMap); string url = new UrlBuilder().Create(baseUrl, parameterData); Uri myUri = new Uri(url); obfuscatedRequestBody = this.ObfuscateRequest(myUri.Query, HttpResources.HttpRequestFieldValueLocator, requestFieldMap); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = WebRequestMethods.Http.Get; return request; }
/// <summary> /// Merge <paramref name="source"/> into the current instance, /// using target property as the key. /// </summary> /// <param name="list">The <see cref="MappedFieldList"/> that acts as the this instance.</param> /// <param name="source">The list to merge into the current instance.</param> /// <remarks> /// When both the current instance and <paramref name="source"/> contain a mapping /// for the same target, the mapping from <paramref name="source"/> will take precedence. /// </remarks> public static void MergeOnTarget(this MappedFieldList list, MappedFieldList source) { foreach (var item in source) { if (list.ContainsTarget(item.Target)) { list.RemoveByTarget(item.Target); } } list.AddRange(source); }
/// <summary> /// Creates a new <see cref="HttpWebRequest" />. /// </summary> /// <param name="endpoint">Contains information on what sort of request to create.</param> /// <param name="application">The application containing values to map.</param> /// <param name="requestFieldMap">The list of request fields to map.</param> /// <param name="obfuscatedRequestBody">The request body.</param> /// <returns>A new <see cref="HttpWebRequest" />.</returns> internal HttpWebRequest Create(ServiceEndpoint endpoint, Application application, MappedFieldList requestFieldMap, out string obfuscatedRequestBody) { obfuscatedRequestBody = string.Empty; if (endpoint.Method == HttpRequestMethod.GET && endpoint.RequestFormat != ServiceDataFormat.HttpVariables) { throw new InvalidOperationException(string.Format(ExceptionMessages.IncompatibleRequestFormat, endpoint.RequestFormat, endpoint.Method)); } return (endpoint.Method == HttpRequestMethod.GET) ? this.CreateGetRequest(endpoint.Url, application, requestFieldMap, out obfuscatedRequestBody) : this.CreatePostRequest(endpoint, application, requestFieldMap, out obfuscatedRequestBody); }
public void ApplicationDataMissingValue() { Application application = new Application(new ApplicationData(), Guid.NewGuid().ToString("N")); MappedFieldList fieldMap = new MappedFieldList { new MappedField { MapType = MapType.Field, Source = "ReleaseYear", Target = "FinalDestination2" } }; JObject jobj = new JObject(); new JsonEndpointRequestMapper().Map(application, fieldMap, jobj); Assert.AreEqual(string.Empty, jobj.GetValue("FinalDestination2")); }
public void ApplicationDataMissingValue() { Application application = new Application(new ApplicationData(), Guid.NewGuid().ToString("N")); MappedFieldList fieldMap = new MappedFieldList { new MappedField { MapType = MapType.Field, Source = "ReleaseYear", Target = "FinalDestination2" } }; Dictionary<string, string> dictionary = new Dictionary<string, string>(); new HttpVarEndpointRequestMapper().Map(application, fieldMap, dictionary); Assert.AreEqual(string.Empty, dictionary["FinalDestination2"]); }
public void ApplicationDataMissingValue() { Application application = new Application(new ApplicationData(), Guid.NewGuid().ToString("N")); MappedFieldList fieldMap = new MappedFieldList { new MappedField { MapType = MapType.Field, Source = "ReleaseYear", Target = "FinalDestination2" } }; new XmlEndpointRequestMapper().Map(application, fieldMap, this.xmlDoc); XmlNodeList nodeList = this.xmlDoc.GetElementsByTagName("FinalDestination2"); Assert.AreEqual(1, nodeList.Count); Assert.AreEqual(string.Empty, nodeList[0].InnerText); }
/// <summary> /// Populates <paramref name="target"/> with values from <paramref name="source"/> and <paramref name="response"/> /// as defined by <paramref name="fieldMap"/>. It also returns a new <see cref="Newtonsoft.Json.Linq.JArray"/> with mapped values. /// </summary> /// <param name="source">The response body from which to get values.</param> /// <param name="response">The response object from which to get values.</param> /// <param name="fieldMap">A definition of field mappings.</param> /// <param name="target">The target object to populate with mapped key/values.</param> /// <param name="maxResults">The maximum number of results to return.</param> /// <returns>A new <see cref="Newtonsoft.Json.Linq.JArray"/> with mapped values.</returns> public JArray MapArray(JArray source, HttpWebResponse response, MappedFieldList fieldMap, ApplicationData target, int? maxResults = null) { int max = maxResults ?? int.MaxValue; JArray outArray = new JArray(); IEnumerator<JObject> enumerator = source.Children<JObject>().GetEnumerator(); int i = 0; while (enumerator.MoveNext() && i < max) { outArray.Add(this.MapObject(enumerator.Current, response, fieldMap, target)); i++; } return outArray; }
public void MapArrayMaxResults() { const int max = 5; string json = AssemblyResourceReader.ReadAsString("Test_Data.States.json"); JToken token = JToken.Parse(json); MappedFieldList fieldList = new MappedFieldList { new MappedField { MapType = MapType.Content, Target = "Value", Source = "abbreviation" } }; JArray mapped = new HttpEndpointResponseMapper().MapToken(token, null, fieldList, null, max) as JArray; Assert.AreEqual(max, mapped.Count); }
/// <summary> /// Creates a returns a POST request. /// </summary> /// <param name="endpoint">Contains information on what sort of request to create.</param> /// <param name="application">The application.</param> /// <param name="requestFieldMap">The list of request fields to map.</param> /// <param name="obfuscatedRequestBody">The request body.</param> /// <returns>A new <see cref="HttpWebRequest" /> uses the POST method.</returns> private HttpWebRequest CreatePostRequest(ServiceEndpoint endpoint, Application application, MappedFieldList requestFieldMap, out string obfuscatedRequestBody) { string content = string.Empty; string contentType = string.Empty; obfuscatedRequestBody = string.Empty; switch (endpoint.RequestFormat) { case ServiceDataFormat.HttpVariables: contentType = InternetMediaType.ApplicationXFormEncodedData; content = this.GetHttpVariableRequestContent(application, requestFieldMap).ToHttpVariables(); obfuscatedRequestBody = this.ObfuscateRequest(content, HttpResources.HttpRequestFieldValueLocator, requestFieldMap); break; case ServiceDataFormat.Json: contentType = InternetMediaType.ApplicationJson; content = this.GetJsonRequestContent(application, requestFieldMap); obfuscatedRequestBody = this.ObfuscateRequest(content, HttpResources.JsonRequestFieldValueLocator, requestFieldMap); break; case ServiceDataFormat.Xml: contentType = InternetMediaType.TextXml; content = this.GetXmlRequestContent(endpoint.RequestRoot, application, requestFieldMap); obfuscatedRequestBody = this.ObfuscateRequest(content, HttpResources.XmlRequestFieldValueLocator, requestFieldMap); break; } HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endpoint.Url); request.Method = WebRequestMethods.Http.Post; request.ContentType = contentType; byte[] bytes = Encoding.UTF8.GetBytes(content); using (Stream stream = request.GetRequestStream()) { stream.Write(bytes, 0, bytes.Length); } return request; }
/// <summary> /// Populates <paramref name="target"/> with static and <paramref name="source"/> values /// as defined by <paramref name="fieldMap"/>. /// </summary> /// <param name="source">The application from which to get field values.</param> /// <param name="fieldMap">A definition of field mappings.</param> /// <param name="target">The target object to populate with mapped key/values.</param> internal void Map(Application source, MappedFieldList fieldMap, XmlDocument target) { XmlNode root = target.FirstChild; foreach (MappedField map in fieldMap) { switch (map.MapType) { case MapType.Value: case MapType.PrivateValue: root.AddElement(map.Target, map.Source); break; case MapType.Field: object tokenValue = source.GetTokenValue(map.Source); if (tokenValue == null) { root.AddElement(map.Target, string.Empty); } else { var value = tokenValue as IEnumerable<object>; if (value != null) { this.GetNestedValue(value, map, root); } else { root.AddElement(map.Target, tokenValue.ToString()); } } break; default: throw new InvalidOperationException(string.Format(ExceptionMessages.InvalidMapType, map.MapType)); } } }
public void MapArrayResultsToField() { string json = AssemblyResourceReader.ReadAsString("Test_Data.Errors.json"); JToken token = JToken.Parse(json); MappedFieldList fieldList = new MappedFieldList { new MappedField { MapType = MapType.Content, Target = "Test_field", Source = "Server_field" } }; ApplicationData applicationData = new ApplicationData { { "Test_field", null } }; JObject mapped = new HttpEndpointResponseMapper().MapToken(token, null, fieldList, applicationData) as JObject; JToken first = mapped.First; Assert.AreEqual(2, first.Values().Count()); Assert.AreEqual(2, applicationData.GetValue<string[]>("Test_field").Count()); }
public void RequestFieldMap() { MappedFieldList fieldList = new MappedFieldList { new MappedField { Target = "Field1", MapType = MapType.Value, Source = "Value1" }, new MappedField { Target = "Field2", MapType = MapType.Value, Source = "Value2" } }; WebhookWorkflowTransitionAction action = new WebhookWorkflowTransitionAction { ServiceEndpointId = "1", RequestFieldMap = fieldList }; EventActionWebhookResponseHandler handler = new EventActionWebhookResponseHandler(new HttpEndpointCommunicator(new EmptyHttpEndpointCommunicationLogger())) { Endpoints = this.Endpoints }; EventResult result = handler.Handle(null, action, new Application(), new PageList(), this.getApplicationAccessFn); Assert.IsTrue(result.Processed); }
/// <summary> /// Obfuscates private keys by replacing them with a constant value. /// </summary> /// <param name="content">The request content to be obfuscated.</param> /// <param name="searchFormat">The search format string.</param> /// <param name="requestFieldMap">The request field map.</param> /// <returns><paramref name="content"/> with private keys obfuscated.</returns> private string ObfuscateRequest(string content, string searchFormat, MappedFieldList requestFieldMap) { return requestFieldMap.Where(f => f.MapType == MapType.PrivateValue) .Select(field => string.Format(searchFormat, field.Target, field.Source)) .Aggregate(content, (current, pattern) => Regex.Replace(current, pattern, HttpResources.ObfuscatedFieldReplacement)); }
protected override void DoTestInitialize() { this.secondList = new MappedFieldList { new MappedField { MapType = MapType.Value, Target = "Fu", Source = "Bar" }, new MappedField { MapType = MapType.Content, Target = "Baz", Source = "Quxx" } }; }
public void UpdateApplication() { Application application = new Application(); application.ApplicationData.Add("Field1", string.Empty); application.ApplicationData.Add("Field2", string.Empty); MappedFieldList fieldList = new MappedFieldList { new MappedField { Target = "Field1", MapType = MapType.Content, Source = "Foo" }, new MappedField { Target = "Field2", MapType = MapType.Content, Source = "Baz" } }; WebhookWorkflowTransitionAction action = new WebhookWorkflowTransitionAction { ServiceEndpointId = "1", ResponseFieldMap = fieldList }; EventActionWebhookResponseHandler handler = new EventActionWebhookResponseHandler(new HttpEndpointCommunicator(new EmptyHttpEndpointCommunicationLogger())) { Endpoints = this.Endpoints }; EventResult result = handler.Handle(null, action, application, new PageList(), this.getApplicationAccessFn); Assert.AreEqual("Bar", result.UpdatedApplication.ApplicationData["Field1"]); Assert.AreEqual("Qux", result.UpdatedApplication.ApplicationData["Field2"]); }
/// <summary> /// Gets the request content as a HTTP variable string. /// </summary> /// <param name="application">The application.</param> /// <param name="requestFieldMap">The request field map.</param> /// <returns>The request content as a dictionary of request variables.</returns> private Dictionary<string, string> GetHttpVariableRequestContent(Application application, MappedFieldList requestFieldMap) { HttpVarEndpointRequestMapper fieldMapper = new HttpVarEndpointRequestMapper(); Dictionary<string, string> parameters = new Dictionary<string, string>(); fieldMapper.Map(application, requestFieldMap, parameters); return parameters; }
public void RepeaterRowValues() { Application application = new Application(new ApplicationData(), Guid.NewGuid().ToString("N")); List<NestedDictionary> repeater = new List<NestedDictionary>(); NestedDictionary child1 = new NestedDictionary(); child1["Title"] = "The Final Destination (4)"; child1["Year"] = "2009"; NestedDictionary child2 = new NestedDictionary(); child2["Title"] = "Final Destination 5"; child2["Year"] = "2011"; repeater.Add(child1); repeater.Add(child2); application.ApplicationData["Repeater"] = repeater.ToArray(); MappedFieldList fieldMap = new MappedFieldList { new MappedField { MapType = MapType.Field, Source = "Repeater", Target = "FinalDestinations" } }; JObject jobj = new JObject(); new JsonEndpointRequestMapper().Map(application, fieldMap, jobj); Assert.AreEqual("2009", jobj.GetValue("FinalDestinations").First["Year"]); Assert.AreEqual("2011", jobj.GetValue("FinalDestinations").First.Next["Year"]); }
public void StaticValue() { Application application = new Application(new ApplicationData(), Guid.NewGuid().ToString("N")); MappedFieldList fieldMap = new MappedFieldList { new MappedField { MapType = MapType.Value, Source = "Released Year 2000", Target = "FinalDestination1" } }; JObject jobj = new JObject(); new JsonEndpointRequestMapper().Map(application, fieldMap, jobj); Assert.AreEqual("Released Year 2000", jobj.GetValue("FinalDestination1")); }
public void ApplicationProperty() { Application application = new Application(new ApplicationData(), Guid.NewGuid().ToString("N")); MappedFieldList fieldMap = new MappedFieldList { new MappedField { MapType = MapType.Field, Source = "ApplicationId", Target = "FinalDestination1" } }; JObject jobj = new JObject(); new JsonEndpointRequestMapper().Map(application, fieldMap, jobj); Assert.AreEqual(application.ApplicationId, jobj.GetValue("FinalDestination1")); }
public void LocalTestInitialize() { this.fieldList = new MappedFieldList { new MappedField { MapType = MapType.Value, Target = "Foo", Source = "Bar" }, new MappedField { MapType = MapType.PrivateValue, Target = "Baz", Source = "Qux" }, new MappedField { MapType = MapType.PrivateValue, Target = "Corge", Source = "Grault" }, new MappedField { MapType = MapType.Value, Target = "Garply", Source = "Waldo" } }; }
public void StatusCodeNotOk() { MappedFieldList fieldList = new MappedFieldList { new MappedField { Target = REQUEST_HTTP_STATUS_CODE_KEY, MapType = MapType.Value, Source = ((int)HttpStatusCode.InternalServerError).ToString() } }; WebhookWorkflowTransitionAction action = new WebhookWorkflowTransitionAction { ServiceEndpointId = "1", RequestFieldMap = fieldList }; EventActionWebhookResponseHandler handler = new EventActionWebhookResponseHandler(new HttpEndpointCommunicator(new EmptyHttpEndpointCommunicationLogger())) { Endpoints = this.Endpoints }; EventResult result = handler.Handle(null, action, new Application(), new PageList(), this.getApplicationAccessFn); Assert.IsFalse(result.Processed); }
public void StatusCodeOk() { MappedFieldList fieldList = new MappedFieldList(); WebhookWorkflowTransitionAction action = new WebhookWorkflowTransitionAction { ServiceEndpointId = "1", RequestFieldMap = fieldList }; EventActionWebhookResponseHandler handler = new EventActionWebhookResponseHandler(new HttpEndpointCommunicator(new EmptyHttpEndpointCommunicationLogger())) { Endpoints = this.Endpoints }; EventResult result = handler.Handle(null, action, new Application(), new PageList(), this.getApplicationAccessFn); Assert.IsTrue(result.Processed); }
/// <summary> /// Gets the request content as a JSON string. /// </summary> /// <param name="application">The application.</param> /// <param name="requestFieldMap">The request field map.</param> /// <returns>The request content as a JSON string.</returns> private string GetJsonRequestContent(Application application, MappedFieldList requestFieldMap) { JsonEndpointRequestMapper fieldMapper = new JsonEndpointRequestMapper(); JObject parameters = new JObject(); fieldMapper.Map(application, requestFieldMap, parameters); return JsonConvert.SerializeObject(parameters); }
public void LocalTestInitialize() { this.fieldList = new MappedFieldList { new MappedField { MapType = MapType.Value, Target = "Foo", Source = "Not Bar" }, new MappedField { MapType = MapType.PrivateValue, Target = "Baz", Source = string.Empty }, new MappedField { MapType = MapType.PrivateValue, Target = "Corge", Source = "Not Grault" }, new MappedField { MapType = MapType.Value, Target = "Garply", Source = string.Empty }, new MappedField { MapType = MapType.PrivateValue, Target = "Fred", Source = string.Empty }, new MappedField { MapType = MapType.PrivateValue, Target = "Plugh", Source = "Thud" } }; this.existing = new MappedFieldList { new MappedField { MapType = MapType.Value, Target = "Foo", Source = "Bar" }, new MappedField { MapType = MapType.PrivateValue, Target = "Baz", Source = "Qux" }, new MappedField { MapType = MapType.PrivateValue, Target = "Corge", Source = "Grault" }, new MappedField { MapType = MapType.Value, Target = "Garply", Source = "Waldo" } }; }
/// <summary> /// Gets the request content as an XML string. /// </summary> /// <param name="rootName">The name of the root container node.</param> /// <param name="application">The application.</param> /// <param name="requestFieldMap">The request field map.</param> /// <returns>The request content as an XML string.</returns> private string GetXmlRequestContent(string rootName, Application application, MappedFieldList requestFieldMap) { XmlEndpointRequestMapper xmlFieldMapper = new XmlEndpointRequestMapper(); XmlDocument doc = new XmlDocument(); doc.AddElement(rootName); xmlFieldMapper.Map(application, requestFieldMap, doc); return doc.OuterXml; }