public bool Compare(string expected, string result, string schema) { //normalize the string. expected = expected.Trim().Replace("\r\n", "").Replace("\\\"", "\\"); result = result.Trim().Replace("\r\n", "").Replace("\\\"", "\\"); XDocument xdocExpected = null, xdocResult = null; Dictionary <string, object> jsonSchema = null; try { xdocExpected = XDocument.Parse(expected); xdocResult = XDocument.Parse(result); jsonSchema = JsonComparer.DeserializeObject(schema); return(Compare(xdocExpected.Document.FirstNode, xdocResult.Document.FirstNode, jsonSchema)); } catch (Exception ex) { string cause = "Expected XML"; if (xdocExpected != null && xdocResult == null) { cause = "Result XML"; } else if (xdocResult != null && jsonSchema == null) { cause = "Schema"; } else { cause = "Internal Application Error"; } Differences.Add(new CompareResultInfo { Schema = cause + " - " + ex.Message, Field = "Application Error", ApplicationError = true }); return(false); } }
private void RunAsync(bool secure, Action <TestRunResult> cb, Action <TestRunResult> onDone) { try { var ret = new TestRunResult(); ret.RunStarted = DateTime.Now; ret.Results = new List <MethodResult>(); var methods = this.Methods; if (!this.RunAll) { methods = this.Methods.Where(m => m.Include).ToList(); } foreach (var method in methods) { try { if (method.Params == null) { method.Params = string.Empty; } var methodRes = new MethodResult(); methodRes.RunStarted = DateTime.Now; methodRes.TestName = method.TestName; methodRes.TestDescription = method.TestDescription; var dict = new GSObject(); if (Program.MainForm.chkPreEncodeParams.Checked) { dict.ParseQuerystring(this.getEncodedValues(method.Params.Trim())); } else { dict.ParseQuerystring(method.Params.Trim()); } dict.Put("uid", dict.GetString("uid", null)); if (GetSecure().HasValue) { secure = GetSecure().Value; } GSRequest g = new GSRequest(this.APIKey, this.SecKey, method.Name, dict, secure); var req = new GSRequest(this.APIKey, this.SecKey, method.Name, dict, secure); var res = req.Send(); ret.Results.Add(methodRes); methodRes.Request = req; methodRes.Response = res; methodRes.SchemaValidation = new SchemaValidation("http://socialize.api.gigya.com/schema"); if (dict.GetString("format").Equals("xml", StringComparison.InvariantCultureIgnoreCase)) { var xc = new XmlComparer(); xc.Compare(method.Expected, res.GetResponseText(), method.Schema); methodRes.Differences = xc.Differences; var xdoc = XDocument.Parse(res.GetResponseText()); var xroot = (xdoc.Document.FirstNode as XContainer); var errCode = xroot.Element(XName.Get("errorCode", "urn:com:gigya:api")); int iErrCode; if (errCode != null && int.TryParse(errCode.Value, out iErrCode)) { methodRes.ErrorCode = iErrCode; var errMsg = xroot.Element(XName.Get("errorMessage", "urn:com:gigya:api")); methodRes.ErrorMessage = errMsg == null ? string.Empty : errMsg.Value; } else { methodRes.ErrorCode = 0; methodRes.ErrorMessage = ""; } if (methodRes.Differences.Count > 0 && methodRes.ErrorCode == 0) { methodRes.ErrorCode = -1; } var statusCode = xroot.Element(XName.Get("statusCode", "urn:com:gigya:api")); int iStatusCode; if (statusCode != null && int.TryParse(statusCode.Value, out iStatusCode)) { methodRes.StatusCode = iStatusCode; } else { methodRes.StatusCode = 200; } var sr = new StringReader(res.GetResponseText()); methodRes.Validation = methodRes.SchemaValidation.Validate(sr); } else { var jc = new JsonComparer(); jc.Compare(method.Expected, res.GetResponseText(), method.Schema); methodRes.Differences = jc.Differences; if (methodRes.Differences.Count > 0 && res.GetErrorCode() == 0) { methodRes.ErrorCode = -1; } else { methodRes.ErrorCode = res.GetErrorCode(); } methodRes.ErrorMessage = res.GetErrorMessage(); methodRes.StatusCode = res.GetData().GetInt("statusCode", 200); methodRes.Validation = true; } methodRes.RunEnded = DateTime.Now; //invoke the callback under the UI thread. Program.MainForm.Invoke(cb, ret); } catch (Exception ex) { MessageBox.Show(ex.Message, ex.Message); } } //invoke the callback under the UI thread. Program.MainForm.Invoke(cb, ret); //invoke the callback under the UI thread. Program.MainForm.Invoke(onDone, ret); ret.RunEnded = DateTime.Now; } catch (Exception ex) { MessageBox.Show(ex.Message, ex.Message); } }