public void TestHeaderMissingInErrorResponse()
        {
            var body = MakeErrorPB("Sample Error Code", "Sample Error Message");

            OTSClientTestHelper.SetHTTPResponseBody(body);
            OTSClientTestHelper.SetHttpStatusCode(HttpStatusCode.BadRequest);
            var headers = MakeResponseHeaders("/ListTable", body);

            headers.Remove("x-ots-contentmd5");
            headers.Remove("x-ots-requestid");
            headers.Remove("x-ots-date");
            headers.Remove("x-ots-contenttype");
            headers["Authorization"] = String.Format("OTS {0}:{1}",
                                                     TestAccessKeyID,
                                                     MakeSignature("/ListTable", headers, TestAccessKeySecret));
            OTSClientTestHelper.SetHttpRequestHeaders(headers);

            var request = new ListTableRequest();

            try {
                var response = OTSClient.ListTable(request);
                Assert.Fail();
            } catch (OTSServerException e) {
                AssertOTSServerException(
                    new OTSServerException("/ListTable", HttpStatusCode.BadRequest, "Sample Error Code", "Sample Error Message"),
                    e);
            }
        }
        public void TestInvalidPBInError()
        {
            OTSClientTestHelper.SetHttpStatusCode(HttpStatusCode.BadRequest);
            OTSClientTestHelper.SetHTTPResponseBody(new byte[] {});

            var request = new ListTableRequest();

            try {
                var response = OTSClient.ListTable(request);
                Assert.Fail();
            } catch (OTSServerException e) {
                AssertOTSServerException(new OTSServerException("/ListTable", HttpStatusCode.BadRequest), e);
            }

            var body = new byte[20];

            OTSClientTestHelper.SetHttpStatusCode(HttpStatusCode.BadRequest);
            OTSClientTestHelper.SetHTTPResponseBody(body);
            var headers = MakeResponseHeaders("/ListTable", body);

            OTSClientTestHelper.SetHttpRequestHeaders(headers);

            request = new ListTableRequest();

            try {
                var response = OTSClient.ListTable(request);
                Assert.Fail();
            } catch (OTSServerException e) {
                AssertOTSServerException(new OTSServerException("/ListTable", HttpStatusCode.BadRequest), e);
            }
        }
        public void TestDateDifference()
        {
            var body = MakeListTableResponseBody();

            OTSClientTestHelper.SetHTTPResponseBody(body);
            OTSClientTestHelper.SetHttpStatusCode(HttpStatusCode.OK);
            var headers = MakeResponseHeaders("/ListTable", body);

            headers["x-ots-date"]    = Util.OtsUtils.FormatDateTimeStr(DateTime.UtcNow.AddMinutes(16));
            headers["Authorization"] = String.Format("OTS {0}:{1}",
                                                     TestAccessKeyID,
                                                     MakeSignature("/ListTable", headers, TestAccessKeySecret));
            OTSClientTestHelper.SetHttpRequestHeaders(headers);

            var request = new ListTableRequest();

            try {
                var response = OTSClient.ListTable(request);
                Assert.Fail();
            } catch (OTSClientException e) {
                Assert.AreEqual("The difference between date in response and system time is more than 15 minutes. HTTP Status: OK.", e.Message);
            }

            body = MakeListTableResponseBody();

            OTSClientTestHelper.SetHTTPResponseBody(body);
            OTSClientTestHelper.SetHttpStatusCode(HttpStatusCode.OK);
            headers = MakeResponseHeaders("/ListTable", body);
            headers["x-ots-date"]    = Util.OtsUtils.FormatDateTimeStr(DateTime.UtcNow.AddMinutes(-16));
            headers["Authorization"] = String.Format("OTS {0}:{1}",
                                                     TestAccessKeyID,
                                                     MakeSignature("/ListTable", headers, TestAccessKeySecret));
            OTSClientTestHelper.SetHttpRequestHeaders(headers);

            request = new ListTableRequest();

            try {
                var response = OTSClient.ListTable(request);
                Assert.Fail();
            } catch (OTSClientException e) {
                Assert.AreEqual("The difference between date in response and system time is more than 15 minutes. HTTP Status: OK.", e.Message);
            }
        }
        public void TestContentMD5MismatchInResponse()
        {
            var body = MakeListTableResponseBody();

            OTSClientTestHelper.SetHTTPResponseBody(body);
            OTSClientTestHelper.SetHttpStatusCode(HttpStatusCode.OK);
            var headers = MakeResponseHeaders("/ListTable", new byte[20]);

            OTSClientTestHelper.SetHttpRequestHeaders(headers);

            var request = new ListTableRequest();

            try {
                var response = OTSClient.ListTable(request);
                Assert.Fail();
            } catch (OTSClientException e) {
                Assert.AreEqual("MD5 mismatch in response. HTTP Status: OK.", e.Message);
            }
        }
        public void TestInvalidDateInResponse()
        {
            var body = MakeListTableResponseBody();

            OTSClientTestHelper.SetHTTPResponseBody(body);
            OTSClientTestHelper.SetHttpStatusCode(HttpStatusCode.OK);
            var headers = MakeResponseHeaders("/ListTable", body);

            headers["x-ots-date"] = "Invalid Date String";
            OTSClientTestHelper.SetHttpRequestHeaders(headers);

            var request = new ListTableRequest();

            try {
                var response = OTSClient.ListTable(request);
                Assert.Fail();
            } catch (OTSClientException e) {
                Assert.AreEqual("Invalid date format in response: Invalid Date String HTTP Status: OK.", e.Message);
            }
        }
        public void TestInvalidAuthorizationFormat()
        {
            var body = MakeListTableResponseBody();

            OTSClientTestHelper.SetHTTPResponseBody(body);
            OTSClientTestHelper.SetHttpStatusCode(HttpStatusCode.OK);
            var headers = MakeResponseHeaders("/ListTable", body);

            headers["Authorization"] = String.Format("blahblah");
            OTSClientTestHelper.SetHttpRequestHeaders(headers);

            var request = new ListTableRequest();

            try {
                var response = OTSClient.ListTable(request);
                Assert.Fail();
            } catch (OTSClientException e) {
                Assert.IsTrue(true);
            }
        }
        public void TestNoRequestIDInErrorResponse()
        {
            var body = MakeErrorPB("Sample Error Code", "Sample Error Message");

            OTSClientTestHelper.SetHttpStatusCode(HttpStatusCode.BadRequest);
            OTSClientTestHelper.SetHTTPResponseBody(body);
            var headers = MakeResponseHeaders("/ListTable", body, hasRequestID: false);

            OTSClientTestHelper.SetHttpRequestHeaders(headers);

            var request = new ListTableRequest();

            try {
                var response = OTSClient.ListTable(request);
                Assert.Fail();
            } catch (OTSServerException e) {
                AssertOTSServerException(
                    new OTSServerException("/ListTable", HttpStatusCode.BadRequest, "Sample Error Code", "Sample Error Message"),
                    e);
            }
        }
        public void TestInvalidPBInError()
        {
            OTSClientTestHelper.SetHttpStatusCode(HttpStatusCode.BadRequest);
            OTSClientTestHelper.SetHTTPResponseBody(new byte[] { });

            var request = new ListTableRequest();

            try
            {
                var response = OTSClient.ListTable(request);
                Assert.Fail();
            }
            catch (OTSServerException e)
            {
                AssertOTSServerException(new OTSServerException("/ListTable", HttpStatusCode.BadRequest), e);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
        public void TestAccessIDInAuthorizationMismatch()
        {
            var body = MakeListTableResponseBody();

            OTSClientTestHelper.SetHTTPResponseBody(body);
            OTSClientTestHelper.SetHttpStatusCode(HttpStatusCode.OK);
            var headers = MakeResponseHeaders("/ListTable", body);

            headers["Authorization"] = String.Format("OTS {0}:{1}",
                                                     "AnotherAccessKeyID",
                                                     MakeSignature("/ListTable", headers, TestAccessKeySecret));
            OTSClientTestHelper.SetHttpRequestHeaders(headers);

            var request = new ListTableRequest();

            try {
                var response = OTSClient.ListTable(request);
                Assert.Fail();
            } catch (OTSClientException) {
                Assert.IsTrue(true);
            }
        }
        public void TestAuthorizationHeaderMissingWhenForbidden()
        {
            var body = MakeErrorPB("Sample Error Code", "Sample Error Message");

            OTSClientTestHelper.SetHTTPResponseBody(body);
            OTSClientTestHelper.SetHttpStatusCode(HttpStatusCode.Forbidden);
            var headers = MakeResponseHeaders("/ListTable", body);

            headers.Remove("x-ots-authorization");
            OTSClientTestHelper.SetHttpRequestHeaders(headers);

            var request = new ListTableRequest();

            try {
                var response = OTSClient.ListTable(request);
                Assert.Fail();
            } catch (OTSServerException e) {
                AssertOTSServerException(
                    new OTSServerException("/ListTable", HttpStatusCode.Forbidden, "Sample Error Code", "Sample Error Message"),
                    e);
            }
        }
        public void TestUnicodeInErrorCode()
        {
            var body = MakeErrorPB("中文错误码", "Sample Error Message");

            OTSClientTestHelper.SetHttpStatusCode(HttpStatusCode.BadRequest);
            OTSClientTestHelper.SetHTTPResponseBody(body);
            var headers = MakeResponseHeaders("/ListTable", body);

            OTSClientTestHelper.SetHttpRequestHeaders(headers);

            var request = new ListTableRequest();

            try {
                var response = OTSClient.ListTable(request);
                Assert.Fail();
            } catch (OTSServerException e) {
                AssertOTSServerException(
                    new OTSServerException("/ListTable", HttpStatusCode.BadRequest, "中文错误码", "Sample Error Message"),
                    e);
                Assert.AreEqual("fake-request-id-for-test", e.RequestID);
            }
        }