예제 #1
0
        public static OpenAPICases[] RequestToResponse(OpenAPICases[] Cases, string appKey, string secret, bool flagUpdate = false)
        {
            MD5 md5 = new MD5CryptoServiceProvider();

            byte[] ciphertext;

            string requestBody = "";

            for (int i = 0; i < Cases.Length; i++)
            {
                requestBody = Cases[i].requestBody;

                string jsonFormatRequestBody             = ConvertJson.String2Json(requestBody);
                string jsonAndLocalTimeFormatRequestBody = EnergyViewDataDtoConvertor.GetEnergyViewDataWithLocalTime(jsonFormatRequestBody);
                Cases[i].formatRequestBody = jsonAndLocalTimeFormatRequestBody;

                WebRequest request = HttpWebRequest.Create(Cases[i].url);
                request.ContentType = "application/json";
                request.Headers.Add("X-Auth-AppKey", appKey);
                request.Method = "POST";
                ciphertext     = md5.ComputeHash(UnicodeEncoding.UTF8.GetBytes(appKey + requestBody + secret));
                string s = BitConverter.ToString(ciphertext).Replace("-", string.Empty);
                request.Headers.Add("X-Auth-Fig", s);

                var requestBodyBytes = Encoding.UTF8.GetBytes(requestBody);
                using (var requestBodyStream = request.GetRequestStream())
                {
                    requestBodyStream.Write(requestBodyBytes, 0, requestBodyBytes.Length);
                }
                using (var response = request.GetResponse())
                {
                    using (var responseStream = response.GetResponseStream())
                    {
                        using (var sr = new StreamReader(responseStream, Encoding.UTF8))
                        {
                            string originalResponseBody               = sr.ReadToEnd().ToString();
                            string jsonFormatResponseBody             = ConvertJson.String2Json(originalResponseBody);
                            string jsonAndLocalTimeFormatResponseBody = EnergyViewDataDtoConvertor.GetEnergyViewDataWithLocalTime(jsonFormatResponseBody);

                            //response body save to expectedResponseBody when update cases
                            if (true == flagUpdate)
                            {
                                Cases[i].expectedResponseBody       = originalResponseBody;
                                Cases[i].formatExpectedResponseBody = jsonAndLocalTimeFormatResponseBody;
                                Cases[i].result = "NA";
                            }
                            else
                            {
                                Cases[i].actualResponseBody       = originalResponseBody;
                                Cases[i].formatActualResponseBody = jsonAndLocalTimeFormatResponseBody;
                                Cases[i].result = "NA";
                            }
                        }
                    }
                }
            }
            return(Cases);
        }
예제 #2
0
        public static CompareReport CompareEnergyUseResponseBody(string expectedResponseBodyStr, string actualResponseBodyStr, out bool outResult)
        {
            string expectedResponseBody;
            string actualResponseBody;

            expectedResponseBody = expectedResponseBodyStr;
            actualResponseBody   = actualResponseBodyStr;

            CompareReport report = new CompareReport();

            #region Confirm that if respons body is empty

            CompareStringResult resultEmpty = IsResponseBodyEqual(expectedResponseBodyStr, actualResponseBodyStr);

            switch (resultEmpty)
            {
            case CompareStringResult.acEmpty:
                outResult           = false;
                report.errorMessage = "Actual Response body is empty, but expected not\n";
                return(report);

            case CompareStringResult.exEmpty:
                outResult           = false;
                report.errorMessage = "Expected Response body is empty, but actual not\n";
                return(report);

            case CompareStringResult.equal:
                outResult           = true;
                report.errorMessage = "Expected Response body is equal to actual response body.\n";
                return(report);

            case CompareStringResult.bothEmpty:
                outResult           = true;
                report.errorMessage = "Expected Response body and actual response bady are both empty.\n";
                return(report);

            default:
                break;
            }

            #endregion

            #region confirm that if response body return error code message

            //format6--当期望值或实际值是errorcode
            CompareStringErrorResult resultError = IsResponseBodyErrorMsgEqual(expectedResponseBodyStr, actualResponseBodyStr);

            switch (resultError)
            {
            case CompareStringErrorResult.acError:
                outResult           = false;
                report.errorMessage = "Actual Response body return error message, but expected not\n";
                return(report);

            case CompareStringErrorResult.exError:
                outResult           = false;
                report.errorMessage = "Expected Response body return error message, but actual not\n";
                return(report);

            case CompareStringErrorResult.notEqual:
                outResult           = false;
                report.errorMessage = "Expected and actual Response body return error message, but not equal\n";
                return(report);

            case CompareStringErrorResult.equal:
                outResult = true;
                return(report);

            default:
                break;
            }

            #endregion

            EnergyViewDataBody[] expectedData = EnergyViewDataDtoConvertor.GetEnergyViewDataDtoGroups(expectedResponseBody);
            EnergyViewDataBody[] actualData   = EnergyViewDataDtoConvertor.GetEnergyViewDataDtoGroups(actualResponseBody);

            #region confirm if there is no data but structure only
            //format5-完全没有TargetEnergyData内容的情况,即response只是:TargetEnergyData[]
            if (expectedData == null && actualData == null)
            {
                outResult = String.Equals(expectedData, actualData);

                if (!outResult)
                {
                    report.errorMessage = "Expected and actual have no data, but the structure are different as [] and TargetEnergyData[]";
                }

                return(report);
            }
            else if (!(expectedData == null) && actualData == null)
            {
                outResult           = false;
                report.errorMessage = "Expected response body have return value, but actual not, only have TargetEnergyData[] or []";
                return(report);
            }
            else if (expectedData == null && !(actualData == null))
            {
                outResult           = false;
                report.errorMessage = "Actual response body have return value, but expected not, only have TargetEnergyData[] or []";
                return(report);
            }

            #endregion

            #region confirm if the body response target number not equal
            //format2--当helader即target信息在实际结果存在,但期望不存在,即多返回了block,此时不需要比较里面的energydata
            //format3--当helader即target信息在实际结果中没有时,即少返回了block,此时不需要进一步比较里面的energydata
            if (expectedData.Length < actualData.Length)
            {
                outResult           = false;
                report.errorMessage = "actual response body block is more than Expected response body";
                report.detailedInfo = CompareBodyHelper.CompareResponseBodyByFiles(expectedResponseBody, actualResponseBody);
                return(report);
            }
            else if (expectedData.Length > actualData.Length)
            {
                outResult           = false;
                report.errorMessage = "actual response body block is less than Expected response body";
                report.detailedInfo = CompareBodyHelper.CompareResponseBodyByFiles(expectedResponseBody, actualResponseBody);
                return(report);
            }

            #endregion

            #region confirm if the body num is same but not equal
            //format1--当helader即target信息和期望的target一致时,此时要去验证内部energydata数据
            //format7:pie chart
            if (string.Equals(expectedResponseBody, actualResponseBody))
            {
                outResult = true;
                return(report);
            }
            else
            {
                outResult = false;
                report    = CompareMatchedResponseBody(expectedData, actualData);
                return(report);
            }
            #endregion
        }