private byte[] GetNormalBody(ResponseMessage responseMessage) { byte[] bytes = null; switch (responseMessage.BodyData?.DetectedBodyType) { case BodyType.String: bytes = (responseMessage.BodyData.Encoding ?? _utf8NoBom).GetBytes(responseMessage.BodyData.BodyAsString); break; case BodyType.Json: Formatting formatting = responseMessage.BodyData.BodyAsJsonIndented == true ? Formatting.Indented : Formatting.None; string jsonBody = JsonConvert.SerializeObject(responseMessage.BodyData.BodyAsJson, new JsonSerializerSettings { Formatting = formatting, NullValueHandling = NullValueHandling.Ignore }); bytes = (responseMessage.BodyData.Encoding ?? _utf8NoBom).GetBytes(jsonBody); break; case BodyType.Bytes: bytes = responseMessage.BodyData.BodyAsBytes; break; case BodyType.File: bytes = _fileSystemHandler.ReadResponseBodyAsFile(responseMessage.BodyData.BodyAsFile); break; } return(bytes); }
public async Task <ResponseMessage> ProvideResponseAsync(RequestMessage requestMessage, IFileSystemHandler fileSystemHandler) { Check.NotNull(requestMessage, nameof(requestMessage)); if (Delay != null) { await Task.Delay(Delay.Value); } if (Callback != null) { var callbackResponseMessage = Callback(requestMessage); if (!WithCallbackUsed) { // Copy StatusCode from ResponseMessage callbackResponseMessage.StatusCode = ResponseMessage.StatusCode; // Copy Headers from ResponseMessage (if defined) if (ResponseMessage.Headers != null) { callbackResponseMessage.Headers = ResponseMessage.Headers; } } return(callbackResponseMessage); } if (ProxyUrl != null && _httpClientForProxy != null) { var requestUri = new Uri(requestMessage.Url); var proxyUri = new Uri(ProxyUrl); var proxyUriWithRequestPathAndQuery = new Uri(proxyUri, requestUri.PathAndQuery); return(await HttpClientHelper.SendAsync(_httpClientForProxy, requestMessage, proxyUriWithRequestPathAndQuery.AbsoluteUri)); } if (UseTransformer) { var responseMessageTransformer = new ResponseMessageTransformer(fileSystemHandler); return(responseMessageTransformer.Transform(requestMessage, ResponseMessage)); } if (!UseTransformer && ResponseMessage.BodyData?.BodyAsFileIsCached == true) { ResponseMessage.BodyData.BodyAsBytes = fileSystemHandler.ReadResponseBodyAsFile(ResponseMessage.BodyData.BodyAsFile); ResponseMessage.BodyData.BodyAsFile = null; } return(ResponseMessage); }
/// <inheritdoc cref="IOwinResponseMapper.MapAsync"/> public async Task MapAsync(ResponseMessage responseMessage, IResponse response) { if (responseMessage == null) { return; } response.StatusCode = responseMessage.StatusCode; byte[] bytes = null; switch (responseMessage.BodyData?.DetectedBodyType) { case BodyType.String: bytes = (responseMessage.BodyData.Encoding ?? _utf8NoBom).GetBytes(responseMessage.BodyData.BodyAsString); break; case BodyType.Json: Formatting formatting = responseMessage.BodyData.BodyAsJsonIndented == true ? Formatting.Indented : Formatting.None; string jsonBody = JsonConvert.SerializeObject(responseMessage.BodyData.BodyAsJson, new JsonSerializerSettings { Formatting = formatting, NullValueHandling = NullValueHandling.Ignore }); bytes = (responseMessage.BodyData.Encoding ?? _utf8NoBom).GetBytes(jsonBody); break; case BodyType.Bytes: bytes = responseMessage.BodyData.BodyAsBytes; break; case BodyType.File: bytes = _fileSystemHandler.ReadResponseBodyAsFile(responseMessage.BodyData.BodyAsFile); break; } SetResponseHeaders(responseMessage, response); if (bytes != null) { await response.Body.WriteAsync(bytes, 0, bytes.Length); } }
/// <inheritdoc cref="IBodyResponseBuilder.WithBodyFromFile"/> public IResponseBuilder WithBodyFromFile(string filename, bool cache = true) { Check.NotNull(filename, nameof(filename)); ResponseMessage.BodyData = new BodyData { BodyAsFileIsCached = cache }; if (cache) { ResponseMessage.BodyData.DetectedBodyType = BodyType.Bytes; ResponseMessage.BodyData.BodyAsBytes = _fileSystemHandler.ReadResponseBodyAsFile(filename); } else { ResponseMessage.BodyData.DetectedBodyType = BodyType.File; ResponseMessage.BodyData.BodyAsFile = filename; } return(this); }