public void AutoTamperResponseBefore(Session oSession)
 {
     if (bAutotestEnabled && apiItems != null && oSession.responseCode == 200)
     {
         //foreach (MockResponseRules singleRule in rules.Values)
         //{
         //    if (oSession.uriContains(singleRule.ApiPattern))
         //    {
         //        String orgResponseBody = Encoding.UTF8.GetString(oSession.responseBodyBytes);
         //        oSession.responseBodyBytes = Encoding.UTF8.GetBytes(singleRule.MockedResponse(orgResponseBody));
         //    }
         //}
         foreach (XMLApiItem item in apiItems.Values)
         {
             if (oSession.uriContains(item.Apiurl) && item.Response.Type == ResponseType.JSON || item.Response.Type == ResponseType.JSON_LIST)
             {
                 String orgResponseBody = Encoding.UTF8.GetString(oSession.responseBodyBytes);
                 JToken jtResponse      = JToken.Parse(orgResponseBody);
                 Dictionary <String, JsonResponseEntry> responseJsonEntry = JsonUtil.analyzeResponseEntry(jtResponse);
                 JsonXMLMatcher matcher = JsonUtil.matchJsonWithXML(responseJsonEntry, item.Response.Items, item.Response.Type == ResponseType.JSON_LIST);
                 item.Response.matchResult = matcher.matchResult;
                 if (matcher.matchResult == JsonXMLMatcher.MatchResult.MATCH)
                 {
                     logger.Log(item.Apiurl + " match result: MATCHED");
                 }
                 else
                 {
                     if (matcher.matchResult == JsonXMLMatcher.MatchResult.NOT_MATCH)
                     {
                         oSession["ui-backcolor"] = "red";
                     }
                     logger.Log(item.Apiurl + " match result:" + matcher.matchResultToString() + "\nroot->\n    " +
                                matcher.ToString().Replace("\n", "\n    "));
                 }
                 String newResponseBody = JsonUtil.assembleJson(responseJsonEntry).ToString();
                 String str             = Regex.Replace(newResponseBody.Replace(@"\\", @"\"), @"\s *", "").Replace(",\"", ", \"").Replace("\":", "\": ");
                 oSession.responseBodyBytes = Encoding.Default.GetBytes(str);
                 logger.Log("RessembleJson:\n" + str + "\n");
             }
         }
     }
 }
Example #2
0
        static JsonXMLItemMatcher matchJsonEntryWithXMLItem(JsonResponseEntry jsonEntry, XMLResponseItem xmlItem)
        {
            String             itemKey     = xmlItem.ItemName;
            JsonXMLItemMatcher itemMatcher = new JsonXMLItemMatcher();

            switch (jsonEntry.type)
            {
            case JsonResponseEntry.ResponseEntryType.DICT:
                if (xmlItem.Type == ResponseItemType.DICT)
                {
                    JsonXMLMatcher matcher = matchJsonWithXML(jsonEntry.subEntry, xmlItem.SubItems, false);
                    switch (matcher.matchResult)
                    {
                    case JsonXMLMatcher.MatchResult.LIST_OR_DICT_NULL:
                        itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.DICT_NULL;
                        itemMatcher.strResult       = itemKey + "->dict null\n    " + matcher.ToString().Replace("\n", "\n    ");
                        break;

                    case JsonXMLMatcher.MatchResult.MATCH:
                        itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.DICT_MATCHED;
                        break;

                    case JsonXMLMatcher.MatchResult.MATCH_PARTLY:
                        itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.LIST_OR_DICT_PARTLY_MATCHED;
                        itemMatcher.strResult       = itemKey + "-> dict partly match\n    " + matcher.ToString().Replace("\n", "\n    ");
                        break;

                    case JsonXMLMatcher.MatchResult.NOT_MATCH:
                        itemMatcher.strResult       = itemKey + "-> Not match\n    " + matcher.ToString().Replace("\n", "\n    ");
                        itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.TYPE_NOT_MATCH;
                        break;
                    }
                }
                else
                {
                    itemMatcher.strResult = itemKey + "-> Type not match in xml is "
                                            + XMLResponseItemTypeToString(xmlItem.Type)
                                            + " but in json is DICT\n";
                    itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.TYPE_NOT_MATCH;
                }
                break;

            case JsonResponseEntry.ResponseEntryType.DICT_NULL:
                if (xmlItem.Type == ResponseItemType.DICT)
                {
                    itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.DICT_NULL;
                    itemMatcher.strResult       = itemKey + "-> Dict null\n";
                }
                else
                {
                    itemMatcher.strResult = itemKey + "-> Type not match in xml is"
                                            + XMLResponseItemTypeToString(xmlItem.Type)
                                            + "but in json is DICT_NULL\n";
                    itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.TYPE_NOT_MATCH;
                }
                break;

            case JsonResponseEntry.ResponseEntryType.LIST_ENTRY:
                if (xmlItem.Type == ResponseItemType.LIST)
                {
                    JsonXMLMatcher matcher = matchJsonWithXML(jsonEntry.listEntry[0], xmlItem.SubItems, false);
                    switch (matcher.matchResult)
                    {
                    case JsonXMLMatcher.MatchResult.LIST_OR_DICT_NULL:
                        itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.DICT_NULL;
                        itemMatcher.strResult       = itemKey + "->list null\n" + matcher.ToString().Replace("\n", "\n    ");
                        break;

                    case JsonXMLMatcher.MatchResult.MATCH:
                        itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.DICT_MATCHED;
                        break;

                    case JsonXMLMatcher.MatchResult.MATCH_PARTLY:
                        itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.LIST_OR_DICT_PARTLY_MATCHED;
                        itemMatcher.strResult       = itemKey + "-> List partly match\n    " + matcher.ToString().Replace("\n", "\n    ");
                        break;

                    case JsonXMLMatcher.MatchResult.NOT_MATCH:
                        itemMatcher.strResult       = itemKey + "-> Not match\n    " + matcher.ToString().Replace("\n", "\n    ");
                        itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.TYPE_NOT_MATCH;
                        break;
                    }
                }
                else
                {
                    itemMatcher.strResult = itemKey + "-> Type not match in xml is "
                                            + XMLResponseItemTypeToString(xmlItem.Type)
                                            + " but in json is LIST_ENTRY\n";
                    itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.TYPE_NOT_MATCH;
                }
                break;

            case JsonResponseEntry.ResponseEntryType.LIST_STRING:
                if (xmlItem.Type == ResponseItemType.LIST)
                {
                    itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.LIST_STRING_MATCHED;
                }
                else
                {
                    itemMatcher.strResult = itemKey + "-> Type not match in xml is "
                                            + XMLResponseItemTypeToString(xmlItem.Type)
                                            + " but in json is LIST_STRING\n";
                    itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.TYPE_NOT_MATCH;
                }
                break;

            case JsonResponseEntry.ResponseEntryType.LIST_NULL:
                if (xmlItem.Type == ResponseItemType.LIST)
                {
                    itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.LIST_NULL;
                    itemMatcher.strResult       = itemKey + "-> List null\n";
                }
                else
                {
                    itemMatcher.strResult = itemKey + "-> Type not match in xml is "
                                            + XMLResponseItemTypeToString(xmlItem.Type)
                                            + " but in json is LIST_NULL\n";
                    itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.TYPE_NOT_MATCH;
                }
                break;

            case JsonResponseEntry.ResponseEntryType.STRING:
                if (xmlItem.Type == ResponseItemType.STRING)
                {
                    itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.STRING_MATCHED;
                }
                else
                {
                    itemMatcher.strResult = itemKey + "-> Type not match in xml is "
                                            + XMLResponseItemTypeToString(xmlItem.Type)
                                            + " but in json is STRING\n";
                    itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.TYPE_NOT_MATCH;
                }
                break;

            case JsonResponseEntry.ResponseEntryType.INT:
                if (xmlItem.Type == ResponseItemType.INT)
                {
                    itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.INT_MATCHED;
                }
                else
                {
                    itemMatcher.strResult = itemKey + "-> Type not match in xml is "
                                            + XMLResponseItemTypeToString(xmlItem.Type)
                                            + " but in json is INT\n";
                    itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.TYPE_NOT_MATCH;
                }
                break;

            case JsonResponseEntry.ResponseEntryType.NULL:
                if (xmlItem.Type == ResponseItemType.INT || xmlItem.Type == ResponseItemType.STRING)
                {
                    itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.NULL_MATCHED;
                }
                else
                {
                    itemMatcher.strResult = itemKey + "-> Type not match in xml is "
                                            + XMLResponseItemTypeToString(xmlItem.Type)
                                            + " but in json is NULL\n";
                    itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.TYPE_NOT_MATCH;
                }
                break;

            default:
                break;
            }

            if (itemMatcher.itemMatchResult > xmlItem.matchResult)
            {
                xmlItem.matchResult = itemMatcher.itemMatchResult;
            }
            return(itemMatcher);
        }