Esempio n. 1
0
        public static async Task <CassetteRecordResponse> CreateFromResponse(HttpResponseMessage response, CassetteRecordRequest request, CookieContainer?cookieContainer)
        {
            var record = new CassetteRecordResponse(response.Version, (int)response.StatusCode, response.ReasonPhrase, response.ToNameValueCollection())
            {
                Request = await CassetteRecordRequest.CreateFromRequest(response.RequestMessage, cookieContainer)
            };

            // In case of Redirect inner innerHandler can create a new request, that actually did redirect
            // External users can expect this RequestMessage for check redirecting URI for example
            // innerHandler can change request, so we should create a new CassetteRecordRequest
            // from response and compare it with the original value
            if (record.Request == request)
            {
                record.Request = null;
            }

            var(body, newContent) = await CassetteBody.CreateCassetteBody(response.Content);

            record.Body = body;
            if (newContent != null)
            {
                response.Content = newContent;
            }

            return(record);
        }
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var newRecord = await CassetteRecordRequest.CreateFromRequest(request, _cookieContainer);

            var record = _cassette.Find(newRecord, _comparer);

            if (record == null)
            {
                throw new ArgumentException("Can't find request in cassette", nameof(request));
            }

            var recordResponse = record.Response;
            var response       = recordResponse.ToResponseMessage();

            // If there are no changes in request stored in cassette, then use original request
            response.RequestMessage ??= request;

            if (_cookieContainer != null)
            {
                CookieHelper.ProcessReceivedCookies(response, _cookieContainer);
            }

            // Simulate async processing
            await Task.Yield();

            return(response);
        }
Esempio n. 3
0
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var cookieContainer = _innerHandlerCookieContainerGetter?.Invoke();
            var recordRequest   = await CassetteRecordRequest.CreateFromRequest(request, cookieContainer);

            // After sending request _cookieContainer can be extended and recordRequest will definitely change as well
            // cause Cookie header will be added, that's why for compare we should clone CookieContainer
            var cloneCookieContainer = cookieContainer?.Clone(request.RequestUri);

            var response = await base.SendAsync(request, cancellationToken);

            var recordResponse = await CassetteRecordResponse.CreateFromResponse(response, recordRequest, cloneCookieContainer);

            var record = new CassetteRecord(recordRequest, recordResponse);

            _cassette.Add(record);

            return(response);
        }
Esempio n. 4
0
 public CassetteRecord(CassetteRecordRequest request, CassetteRecordResponse response)
 {
     Request  = request;
     Response = response;
 }