コード例 #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));
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        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));
        }
コード例 #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);
 }
コード例 #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));
        }
コード例 #6
0
 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);
 }
コード例 #7
0
 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);
 }
コード例 #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);
        }
コード例 #9
0
 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);
     }
 }
コード例 #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));
        }
コード例 #11
0
 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);
     }
 }
コード例 #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);
            }
        }
コード例 #13
0
        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());
        }
コード例 #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));
        }
コード例 #15
0
 public static IEnumerable <dynamic> ToJsonCollection(this ITestableHttpResponse response, JsonSerializerSettings settings = null) => response.ToJson <ExpandoObject[]>(settings);
コード例 #16
0
 private TestableHttpResponseException(Tuple <string, FileInfo> formattedMessage, ITestableHttpResponse actualResponse, Exception inner)
     : base(formattedMessage.Item1, inner)
 {
     ActualResponse  = actualResponse;
     ResponseLogFile = formattedMessage.Item2;
 }
コード例 #17
0
 public TestableHttpResponseException(string message, ITestableHttpResponse actualResponse, Exception inner = null)
     : this(FormatMessage(message, actualResponse), actualResponse, inner)
 {
 }
コード例 #18
0
 public static bool IsValidResponse(this ITestableHttpResponse response)
 {
     return(response != null && !(response is NoTestableHttpResponse));
 }
コード例 #19
0
 public static T ToAnonymousJson <T>(this ITestableHttpResponse response, T expectedModel, JsonSerializerSettings settings = null) => JsonConvert.DeserializeAnonymousType(response.ToStringContent(), expectedModel, settings);
コード例 #20
0
 public void Dispose()
 {
     LastResponse = null;
     _client.Dispose();
 }
コード例 #21
0
 public static dynamic ToJson(this ITestableHttpResponse response, JsonSerializerSettings settings = null) => JsonConvert.DeserializeObject <ExpandoObject>(response.ToStringContent(), settings);
コード例 #22
0
 public static string ToStringContent(this ITestableHttpResponse response) => response.Content.ContentEncoding.GetString(response.Content.Content);
コード例 #23
0
 public static FileInfo LogResponseOnDisk(this ITestableHttpResponse response)
 {
     return(TestableHttpResponseLogger.LogResponse(response));
 }