public void Test() { IHttpRequestFactory<Stream> factory = new HttpWebRequestFactory(); var request = factory.CreateHttpRequest(new Uri(@"https://testuri")); Assert.IsNotNull(request); request.Method = "PUT"; var httpWebRequest = ((HttpRequest)request).Request; Assert.AreEqual("PUT", httpWebRequest.Method); var putObjectRequest = new PutObjectRequest { BucketName = "TestBucket", Key = "TestKey", ContentBody = "Test_Content", }; var proxyCreds = new System.Net.NetworkCredential("UserName","Password"); var requestContext = new RequestContext(true) { ClientConfig = new AmazonS3Config { ConnectionLimit = 10, MaxIdleTime = 1000, ProxyCredentials = proxyCreds, ProxyHost = "proxyhost", ProxyPort = 8080, ReadWriteTimeout = TimeSpan.FromSeconds(20), Timeout = TimeSpan.FromSeconds(40), UseNagleAlgorithm = false, }, Marshaller = new PutObjectRequestMarshaller(), OriginalRequest = putObjectRequest, Request = new PutObjectRequestMarshaller().Marshall(putObjectRequest), Unmarshaller = new PutObjectResponseUnmarshaller() }; request.ConfigureRequest(requestContext); Assert.IsInstanceOfType(httpWebRequest.Proxy, typeof(WebProxy)); Assert.AreEqual(new Uri("http://proxyhost:8080"), ((WebProxy)httpWebRequest.Proxy).Address); Assert.AreEqual(proxyCreds, httpWebRequest.Proxy.Credentials); Assert.AreEqual(40000, httpWebRequest.Timeout); Assert.AreEqual(20000, httpWebRequest.ReadWriteTimeout); Assert.AreEqual(1000, httpWebRequest.ServicePoint.MaxIdleTime); Assert.AreEqual(false, httpWebRequest.ServicePoint.UseNagleAlgorithm); var date = DateTime.Now.ToUniversalTime(); request.SetRequestHeaders(new Dictionary<string, string> { {"Accept","text/plain"}, //{"Connection","Keep-Alive"}, {"Content-Type","application/json"}, {"Content-Length","100"}, //{"Expect","100-continue"}, {"User-Agent","awssdk"}, {"Date",date.ToString("r")}, //{"Host","s3.amazonaws.com"}, {"Range","bytes=100-200"}, {"Content-Range","bytes 100-300/*"}, {"If-Modified-Since",date.ToString("r")}, {"Expires",date.ToString("r")}, {"NonStandardHeader","TestValue"}, }); Assert.AreEqual("text/plain", httpWebRequest.Accept); Assert.AreEqual("application/json", httpWebRequest.ContentType); Assert.AreEqual(100, httpWebRequest.ContentLength); Assert.AreEqual("awssdk", httpWebRequest.UserAgent); Assert.AreEqual(DateTime.Parse(date.ToString("r")), httpWebRequest.Date); Assert.AreEqual("testuri", httpWebRequest.Host); Assert.AreEqual(DateTime.Parse(date.ToString("r")), httpWebRequest.IfModifiedSince); Assert.AreEqual(DateTime.Parse(date.ToString("r")), DateTime.Parse(httpWebRequest.Headers["Expires"])); Assert.AreEqual("bytes=100-200", httpWebRequest.Headers["Range"]); Assert.AreEqual("bytes 100-300/*", httpWebRequest.Headers["Content-Range"]); Assert.AreEqual("TestValue", httpWebRequest.Headers["NonStandardHeader"]); var sourceStream = new MemoryStream(Encoding.UTF8.GetBytes("Test_Content")); var length = sourceStream.Length; var destinationStream = new MemoryStream(); request.WriteToRequestBody(destinationStream, sourceStream, null, 1024); var sourceContent = Encoding.UTF8.GetBytes("Test_Content"); destinationStream = new MemoryStream(); request.WriteToRequestBody(destinationStream, sourceContent, null); }
public void Test() { var proxyCreds = new System.Net.NetworkCredential("UserName", "Password"); var clientConfig = new AmazonS3Config { ConnectionLimit = 10, MaxIdleTime = 1000, ProxyCredentials = proxyCreds, ProxyHost = "proxyhost", ProxyPort = 8080, ReadWriteTimeout = TimeSpan.FromSeconds(20), Timeout = TimeSpan.FromSeconds(40), UseNagleAlgorithm = false, RegionEndpoint = RegionEndpoint.USEast1 }; var requestFactory = new HttpRequestMessageFactory(clientConfig); IHttpRequestFactory<HttpContent> factory = requestFactory; var request = factory.CreateHttpRequest(new Uri(@"https://testuri")); Assert.IsNotNull(request); request.Method = "PUT"; var httpWebRequestMessage = ((HttpWebRequestMessage)request); var httpRequestMessage = httpWebRequestMessage.Request; Assert.AreEqual(TimeSpan.FromSeconds(40), httpWebRequestMessage.HttpClient.Timeout); Assert.AreEqual(false, httpWebRequestMessage.HttpClient.DefaultRequestHeaders.ExpectContinue); Assert.AreEqual(HttpMethod.Put, httpRequestMessage.Method); var putObjectRequest = new PutObjectRequest { BucketName = "TestBucket", Key = "TestKey", ContentBody = "Test_Content", }; var requestContext = new RequestContext(true) { ClientConfig = clientConfig, Marshaller = new PutObjectRequestMarshaller(), OriginalRequest = putObjectRequest, Request = new PutObjectRequestMarshaller().Marshall(putObjectRequest), Unmarshaller = new PutObjectResponseUnmarshaller() }; request.ConfigureRequest(requestContext); var date = DateTime.Now.ToUniversalTime(); var headers = new Dictionary<string, string> { {"Accept","text/plain"}, //{"Connection","Keep-Alive"}, {"Content-Type","application/json"}, {"Content-Length","100"}, //{"Expect","100-continue"}, {"User-Agent","awssdk"}, {"Date",date.ToString("r")}, {"Host","s3.amazonaws.com"}, {"Range","bytes=100-200"}, {"Content-Range","bytes 100-300/*"}, {"If-Modified-Since",date.ToString("r")}, {"Expires",date.ToString("r")}, {"NonStandardHeader","TestValue"}, }; request.SetRequestHeaders(headers); var sourceStream = new MemoryStream(Encoding.UTF8.GetBytes("Test_Content")); var length = sourceStream.Length; var destinationStream = httpRequestMessage.Content; request.WriteToRequestBody(destinationStream, sourceStream, headers, requestContext); Assert.AreEqual(length, httpRequestMessage.Content.Headers.ContentLength); var sourceContent = Encoding.UTF8.GetBytes("Test_Content123"); destinationStream = httpRequestMessage.Content; request.WriteToRequestBody(destinationStream, sourceContent, headers); Assert.AreEqual(sourceContent.Length, httpRequestMessage.Content.Headers.ContentLength); Assert.AreEqual("text/plain", httpRequestMessage.Headers.Accept.First().MediaType); Assert.AreEqual("application/json", httpRequestMessage.Content.Headers.ContentType.MediaType); Assert.AreEqual("awssdk", httpRequestMessage.Headers.UserAgent.First().Product.Name); Assert.AreEqual(DateTime.Parse(date.ToString("r")), httpRequestMessage.Headers.Date); Assert.AreEqual("s3.amazonaws.com", httpRequestMessage.Headers.Host); Assert.AreEqual(DateTime.Parse(date.ToString("r")), httpRequestMessage.Headers.IfModifiedSince); Assert.AreEqual(DateTime.Parse(date.ToString("r")), httpRequestMessage.Content.Headers.Expires); Assert.AreEqual("bytes", httpRequestMessage.Headers.Range.Unit); Assert.AreEqual(100, httpRequestMessage.Headers.Range.Ranges.First().From); Assert.AreEqual(200, httpRequestMessage.Headers.Range.Ranges.First().To); Assert.AreEqual("bytes", httpRequestMessage.Content.Headers.ContentRange.Unit); Assert.AreEqual(100, httpRequestMessage.Content.Headers.ContentRange.From); Assert.AreEqual(300, httpRequestMessage.Content.Headers.ContentRange.To); Assert.AreEqual("TestValue", httpRequestMessage.Headers.GetValues("NonStandardHeader").First()); }
public void TestHttpRequestCancellation() { IHttpRequestFactory<Stream> factory = new HttpWebRequestFactory(); var request = factory.CreateHttpRequest(new Uri(@"https://testuri")); request.Method = "PUT"; var httpWebRequest = ((HttpRequest)request).Request; Assert.AreEqual("PUT", httpWebRequest.Method); var putObjectRequest = new PutObjectRequest { BucketName = "TestBucket", Key = "TestKey", ContentBody = "Test_Content", }; var requestContext = new RequestContext(true) { ClientConfig = new AmazonS3Config { }, Marshaller = new PutObjectRequestMarshaller(), OriginalRequest = putObjectRequest, Request = new PutObjectRequestMarshaller().Marshall(putObjectRequest), Unmarshaller = new PutObjectResponseUnmarshaller(), }; request.ConfigureRequest(requestContext); var sourceStream = new MemoryStream(Encoding.UTF8.GetBytes("Test_Content")); var length = sourceStream.Length; var destinationStream = new MemoryStream(); var cts = new CancellationTokenSource(); cts.Cancel(); var token = cts.Token; requestContext.CancellationToken = token; try { request.WriteToRequestBody(destinationStream, sourceStream, null, requestContext); } catch (OperationCanceledException exception) { Assert.AreEqual(token, exception.CancellationToken); Assert.AreEqual(true, exception.CancellationToken.IsCancellationRequested); return; } Assert.Fail("An OperationCanceledException was not thrown"); }