public void Find_RecordNotExists_ReturnsNull() { var cassette = new Cassette(Context.Cassette1); var record = cassette.Find("wrong"); Assert.IsNull(record); }
public void Recording_PostEmptyContent_ContentLengthHeaderSaved() { var cassette = new Cassette(Context.CreateCassette("new")); player.Start(); player.Load(cassette); player.Record("record1"); using (var server = new Server(remoteAddress1)) { Task.Run(() => server.Accept()); var client = new Client(baseAddress); client.Send("/", "POST", headers: new NameValueCollection { { "Content-Length", "0" } }); } player.Stop(); var record = cassette.Find("record1"); var mock = (JObject)record.Read(); var jrequest = (JObject)mock["request"]; Assert.IsNull(jrequest["content"]); Assert.AreEqual("0", jrequest["headers"]["Content-Length"].ToString()); }
protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { var newRecord = await CassetteRecordRequest.CreateFromRequest(request, _cookieContainer); var record = _cassette.Find(newRecord, _comparer); if (record == null) { throw new ArgumentException("Can't find request in cassette", nameof(request)); } var recordResponse = record.Response; var response = recordResponse.ToResponseMessage(); // If there are no changes in request stored in cassette, then use original request response.RequestMessage ??= request; if (_cookieContainer != null) { CookieHelper.ProcessReceivedCookies(response, _cookieContainer); } // Simulate async processing await Task.Yield(); return(response); }
public void Find_RecordExists_ReturnsRecord() { var cassette = new Cassette(Context.Cassette1); var record = cassette.Find("record1"); Assert.IsNotNull(record); Assert.AreEqual("record1", record.Name); Assert.AreEqual(3, record.List.Count); }
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 void Stop_AfterRecorded_SavesRecord() { var cassette = new Cassette(Context.CassetteNew); player.Start(); player.Load(cassette); player.Record("record1"); using (var server = new Server(remoteAddress1)) { Task.Run(() => server.Accept()); var client = new Client(baseAddress); var headers = new NameValueCollection { { "Accept", "text/plain" }, { "Connection", "Keep-Alive" }, { "Content-Type", "text/plain; charset=ascii" }, { "Date", "Wed, 01 Jun 2016 08:12:31 GMT" }, { "Expect", "random" }, { "If-Modified-Since", "Wed, 01 Jun 2016 07:00:00 GMT" }, { "Referer", "http://test.com" }, { "Transfer-Encoding", "chunked" }, { "User-Agent", "test client" }, { "Custom-Header", "value" } }; var cookies = new CookieCollection(); cookies.Add(new Cookie() { Name = "cookie1", Value = "value1", Domain = baseAddress.Host }); cookies.Add(new Cookie() { Name = "cookie2", Value = "value2", Domain = baseAddress.Host }); client.Send("/path", "POST", "request", headers, cookies); } player.Stop(); var record = cassette.Find("record1"); var mock = (JObject)record.Read(); var jrequest = (JObject)mock["request"]; var jresponse = (JObject)mock["response"]; Assert.AreEqual(1, record.List.Count); Assert.AreEqual("POST", jrequest["method"].ToString()); Assert.AreEqual("http://localhost:5560/path", jrequest["uri"].ToString()); Assert.AreEqual("request", jrequest["content"].ToString()); var requestHeadersCount = ((JObject)jrequest["headers"]).Count; if (jrequest["headers"]["Connection"] == null) { Assert.AreEqual(11, requestHeadersCount); } else { Assert.AreEqual(12, requestHeadersCount); Assert.AreEqual("Keep-Alive", jrequest["headers"]["Connection"].ToString()); } Assert.AreEqual("value", jrequest["headers"]["Custom-Header"].ToString()); Assert.AreEqual("Wed, 01 Jun 2016 08:12:31 GMT", jrequest["headers"]["Date"].ToString()); Assert.AreEqual("chunked", jrequest["headers"]["Transfer-Encoding"].ToString()); Assert.AreEqual("text/plain; charset=ascii", jrequest["headers"]["Content-Type"].ToString()); Assert.AreEqual("text/plain", jrequest["headers"]["Accept"].ToString()); Assert.AreEqual("cookie1=value1; cookie2=value2", jrequest["headers"]["Cookie"].ToString()); Assert.AreEqual("random,100-continue", jrequest["headers"]["Expect"].ToString()); Assert.AreEqual("localhost:5560", jrequest["headers"]["Host"].ToString()); Assert.AreEqual("Wed, 01 Jun 2016 07:00:00 GMT", jrequest["headers"]["If-Modified-Since"].ToString()); Assert.AreEqual("http://test.com", jrequest["headers"]["Referer"].ToString()); Assert.AreEqual("test client", jrequest["headers"]["User-Agent"].ToString()); Assert.AreEqual(200, (int)jresponse["statusCode"]); Assert.AreEqual("Hurrah!", jresponse["statusDescription"].ToString()); Assert.AreEqual("response", jresponse["content"].ToString()); Assert.AreEqual(9, ((JObject)jresponse["headers"]).Count); Assert.AreEqual("us-ascii", jresponse["headers"]["Content-Encoding"].ToString()); Assert.AreEqual("value", jresponse["headers"]["Custom-Header"].ToString()); Assert.AreEqual("close", jresponse["headers"]["Connection"].ToString()); Assert.AreEqual("8", jresponse["headers"]["Content-Length"].ToString()); Assert.AreEqual("text/plain; charset=ascii", jresponse["headers"]["Content-Type"].ToString()); Assert.IsNotNull(jresponse["headers"]["Date"]); Assert.AreEqual("http://test.com", jresponse["headers"]["Location"].ToString()); Assert.AreEqual("cookie1=value1, cookie2=value2", jresponse["headers"]["Set-Cookie"].ToString()); Assert.IsNotNull(jresponse["headers"]["Server"]); }
public void Recording_HandlesBinaryContent() { long requestContentLength = 0, responseContentLength = 0; var cassette = new Cassette(Context.CreateCassette("new")); player.Start(); player.Load(cassette); player.Record("record1"); Action <HttpListenerRequest, HttpListenerResponse> callback = delegate(HttpListenerRequest request, HttpListenerResponse response) { response.StatusCode = 200; response.StatusDescription = "Get your file"; response.KeepAlive = false; response.ContentType = "image/gif; charset=ascii"; using (var bitmap = new Bitmap(40, 50)) using (var stream = new MemoryStream()) { bitmap.Save(stream, ImageFormat.Gif); var content = stream.ToArray(); responseContentLength = response.ContentLength64 = content.Length; response.OutputStream.Write(content, 0, content.Length); } }; using (var server = new Server(remoteAddress1)) { Task.Run(() => server.Accept(callback)); var client = new Client(baseAddress); var headers = new NameValueCollection { { "Connection", "Keep-Alive" }, { "Content-Type", "image/png" }, { "Date", "Sat, 13 Oct 2018 18:12:00 GMT" }, { "User-Agent", "test client" }, { "Cache-Control", "no - cache" } }; using (var bitmap = new Bitmap(10, 20)) using (var stream = new MemoryStream()) { bitmap.Save(stream, ImageFormat.Png); requestContentLength = stream.Length; client.Send("/path", "POST", stream.ToArray(), headers); } } player.Stop(); var record = cassette.Find("record1"); var mock = (JObject)record.Read(); var jrequest = (JObject)mock["request"]; var jresponse = (JObject)mock["response"]; Assert.AreEqual(requestContentLength, (int)jrequest["headers"]["Content-Length"]); Assert.AreEqual(200, (int)jresponse["statusCode"]); Assert.AreEqual(responseContentLength, (int)jresponse["headers"]["Content-Length"]); }