Exemplo n.º 1
0
        internal void AddRequest(Session httpReq, Page page, bool secondary = true, bool beanShell = true, bool gxApp = false)
        {
            if (IsInBlackList(httpReq))
            {
                return;
            }

            // find the Page of the request in the Page tree
            if (page != null)
            {
                page = page.FindSubPage(httpReq);
            }

            var referer = httpReq.oRequest.headers["Referer"];

            // if isn't static resource. Can be a follow redirect of last request or a new request
            if (httpReq.IsPrimaryRequest())
            {
                // this means the current primaryRequest is first request of the step
                if (Requests.Count == 0)
                {
                    var req = CreatePageRequest(httpReq, this, page, secondary, beanShell, gxApp);
                    Requests.Add(req);
                }
                else
                {
                    var lastPageRequest = Requests.Last();

                    // todo: should use the referer request instead of LastPrimaryRequest?
                    var lastFollowRedirectPage          = lastPageRequest.GetLastPrimaryRequest();
                    var fiddlerSessionOfLastPageRequest = lastFollowRedirectPage.FiddlerSession;

                    // the response code of the previous request was a redirect
                    if (fiddlerSessionOfLastPageRequest.IsRedirectByResponseCode())
                    {
                        var followRedirect = new AbstractFollowRedirect(httpReq, RedirectType.ByResponseCode, page);
                        followRedirect.Validations.Add(CreateDefaultValidationFromRequest(httpReq));

                        // if there are parameters in the URL, need to extract them to use in current request
                        var urlParameters = GetParametersFromURL(httpReq.fullUrl);
                        if (urlParameters != string.Empty)
                        {
                            const string regExp = @"Location: http://.*\?(.*)";

                            // if there aren't parameters to extract and to use created in the comparer, then create it here
                            if (page == null)
                            {
                                var paramName = NameGenerator.GetInstance().GetNewName();

                                // add parameter to extract
                                var newParam = new JMeterRegExParameter(
                                    ExtractFrom.Headers,
                                    new List <UseIn> {
                                    UseIn.Url
                                },
                                    paramName,
                                    regExp,
                                    "$1$",
                                    urlParameters,
                                    "Used in pages: { " + httpReq.id + " } Original value: " + urlParameters);

                                lastFollowRedirectPage.AddParameterToExtract(newParam);

                                // modify url to use the parameter
                                followRedirect.AddParameterToUse(newParam);
                            }
                            else
                            {
                                var paramName = NameGenerator.GetInstance().GetNewName();

                                // there are alredy parameters to use in the URL, don't add a newone
                                if (page.GetParametersToUseInURL().Count == 0)
                                {
                                    var param = new Parameter
                                    {
                                        ExtractFromSection = FiddlerSessionComparer.ExtractFrom.Headers,
                                        ExtractFromPage    = lastFollowRedirectPage.InfoPage,
                                        VariableName       = paramName,
                                        Values             = new List <string> {
                                            urlParameters
                                        },
                                        ParamExtractor = new RegExpExtractor(1, regExp),
                                    };

                                    param.AddParameterPage(page, UseToReplaceIn.Url, urlParameters, "${" + paramName + "}");

                                    lastFollowRedirectPage.InfoPage.AddPreparedParameterToExtract(param, ParameterContext.Default);
                                    page.AddPreparedParameterToUse(param);
                                }
                            }
                        }

                        lastPageRequest.AddFollowRedirect(followRedirect);

                        lastFollowRedirectPage.Validations.Clear();
                        lastFollowRedirectPage.Validations.Add(CreateResponseCodeValidation(lastPageRequest.ResponseCode));
                    }
                    // the response code of the previous request was a redirect
                    else if (fiddlerSessionOfLastPageRequest.IsRedirectByJavaScript())
                    {
                        var validations = new List <AbstractValidation> {
                            CreateDefaultValidationFromRequest(httpReq)
                        };
                        lastPageRequest.AddFollowRedirect(httpReq, RedirectType.ByJavaScript, page, validations);
                        lastPageRequest.Validations.Clear();
                        lastPageRequest.Validations.Add(CreateResponseCodeValidation(200));

                        // Add validation to the followRedirect: HTTP "200" + Content "Redirect"
                        if (fiddlerSessionOfLastPageRequest.IsGenexusRedirect())
                        {
                            lastPageRequest.Validations.Add(CreateAppearTextValidation("redirect", "This HTML should be a javascript redirect", false, true));

                            // This is done just when comparing two fiddler sessions
                            ////var parametersExtractor = CreateRegExpExtractorToGetRedirectParameters(lastPageRequest.FiddlerSession.GetResponseBodyAsString());
                            ////if (parametersExtractor != null)
                            ////{
                            ////    lastPageRequest.ParametersToExtract.Add(parametersExtractor);
                            ////}
                        }
                    }
                    // the last request wasn't a redirect, it's a new request in the step
                    else
                    {
                        // Constructor creates PageRequest with default validations
                        var req = CreatePageRequest(httpReq, this, page, secondary, beanShell, gxApp);
                        Requests.Add(req);
                    }
                }
            }
            else
            {
                // If secondary request has referer header
                if (!String.IsNullOrEmpty(referer))
                {
                    var refererRequest = GetRequestByUrl(referer);

                    if (refererRequest != null)
                    {
                        refererRequest.AddSecondaryRequest(httpReq);
                    }
                    else
                    {
                        if (Requests.Count > 0)
                        {
                            Requests.Last().AddSecondaryRequest(httpReq);
                        }
                        else
                        {
                            // Add it as a PrimaryRequest
                            // may be an error on GetRequestByUrl() ?
                            var req = CreatePageRequest(httpReq, this, page, secondary, beanShell, gxApp);
                            Requests.Add(req);
                        }
                    }
                }
                else
                {
                    Requests.Last().AddSecondaryRequest(httpReq);
                }
            }
        }