#pragma warning disable CS1998 // No async operations in mock /// <summary> /// Implements the same method as the real service interface allowing the mock to be plugged in /// </summary> /// <typeparam name="Response"></typeparam> /// <typeparam name="Request"></typeparam> /// <param name="resource"></param> /// <param name="verb"></param> /// <param name="request"></param> /// <param name="deleteBody"></param> /// <param name="session"></param> /// <returns></returns> public async Task <ShippingApiResponse <Response> > HttpRequest <Response, Request>(string resource, HttpVerb verb, Request request, bool deleteBody, ISession session = null) where Request : IShippingApiRequest { string fullPath = request.RecordingFullPath(resource, session); if (File.Exists(fullPath)) { var apiResponse = new ShippingApiResponse <Response> { HttpStatus = HttpStatusCode.OK, Success = true }; long jsonPosition = 0; using (var fileStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read)) using (var mimeStream = new MimeStream(fileStream)) { mimeStream.SeekNextPart(); //request mimeStream.SeekNextPart(); //response mimeStream.ClearHeaders(); mimeStream.ReadHeaders(); // reads http headers as well if (!mimeStream.FirstLine.StartsWith("HTTP", StringComparison.InvariantCulture)) { apiResponse = new ShippingApiResponse <Response> { HttpStatus = HttpStatusCode.InternalServerError, Success = false }; session.LogDebug(string.Format("Mock request failed {0}", fullPath)); apiResponse.Errors.Add(new ErrorDetail() { ErrorCode = "Mock 500", Message = "Bad format " + fullPath }); return(apiResponse); } var hrc = mimeStream.FirstLine.Split(' '); apiResponse.HttpStatus = (HttpStatusCode)int.Parse(hrc[1]); apiResponse.Success = apiResponse.HttpStatus == HttpStatusCode.OK; foreach (var h in mimeStream.Headers) { apiResponse.ProcessResponseAttribute(h.Key, h.Value); } using (var recordingStream = new RecordingStream(mimeStream, request.RecordingFullPath(resource, session), FileMode.Create, RecordingStream.RecordType.PlainText)) { try { //dont open the record file recordingStream.IsRecording = false; ShippingApiResponse <Response> .Deserialize(session, recordingStream, apiResponse, jsonPosition); } catch (Exception ex) { session.LogError(string.Format("Mock request {0} got deserialization exception {1}", fullPath, ex.Message)); throw ex; } } } return(apiResponse); } else { var apiResponse = new ShippingApiResponse <Response> { HttpStatus = HttpStatusCode.NotFound, Success = false }; session.LogDebug(string.Format("Mock request failed {0}", fullPath)); apiResponse.Errors.Add(new ErrorDetail() { ErrorCode = "Mock 401", Message = "Could not find response file" + fullPath }); return(apiResponse); } }