/// <summary> /// Saves the specified HTTP <paramref name="response" /> to the message store as an asynchronous operation. /// </summary> /// <param name="request">The HTTP request message that was sent.</param> /// <param name="response">The HTTP response messsage to save.</param> /// <returns> /// The task object representing the asynchronous operation. /// </returns> public override async Task SaveAsync(HttpRequestMessage request, HttpResponseMessage response) { // don't save content if not success if (!response.IsSuccessStatusCode || response.Content == null || response.StatusCode == HttpStatusCode.NoContent) { return; } var httpContent = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false); var fakeResponse = Convert(response); var key = GenerateKey(request); var container = new FakeResponseContainer { HttpContent = httpContent, ResponseMessage = fakeResponse }; // save to store #if PORTABLE lock (_storeLock) _responseStore[key] = container; #else _responseStore.AddOrUpdate(key, container, (k, o) => container); #endif }
/// <summary> /// Register a fake response using the specified fluent <paramref name="builder"/> action. /// </summary> /// <param name="builder">The fluent container builder.</param> /// <exception cref="ArgumentNullException"></exception> public void Register(Action <FakeContainerBuilder> builder) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } var container = new FakeResponseContainer(); var containerBuilder = new FakeContainerBuilder(container); builder(containerBuilder); // save to store var key = container.RequestUri.ToString(); _responseStore.AddOrUpdate(key, container, (k, o) => container); }
/// <summary> /// Initializes a new instance of the <see cref="FakeContentBuilder"/> class. /// </summary> /// <param name="container">The container to build.</param> public FakeContentBuilder(FakeResponseContainer container) : base(container) { }
/// <summary> /// Initializes a new instance of the <see cref="FakeContainerBuilder{TBuilder}"/> class. /// </summary> /// <param name="container">The container to build.</param> protected FakeContainerBuilder(FakeResponseContainer container) { Container = container; }