Ejemplo n.º 1
0
        /// <summary>
        /// Executes WOPI request at given WOPI endpoint address against provided wopi FileRep.
        /// </summary>
        public override IResponseData Execute(string endpointAddress,
                                              string accessToken,
                                              long accessTokenTtl,
                                              ITestCase testCase,
                                              Dictionary <string, string> savedState,
                                              IResourceManager resourceManager,
                                              string userAgent,
                                              RSACryptoServiceProvider proofKeyProviderNew,
                                              RSACryptoServiceProvider proofKeyProviderOld)
        {
            // Get the url of the WOPI endpoint that we'll call - either the normal endpoint, or a SavedState override.
            // If it's an override it might change the accessToken that we're using because it probably already has a token on it.
            Uri uri = GetRequestUri(endpointAddress, ref accessToken, accessTokenTtl, savedState);

            // Run any access token mutators defined on this request.
            string accessTokenToUse = GetMutatedAccessToken(accessToken);

            if (accessToken != accessTokenToUse)
            {
                // The access token changed so update our uri with the new one
                uri = new Uri(UrlHelper.AppendOrReplaceQueryParameter(uri.AbsoluteUri, "access_token", accessTokenToUse));
            }
            // At this point we have the final uri and accessTokenToUse values.  We'll use them later in proof key signing

            List <KeyValuePair <string, string> >        headers       = DefaultHeaders.ToList();
            IEnumerable <KeyValuePair <string, string> > customHeaders = GetCustomHeaders(savedState, resourceManager);

            if (customHeaders != null)
            {
                headers.AddRange(customHeaders);
            }

            if (!string.IsNullOrEmpty(WopiOverrideValue))
            {
                headers.Add(new KeyValuePair <string, string>(Constants.Headers.Override, WopiOverrideValue));
            }

            if (proofKeyProviderNew != null && proofKeyProviderOld != null)
            {
                Dictionary <string, string> originalProofKeyHeaders =
                    GetProofKeyHeaders(accessTokenToUse, uri, proofKeyProviderNew, proofKeyProviderOld);
                Dictionary <string, string> proofKeyHeadersToUse =
                    GetMutatedProofKeyHeaders(originalProofKeyHeaders, timestamp => GetProofKeyHeaders(accessTokenToUse, uri, proofKeyProviderNew, proofKeyProviderOld, timestamp));
                headers.AddRange(proofKeyHeadersToUse);
            }

            headers.Add(new KeyValuePair <string, string>(Constants.Headers.Authorization, "Bearer " + accessTokenToUse));

            MemoryStream         contentStream = HasRequestContent ? GetRequestContent(resourceManager) : null;
            RequestExecutionData executionData = new RequestExecutionData(uri, headers, contentStream);

            return(ExecuteRequest(executionData, userAgent));
        }
Ejemplo n.º 2
0
        public override IResponseData Execute(
            string endpointAddress,
            string accessToken,
            long accessTokenTtl,
            ITestCase testCase,
            Dictionary <string, string> savedState,
            IResourceManager resourceManager,
            string userAgent,
            RSACryptoServiceProvider proofKeyProviderNew,
            RSACryptoServiceProvider proofKeyProviderOld)
        {
            RequestExecutionData executionData = new RequestExecutionData(
                new Uri(GetEndpointAddressOverride(savedState)),
                Enumerable.Empty <KeyValuePair <string, string> >(),
                null);

            return(ExecuteRequest(executionData));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Executes request and gathers response data.
        /// </summary>
        /// <param name="targetUri">URI request should be made against</param>
        /// <param name="headers">Set of custom headers that should be added to the request</param>
        /// <param name="content">Request content stream</param>
        /// <returns>IResponseData instance with information takes from response.</returns>
        protected IResponseData ExecuteRequest(
            RequestExecutionData executionData,
            string userAgent = null
            )
        {
            TargetUrl      = executionData.TargetUri.AbsoluteUri;
            RequestHeaders = executionData.Headers.ToArray();

            HttpWebRequest request = WebRequest.CreateHttp(executionData.TargetUri);

            request.UserAgent = userAgent;

            // apply custom headers
            foreach (KeyValuePair <string, string> header in RequestHeaders)
            {
                request.Headers.Add(header.Key, header.Value);
            }

            request.Method = RequestMethod;

            MemoryStream content = executionData.ContentStream;

            // set proper ContentLength and content stream
            if (content != null)
            {
                request.ContentLength = content.Length;
                using (Stream requestStream = request.GetRequestStream())
                {
                    content.Seek(0, SeekOrigin.Begin);
                    content.CopyTo(requestStream);
                }
            }
            else
            {
                request.ContentLength = 0;
            }
            Stopwatch timer = new Stopwatch();

            try
            {
                timer.Start();
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    timer.Stop();
                    return(GetResponseData(response, IsTextResponseExpected, timer.Elapsed));
                }
            }
            // ProtocolErrors will have a non-null Response object so we can still get response details
            catch (WebException ex) when(ex.Status == WebExceptionStatus.ProtocolError)
            {
                using (HttpWebResponse response = (HttpWebResponse)ex.Response)
                {
                    timer.Stop();
                    return(GetResponseData(response, IsTextResponseExpected, timer.Elapsed));
                }
            }
            // no response, so we wrap the exception details so they can be included in a validation failure
            catch (WebException ex)
            {
                return(ExceptionHelper.WrapExceptionInResponseData(ex));
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Executes request and gathers response data.
        /// </summary>
        /// <param name="targetUri">URI request should be made against</param>
        /// <param name="headers">Set of custom headers that should be added to the request</param>
        /// <param name="content">Request content stream</param>
        /// <param name="hasToBeSuccessful">Whether Response Code has to be 200 or other response codes are ok.</param>
        /// <returns>IResponseData instance with information takes from response.</returns>
        protected IResponseData ExecuteRequest(
            RequestExecutionData executionData,
            string userAgent       = null,
            bool hasToBeSuccessful = false
            )
        {
            HasToBeSuccessful = hasToBeSuccessful;
            TargetUrl         = executionData.TargetUri.AbsoluteUri;
            RequestHeaders    = executionData.Headers.ToArray();

            HttpWebRequest request = WebRequest.CreateHttp(executionData.TargetUri);

            request.UserAgent = userAgent;

            // apply custom headers
            foreach (KeyValuePair <string, string> header in RequestHeaders)
            {
                request.Headers.Add(header.Key, header.Value);
            }

            request.Method = RequestMethod;

            MemoryStream content = executionData.ContentStream;

            // set proper ContentLength and content stream
            if (content != null)
            {
                request.ContentLength = content.Length;
                using (Stream requestStream = request.GetRequestStream())
                {
                    content.Seek(0, SeekOrigin.Begin);
                    content.CopyTo(requestStream);
                }
            }
            else
            {
                request.ContentLength = 0;
            }
            Stopwatch timer = new Stopwatch();

            try
            {
                timer.Start();
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    timer.Stop();
                    return(GetResponseData(response, IsTextResponseExpected, timer.Elapsed));
                }
            }
            catch (WebException ex)
            {
                // if hasToBeSuccessful we don't want the exception to be caught here.
                if (hasToBeSuccessful)
                {
                    throw;
                }

                // get response information anyway otherwise.
                using (HttpWebResponse response = (HttpWebResponse)ex.Response)
                {
                    timer.Stop();
                    return(GetResponseData(response, IsTextResponseExpected, timer.Elapsed));
                }
            }
        }