예제 #1
0
        public static string Flush(string outputPath = null)
        {
            string fileName = string.Empty;

            if (Mode == HttpRecorderMode.Record && records.Count > 0)
            {
                RecordEntryPack pack = new RecordEntryPack();
                string          perfImpactFileName = string.Empty;
                string          fileDirectory      = outputPath ?? RecordsDirectory;
                fileDirectory = Path.Combine(fileDirectory, CallerIdentity);
                RecorderUtilities.EnsureDirectoryExists(fileDirectory);

                lock (records)
                {
                    foreach (RecordEntry recordEntry in records.GetAllEntities())
                    {
                        recordEntry.RequestHeaders.Remove("Authorization");
                        recordEntry.RequestUri = new Uri(recordEntry.RequestUri).PathAndQuery;
                        pack.Entries.Add(recordEntry);
                    }
                }

                fileName       = (TestIdentity ?? "record") + ".json";
                fileName       = Path.Combine(fileDirectory, fileName);
                pack.Variables = Variables;
                pack.Names     = names.Names;

                pack.Serialize(fileName);
            }

            servers.ForEach(s => s.Dispose());

            return(fileName);
        }
예제 #2
0
        public HttpResponseMessage GetResponse()
        {
            HttpResponseMessage response = new HttpResponseMessage();

            response.StatusCode = StatusCode;
            ResponseHeaders.ForEach(h => response.Headers.TryAddWithoutValidation(h.Key, h.Value));
            ResponseContentType = RecorderUtilities.GetContetTypeFromHeaders(ResponseHeaders);
            response.Content    = RecorderUtilities.CreateHttpContent(ResponseBody, ResponseContentType);
            ResponseHeaders.ForEach(h => response.Content.Headers.TryAddWithoutValidation(h.Key, h.Value));
            return(response);
        }
예제 #3
0
        public string GetMatchingKey(HttpRequestMessage request)
        {
            var requestHeaders = new Dictionary <string, List <string> >();

            request.Headers.ForEach(h => requestHeaders.Add(h.Key, h.Value.ToList()));
            if (request.Content != null)
            {
                request.Content.Headers.ForEach(h => requestHeaders.Add(h.Key, h.Value.ToList()));
            }
            return(GetMatchingKey(request.Method.Method, RecorderUtilities.EncodeUriAsBase64(request.RequestUri), requestHeaders));
        }
예제 #4
0
        private RecordEntryContentType DetectContentType(HttpContent content)
        {
            RecordEntryContentType contentType = RecordEntryContentType.Null;

            if (content != null)
            {
                if (RecorderUtilities.IsHttpContentBinary(content))
                {
                    contentType = RecordEntryContentType.Binary;
                }
                else
                {
                    contentType = RecordEntryContentType.Ascii;
                }
            }

            return(contentType);
        }
예제 #5
0
 public Queue <RecordEntry> this[string key]
 {
     get
     {
         if (sessionRecords.ContainsKey(key))
         {
             return(sessionRecords[key]);
         }
         else
         {
             throw new KeyNotFoundException(
                       string.Format("Unable to find a matching HTTP request for URL '{0}'. Calling method {1}().",
                                     RecorderUtilities.DecodeBase64AsUri(key),
                                     GetCallingMethodName()));
         }
     }
     set { sessionRecords[key] = value; }
 }
예제 #6
0
        protected override Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            if (Mode == HttpRecorderMode.Playback)
            {
                // Will throw KeyNotFoundException if the request is not recorded
                lock (records)
                {
                    var key = Matcher.GetMatchingKey(request);

                    var queue = records[key];
                    if (!queue.Any())
                    {
                        throw new InvalidOperationException(string.Format(
                                                                "Queue empty for request {0}",
                                                                RecorderUtilities.DecodeBase64AsUri(key)));
                    }

                    HttpResponseMessage result = queue.Dequeue().GetResponse();
                    result = AddTestModeHeaders(result);

                    result.RequestMessage = request;
                    return(Task.FromResult(result));
                }
            }
            else
            {
                lock (this)
                {
                    return(base.SendAsync(request, cancellationToken).ContinueWith <HttpResponseMessage>(response =>
                    {
                        HttpResponseMessage result = response.Result;
                        if (Mode == HttpRecorderMode.Record)
                        {
                            lock (records)
                            {
                                records.Enqueue(new RecordEntry(result));
                            }
                        }

                        return result;
                    }));
                }
            }
        }
예제 #7
0
        public RecordEntry(HttpResponseMessage response)
        {
            HttpRequestMessage request = response.RequestMessage;

            RequestUri        = request.RequestUri.ToString();
            EncodedRequestUri = RecorderUtilities.EncodeUriAsBase64(request.RequestUri);
            RequestMethod     = request.Method.Method;

            RequestHeaders  = new Dictionary <string, List <string> >();
            ResponseHeaders = new Dictionary <string, List <string> >();

            RequestBody  = string.Empty;
            ResponseBody = string.Empty;

            RequestContentType  = DetectContentType(request.Content);
            ResponseContentType = DetectContentType(response.Content);

            request.Headers.ForEach(h => RequestHeaders.Add(h.Key, h.Value.ToList()));
            response.Headers.ForEach(h => ResponseHeaders.Add(h.Key, h.Value.ToList()));

            StatusCode = response.StatusCode;

            //REQUEST
            if (RequestContentType != RecordEntryContentType.Null)
            {
                RequestBody = RecorderUtilities.FormatHttpContent(request.Content);
                request.Content.Headers.ForEach(h => RequestHeaders.Add(h.Key, h.Value.ToList()));
            }

            //RESPONSE
            if (ResponseContentType != RecordEntryContentType.Null)
            {
                ResponseBody = RecorderUtilities.FormatHttpContent(response.Content);
                response.Content.Headers.ForEach(h => ResponseHeaders.Add(h.Key, h.Value.ToList()));
            }
        }
예제 #8
0
 public static string EncodeUriAsBase64(Uri requestUri)
 {
     return(RecorderUtilities.EncodeUriAsBase64(requestUri.PathAndQuery));
 }
예제 #9
0
 public void Serialize(string path)
 {
     RecorderUtilities.SerializeJson(this, path);
 }
예제 #10
0
 public static RecordEntryPack Deserialize(string path)
 {
     return(RecorderUtilities.DeserializeJson <RecordEntryPack>(path));
 }
예제 #11
0
 public string GetMatchingKey(RecordEntry recordEntry)
 {
     return(GetMatchingKey(recordEntry.RequestMethod,
                           (recordEntry.EncodedRequestUri ?? RecorderUtilities.EncodeUriAsBase64(recordEntry.RequestUri)),
                           recordEntry.RequestHeaders));
 }