static void Main(string[] args) { ApiRecord a = new ApiRecord("0,1,2,3,4,5".Split(',')); Console.WriteLine(JsonConvert.SerializeObject(a)); Console.ReadLine(); }
private static void InternalSetValue(ApiRecord record, IntPtr address, uint value) { uint storage; uint max; if (!ApiProxy.IsEnabled) { return; } max = (uint)(Math.Pow(2, record.Size) - 1); if (value < record.Min || value > record.Max) { throw new ArgumentException(nameof(value)); } storage = ApiProxy.InternalGetValue(0, 32, address); storage &= ~(max << record.Shift); storage |= (value << record.Shift); unsafe { *(uint *)(address.ToPointer()) = storage; } }
public IApiProxyProvider GetApiProxyProvider(ApiRecord record, HttpRequestMessage request, IApiProxyConfiguration configuration) { switch (record.ResponseContentType) { case "text/xml": case "application/xml": var xmlApiRecord = JsonConvert.DeserializeObject <XmlApiRecord>(record.RawContent); return(new XmlContentTypeApiProxyProvider(configuration, _recorder, xmlApiRecord)); } if (!_apiProxyConfiguration.FallbackToDefaultApi) { throw new InvalidOperationException("Content type for mocking not supported"); } return(new RealApiApiProxyProvider(configuration, _recorder)); }
public bool AddApiRecord(ApiRecord record) { if (string.IsNullOrWhiteSpace(record.RawContent)) { var raw = JsonConvert.SerializeObject(record); record.RawContent = raw; } var requestUri = new Uri(record.Uri); if (_mockApiResponses.ContainsKey(GetApiRequestPathAndQuery(requestUri))) { return(false); } _mockApiResponses.Add(GetApiRequestPathAndQuery(requestUri), record); return(true); }
public virtual async Task RecordApi(HttpRequestMessage request, HttpResponseMessage response) { try { var recordApi = _proxyConfiguration.RecordApiRequestResponse; if (!recordApi) { return; } var apiRecordingPath = _proxyConfiguration.ApiRecordingPath; var effectiveMockResponsesPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, apiRecordingPath); var recordedResponse = new ApiRecord { Method = request.Method.ToString(), RequestContent = request.Content != null ? await request.Content.ReadAsStringAsync() : string.Empty, StatusCode = HttpStatusCode.OK.ToString(), Uri = request.RequestUri.ToString(), ResponseContentType = response.Content.Headers.ContentType.MediaType, Mock = true, ResponseContent = response.Content != null ? await response.Content.ReadAsStringAsync() : string.Empty }; var urlParts = request.RequestUri.PathAndQuery.TrimEnd(new char[] { '?', '&' }) .Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries) .ToList(); var fileName = "_"; // last one contains the queryparameters if (urlParts[urlParts.Count - 1].Contains("?")) { var lastPart = urlParts[urlParts.Count - 1]; urlParts.RemoveAt(urlParts.Count - 1); var queryParts = lastPart.Split(new char[] { '?' }); urlParts.AddRange(new string[] { queryParts[0], "_q" }); // file name will contain all the query parameters fileName = queryParts[1]; } else { // always use _q to make sure filename and folder name doesnt clash for different paths urlParts.Add("_q"); } var folderParts = new List <string> { effectiveMockResponsesPath, request.Method.ToString() }; folderParts.AddRange(urlParts); effectiveMockResponsesPath = Path.Combine(folderParts.ToArray()); Directory.CreateDirectory(effectiveMockResponsesPath); string effectiveFilePath = Path.Combine(effectiveMockResponsesPath, fileName); File.WriteAllText(effectiveFilePath, JsonConvert.SerializeObject(recordedResponse)); } catch (Exception ex) { ApiProxyEvents.Raise.VerboseMessaging(ex.ToString() + ex.StackTrace); } }
public void AddMockApiRecord(ApiRecord record) { _inMemoryApiProxyRecordProvider.AddApiRecord(record); }