private static HttpWebResponse GetListBucketsResponse() { var body = @"<?xml version=""1.0"" encoding=""UTF-8""?> <ListAllMyBucketsResult xmlns=""http://s3.amazonaws.com/doc/2006-03-01/""> <Owner> <ID>123456789</ID> <DisplayName>test-account</DisplayName> </Owner> <Buckets> <Bucket> <Name>Bucket1</Name> <CreationDate>2014-04-18T17:56:06.000Z</CreationDate> </Bucket> <Bucket> <Name>Bucket2</Name> <CreationDate>2014-06-05T23:18:53.000Z</CreationDate> </Bucket> <Bucket> <Name>Bucket3</Name> <CreationDate>2014-06-05T23:18:53.000Z</CreationDate> </Bucket> <Bucket> <Name>Bucket4</Name> <CreationDate>2014-06-05T23:18:53.000Z</CreationDate> </Bucket> </Buckets> </ListAllMyBucketsResult>"; var headers = new Dictionary <string, string>(); headers["x-amz-request-id"] = "357B62B8E32948B9"; headers["x-amz-id-2"] = "TFD+oMfRT5nVXm81tex2+Uh8R/VZRaztjPALzXknR7IC3RGfVPhpuiiHMtL0fFKF"; headers["Content-Type"] = "application/xml"; headers["Transfer-Encoding"] = "chunked"; headers["Date"] = "Mon, 23 Jun 2014 03:31:12 GMT"; headers["Server"] = "AmazonS3"; var response = MockWebResponse.Create(HttpStatusCode.OK, headers, body) as HttpWebResponse; return(response); }
public void TestGetObjectResponseValidChecksum(string header, string checksumValue, CoreChecksumAlgorithm expectedAlgorithm) { Tester.Reset(); var context = CreateTestContext(); var request = new GetObjectRequest { BucketName = "foo", Key = "bar", ChecksumMode = ChecksumMode.ENABLED }; ((RequestContext)context.RequestContext).OriginalRequest = request; ((RequestContext)context.RequestContext).Request = new GetObjectRequestMarshaller().Marshall(request); ((RequestContext)context.RequestContext).Unmarshaller = GetObjectResponseUnmarshaller.Instance; var expectedResponseBody = "Hello world"; var response = MockWebResponse.Create(HttpStatusCode.OK, new Dictionary <string, string>(), expectedResponseBody); response.Headers.Add("Content-Length", "11"); response.Headers.Add(header, checksumValue); context.ResponseContext.HttpResponse = new HttpWebRequestResponseData(response); RuntimePipeline.InvokeSync(context); Assert.AreEqual(1, Tester.CallCount); Assert.IsInstanceOfType(context.ResponseContext.Response, typeof(GetObjectResponse)); var getObjectResponse = context.ResponseContext.Response as GetObjectResponse; Assert.AreEqual(expectedAlgorithm, getObjectResponse.ResponseMetadata.ChecksumAlgorithm); Assert.AreEqual(ChecksumValidationStatus.PENDING_RESPONSE_READ, getObjectResponse.ResponseMetadata.ChecksumValidationStatus); // Read the stream to the end to finish checksum calcuation and validation // This implicitly asserts that the checksum is valid because an exception would be thrown otherwise var responseBody = new StreamReader(getObjectResponse.ResponseStream).ReadToEnd(); Assert.AreEqual(expectedResponseBody, responseBody); }
private static Func <HttpHandlerTests.MockHttpRequest, HttpWebResponse> Create( string content, string requestId, bool isOK) { var status = isOK ? HttpStatusCode.OK : HttpStatusCode.NotFound; return((request) => { Dictionary <string, string> headers = new Dictionary <string, string>(StringComparer.Ordinal); if (!string.IsNullOrEmpty(requestId)) { headers.Add(HeaderKeys.RequestIdHeader, requestId); } var response = MockWebResponse.Create(status, headers, content); if (isOK) { return response; } throw new HttpErrorResponseException(new HttpWebRequestResponseData(response)); }); }
public void Test404() { Tester.Reset(); Tester.Action = (int callCount) => { var body = @"<?xml version=""1.0"" encoding=""UTF-8""?> <Error> <Code>NoSuchBucket</Code> <Message>The specified bucket does not exist</Message> <BucketName>nonexistentbucket</BucketName> <RequestId>749945D6E23AFF48</RequestId> <HostId>xN4fcAA90mhYmTC8o/SX2jBBunBSrD45cMwLYwAu0LZhcn9HIU/KXwtieQO05beD</HostId> </Error>"; var headers = new Dictionary <string, string>(); headers["x-amz-request-id"] = "749945D6E23AFF48"; headers["x-amz-id-2"] = "xN4fcAA90mhYmTC8o/SX2jBBunBSrD45cMwLYwAu0LZhcn9HIU/KXwtieQO05beD"; headers["Content-Type"] = "application/xml"; headers["Transfer-Encoding"] = "chunked"; headers["Date"] = "Mon, 16 Jun 2014 17:34:56 GMT"; headers["Server"] = "AmazonS3"; var errorResponse = (HttpWebResponse)MockWebResponse.Create(HttpStatusCode.NotFound, headers, body); throw new HttpErrorResponseException(new HttpWebRequestResponseData(errorResponse)); }; var context = CreateTestContext(); var exception = Utils.AssertExceptionExpected(() => { RuntimePipeline.InvokeSync(context); }, typeof(AmazonS3Exception)); Assert.AreEqual("NoSuchBucket", ((AmazonS3Exception)exception).ErrorCode); Assert.AreEqual(HttpStatusCode.NotFound, ((AmazonS3Exception)exception).StatusCode); Assert.AreEqual(1, Tester.CallCount); }