public async Task SendAsync_WithCookieContainerWithCookieInResponse_CookiesAddedToContainer() { var cassette = new Cassette(); var record = new CassetteRecord( new CassetteRecordRequest( HttpMethod.Get.Method, new Uri("http://*****:*****@"{""a"": 1, ""b"": 2}")); cassette.Add(record); var cookieContainer = new CookieContainer(); var replayingHttpMessageHandler = new PublicReplayingHttpMessageHandler(cassette, cookieContainer); var request = new HttpRequestMessage { Method = HttpMethod.Get, Version = new Version(1, 1), RequestUri = new Uri("http://localhost:8080/test"), Content = null, }; await replayingHttpMessageHandler.SendAsync(request, CancellationToken.None); Assert.That(cookieContainer.GetCookies(request.RequestUri)["value"]?.Value, Is.EqualTo("123")); }
public void AddAndFind_MethodEqualityComparerWithAddedRecords_NoRecords() { var record0 = new CassetteRecord( new CassetteRecordRequest("GET", new Uri("http://localhost:8080/test"), new NameValueCollection()), new CassetteRecordResponse(new Version(1, 1), 204, "No Content", new NameValueCollection())); var record1 = new CassetteRecord( new CassetteRecordRequest("POST", new Uri("http://localhost:8080/test"), new NameValueCollection()), new CassetteRecordResponse(new Version(1, 1), 204, "No Content", new NameValueCollection())); var cassette = new Cassette(new MethodEqualityComparer()); cassette.Add(record0); cassette.Add(record1); Assert.That(cassette.Records, Has.Count.EqualTo(2)); Assert.That(cassette.Find(new CassetteRecordRequest("GET", new Uri("http://localhost:8080/test123"), new NameValueCollection())), Is.EqualTo(record0)); Assert.That(cassette.Find(new CassetteRecordRequest("POST", new Uri("http://localhost:8080/test123"), new NameValueCollection())), Is.EqualTo(record1)); }
public async Task SendAsync_PostRequest_Success(Type cassetteBodyType) { var cassette = new Cassette(); var record = new CassetteRecord( new CassetteRecordRequest( HttpMethod.Post.Method, new Uri("http://*****:*****@"{""a"": 1, ""b"": 2}")); cassette.Add(record); var replayingHttpMessageHandler = new PublicReplayingHttpMessageHandler(cassette); var contentFactory = new Dictionary <Type, Func <HttpContent> > { { typeof(StringCassetteBody), () => new StringContent("{}") }, { typeof(BytesCassetteBody), () => new StreamContent(new MemoryStream(Encoding.UTF8.GetBytes("{}"))) }, }; var content = contentFactory[cassetteBodyType](); var request = new HttpRequestMessage { Method = HttpMethod.Post, Headers = { { "Cookie", "value=1" }, }, Version = new Version(1, 1), RequestUri = new Uri("http://localhost:8080/test"), Content = content, }; var response = await replayingHttpMessageHandler.SendAsync(request, CancellationToken.None); Assert.That(response, Is.Not.Null); Assert.That(response.Version, Is.EqualTo(record.Response.Version)); Assert.That(response.StatusCode, Is.EqualTo((HttpStatusCode)record.Response.StatusCode)); Assert.That(response.ReasonPhrase, Is.EqualTo(record.Response.StatusMessage)); Assert.That(response.RequestMessage, Is.Not.Null); Assert.That(response.Headers.GetValues("Server"), Is.EqualTo(new[] { record.Response.Headers["Server"] })); }
public async Task SendAsync_GetRequest_Success() { var cassette = new Cassette(); var record = new CassetteRecord( new CassetteRecordRequest( HttpMethod.Get.Method, new Uri("http://*****:*****@"{""a"": 1, ""b"": 2}")); cassette.Add(record); var replayingHttpMessageHandler = new PublicReplayingHttpMessageHandler(cassette); var request = new HttpRequestMessage { Method = HttpMethod.Get, Headers = { { "Cookie", "value=1" }, }, Version = new Version(1, 1), RequestUri = new Uri("http://localhost:8080/test"), Content = null, }; var response = await replayingHttpMessageHandler.SendAsync(request, CancellationToken.None); Assert.That(response, Is.Not.Null); Assert.That(response.Version, Is.EqualTo(record.Response.Version)); Assert.That(response.StatusCode, Is.EqualTo((HttpStatusCode)record.Response.StatusCode)); Assert.That(response.ReasonPhrase, Is.EqualTo(record.Response.StatusMessage)); Assert.That(response.RequestMessage, Is.Not.Null); Assert.That(response.Headers.GetValues("Server"), Is.EqualTo(new[] { record.Response.Headers["Server"] })); }
protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { var cookieContainer = _innerHandlerCookieContainerGetter?.Invoke(); var recordRequest = await CassetteRecordRequest.CreateFromRequest(request, cookieContainer); // After sending request _cookieContainer can be extended and recordRequest will definitely change as well // cause Cookie header will be added, that's why for compare we should clone CookieContainer var cloneCookieContainer = cookieContainer?.Clone(request.RequestUri); var response = await base.SendAsync(request, cancellationToken); var recordResponse = await CassetteRecordResponse.CreateFromResponse(response, recordRequest, cloneCookieContainer); var record = new CassetteRecord(recordRequest, recordResponse); _cassette.Add(record); return(response); }
public async Task SendAsync_ExistsInCassette_DoesNotCallInnerHttpHandler() { var cassette = new Cassette(); var record = new CassetteRecord( new CassetteRecordRequest( HttpMethod.Get.Method, new Uri("http://*****:*****@"{""a"": 1, ""b"": 2}")); cassette.Add(record); var mockHttpHandler = new MockHttpRequestHandler(); var tryReplayMessageHandler = new PublicTryReplayHttpMessageHandler(cassette, mockHttpHandler); var request = new HttpRequestMessage { Method = HttpMethod.Get, RequestUri = new Uri("http://localhost:8080/test"), }; var response = await tryReplayMessageHandler.SendAsync(request); Assert.That(mockHttpHandler.Invoked, Is.False); Assert.That(response.Version, Is.EqualTo(record.Response.Version)); Assert.That(response.StatusCode, Is.EqualTo((HttpStatusCode)record.Response.StatusCode)); Assert.That(response.ReasonPhrase, Is.EqualTo(record.Response.StatusMessage)); Assert.That(response.RequestMessage, Is.Not.Null); Assert.That(response.Headers.GetValues("Server"), Is.EqualTo(new[] { record.Response.Headers["Server"] })); }
public void SendAsync_WrongResponseHeader_ThrowsArgumentException() { var cassette = new Cassette(); var record = new CassetteRecord( new CassetteRecordRequest( HttpMethod.Get.Method, new Uri("http://*****:*****@"{""a"": 1, ""b"": 2}")); cassette.Add(record); var replayingHttpMessageHandler = new PublicReplayingHttpMessageHandler(cassette); var request = new HttpRequestMessage { Method = HttpMethod.Get, Headers = { { "Cookie", "value=1" }, }, Version = new Version(1, 1), RequestUri = new Uri("http://localhost:8080/test"), Content = null, }; var argumentException = Assert.ThrowsAsync <ArgumentException>(() => replayingHttpMessageHandler.SendAsync(request, CancellationToken.None)); Assert.That(argumentException.ParamName, Is.Null); }