Exemple #1
0
        public static FileInfo LogResponse(ITestableHttpResponse response)
        {
            var path = Path.Combine(DefaultHttpResponseLogPath, $"response-{DateTime.UtcNow:yyyyMMdd-HHmmss}-{Interlocked.Increment(ref _counter):D5}.txt");

            File.WriteAllText(path, response.DumpToString());
            return(new FileInfo(path));
        }
        public static ITestableHttpResponse PrintResponseInComments(this ITestableHttpResponse response)
        {
            var comment = $"Response for: {response.Request.Method} {response.Request.Uri}:\n{response.DumpToString()}";

            StepExecution.Current.Comment(comment);
            return(response);
        }
        public async Task <ITestableHttpResponse> SendAsync(HttpRequestMessage request)
        {
            var testableHttpRequest = new MockHttpRequest(request, request.Content != null ? await request.Content.ReadAsByteArrayAsync() : null, _client.BaseAddress);
            var response            = await _client.SendAsync(request, HttpCompletionOption.ResponseContentRead);

            return(LastResponse = new TestableHttpResponse(response, await response.Content.ReadAsByteArrayAsync(), testableHttpRequest));
        }
Exemple #4
0
 public RecordedHttpResponse(ITestableHttpResponse model)
 {
     Content      = new RecordedHttpContent(model.Content);
     Headers      = model.Headers.ToDictionary(kv => kv.Key, kv => kv.Value);
     ReasonPhrase = model.ReasonPhrase;
     StatusCode   = model.StatusCode;
     Request      = new RecordedHttpRequest(model.Request);
 }
Exemple #5
0
        public void Add(ITestableHttpResponse response)
        {
            _items.Push(response);

            var path = Path.Combine(_storageDirectory, $"{DateTime.UtcNow:yyyyMMdd-HHmmss}-{Interlocked.Increment(ref _counter):D5}.{Ext}");

            File.WriteAllText(path, JsonConvert.SerializeObject(new RecordedHttpResponse(response), Formatting.Indented));
        }
 public static ITestableHttpResponse EnsureSuccessStatusCode(this ITestableHttpResponse response)
 {
     if ((int)response.StatusCode < 200 || (int)response.StatusCode >= 300)
     {
         throw new TestableHttpResponseException($"Expected response with successful status code, but got '{response.StatusCode}'", response);
     }
     return(response);
 }
 public static ITestableHttpResponse EnsureStatusCode(this ITestableHttpResponse response, HttpStatusCode code)
 {
     if (response.StatusCode != code)
     {
         throw new TestableHttpResponseException($"Expected '{code}' response status code, but got '{response.StatusCode}'.", response);
     }
     return(response);
 }
Exemple #8
0
        private static string CreateTrimmedDump(ITestableHttpResponse actualResponse, int maxLength)
        {
            var dump = actualResponse.DumpToString();

            if (dump.Length > maxLength)
            {
                dump = dump.Substring(0, maxLength) + "...";
            }
            return(dump);
        }
 public static T ProcessInResponseContext <T>(this ITestableHttpResponse response, Func <ITestableHttpResponse, T> action)
 {
     try
     {
         return(action(response));
     }
     catch (Exception e) when(!(e is TestableHttpResponseException))
     {
         throw new TestableHttpResponseException(e.Message, response, e);
     }
 }
Exemple #10
0
        private static Tuple <string, FileInfo> FormatMessage(string message, ITestableHttpResponse actualResponse)
        {
            if (!actualResponse.IsValidResponse())
            {
                return(Tuple.Create($"{message}\n\nActual response object is invalid (no response received so far)", (FileInfo)null));
            }

            var request = actualResponse.Request;
            var logFile = actualResponse.LogResponseOnDisk();

            return(Tuple.Create($"{message}\n\nActual response for {request.Method} {request.Uri}:\n{CreateTrimmedDump(actualResponse, 2048)}\nFull response log: {logFile.FullName}", logFile));
        }
 public static ITestableHttpResponse ProcessInResponseContext(this ITestableHttpResponse response, Action <ITestableHttpResponse> action)
 {
     try
     {
         action(response);
         return(response);
     }
     catch (Exception e) when(!(e is TestableHttpResponseException))
     {
         throw new TestableHttpResponseException(e.Message, response, e);
     }
 }
Exemple #12
0
        private void Replay(IMockHttpResponse rsp, ITestableHttpResponse response)
        {
            rsp.SetStatusCode(response.StatusCode)
            .SetHeaders(response.Headers.Where(kv => !InvalidHeaders.Contains(kv.Key)));

            if (response.Content.ContentLength > 0)
            {
                rsp.SetContent(
                    response.Content.Content,
                    response.Content.ContentEncoding,
                    response.Content.ContentType);
            }
        }
        public static string DumpToString(this ITestableHttpResponse response)
        {
            var builder = new StringBuilder();

            builder.AppendLine($"StatusCode: HTTP {(int)response.StatusCode} {response.StatusCode}");
            builder.AppendLine("Headers:");
            foreach (var header in response.Headers)
            {
                builder.AppendLine($"- {header.Key}: {header.Value}");
            }
            builder.AppendLine("Content:");
            builder.AppendLine(response.ToStringContent());
            return(builder.ToString());
        }
Exemple #14
0
        private ITestableHttpResponse ApplyReplacements(ITestableHttpResponse response)
        {
            var content = response.ToStringContent();
            var headers = response.Headers.ToDictionary(kv => kv.Key, kv => kv.Value);

            foreach (var replacement in _replacements)
            {
                ReplaceIf(replacement, ref content, ReplacementOptions.Content);
                ReplaceHeaders(replacement, headers);
            }

            var httpContent = new MockHttpContent(response.Content.ContentEncoding.GetBytes(content), response.Content.ContentEncoding, response.Content.ContentType, response.Content.Headers);

            return(new TestableHttpResponse(response.Request, response.StatusCode, response.ReasonPhrase, httpContent, headers));
        }
 public static IEnumerable <dynamic> ToJsonCollection(this ITestableHttpResponse response, JsonSerializerSettings settings = null) => response.ToJson <ExpandoObject[]>(settings);
Exemple #16
0
 private TestableHttpResponseException(Tuple <string, FileInfo> formattedMessage, ITestableHttpResponse actualResponse, Exception inner)
     : base(formattedMessage.Item1, inner)
 {
     ActualResponse  = actualResponse;
     ResponseLogFile = formattedMessage.Item2;
 }
Exemple #17
0
 public TestableHttpResponseException(string message, ITestableHttpResponse actualResponse, Exception inner = null)
     : this(FormatMessage(message, actualResponse), actualResponse, inner)
 {
 }
 public static bool IsValidResponse(this ITestableHttpResponse response)
 {
     return(response != null && !(response is NoTestableHttpResponse));
 }
 public static T ToAnonymousJson <T>(this ITestableHttpResponse response, T expectedModel, JsonSerializerSettings settings = null) => JsonConvert.DeserializeAnonymousType(response.ToStringContent(), expectedModel, settings);
 public void Dispose()
 {
     LastResponse = null;
     _client.Dispose();
 }
 public static dynamic ToJson(this ITestableHttpResponse response, JsonSerializerSettings settings = null) => JsonConvert.DeserializeObject <ExpandoObject>(response.ToStringContent(), settings);
 public static string ToStringContent(this ITestableHttpResponse response) => response.Content.ContentEncoding.GetString(response.Content.Content);
 public static FileInfo LogResponseOnDisk(this ITestableHttpResponse response)
 {
     return(TestableHttpResponseLogger.LogResponse(response));
 }