<commit_before>/*
 * Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 * 
 *  http://aws.amazon.com/apache2.0
 * 
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */


#include <aws/external/gtest.h>
#include <aws/core/client/AWSErrorMarshaller.h>
#include <aws/core/client/AWSError.h>
#include <aws/core/client/CoreErrors.h>
#include <aws/core/http/standard/StandardHttpResponse.h>
#include <aws/core/http/standard/StandardHttpRequest.h>
#include <memory>

using namespace Aws::Client;
AWS_CORE_API extern const char* MESSAGE_LOWER_CASE;
AWS_CORE_API extern const char* MESSAGE_CAMEL_CASE;
AWS_CORE_API extern const char* ERROR_TYPE_HEADER;
AWS_CORE_API extern const char* TYPE;

enum JsonErrorResponseStyle
{
    LowerCaseMessage = 1, // error message is in JSON body keyed with "message"
    UpperCaseMessage = 2, // error message is in JSON body keyed with "MESSAGE"
    Header = 4, // exception is stated in the header
    ResponseCode = 8 // exception should be derived from HTTP response code
};

enum XmlErrorResponseStyle
{
    SingularErrorNode = 1,
    PluralErrorNode = 2,
    IllFormed = 4
};

static const char ERROR_MARSHALLER_TEST_ALLOC_TAG[] = "ErrorMarshllerTestAllocTag";

static Aws::UniquePtr<Aws::Http::HttpResponse> BuildHttpResponse(const Aws::String& exception, const Aws::String& message, int style = LowerCaseMessage)
{
    using namespace Aws::Http;
    using namespace Aws::Http::Standard;
    StandardHttpRequest fakeRequest("/some/uri", Aws::Http::HttpMethod::HTTP_GET);
    auto ss = Aws::New<Aws::StringStream>(ERROR_MARSHALLER_TEST_ALLOC_TAG);
    fakeRequest.SetResponseStreamFactory([=] { return ss; });
    if (style & LowerCaseMessage)
    {
        *ss << "{\"" << MESSAGE_LOWER_CASE << "\":\"" << message << "\"";
    }
    else
    {
        *ss << "{\"" << MESSAGE_CAMEL_CASE << "\":\"" << message << "\"";
    }

    auto response = Aws::MakeUnique<StandardHttpResponse>(ERROR_MARSHALLER_TEST_ALLOC_TAG, fakeRequest);

    if (!(style & Header)) 
    {
        *ss << ",\"" << TYPE << "\":\"" << exception << "\"}";
    }
    else 
    {
        *ss << "}";
        response->AddHeader(ERROR_TYPE_HEADER, exception);
    }

    return response;
}

static Aws::UniquePtr<Aws::Http::HttpResponse> BuildHttpXmlResponse(const Aws::String& exception, const Aws::String& message, int style = SingularErrorNode)
{
    using namespace Aws::Http;
    using namespace Aws::Http::Standard;
    StandardHttpRequest fakeRequest("/some/uri", Aws::Http::HttpMethod::HTTP_GET);
    auto ss = Aws::New<Aws::StringStream>(ERROR_MARSHALLER_TEST_ALLOC_TAG);
    fakeRequest.SetResponseStreamFactory([=] { return ss; });
    auto response = Aws::MakeUnique<StandardHttpResponse>(ERROR_MARSHALLER_TEST_ALLOC_TAG, fakeRequest);

    *ss << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    if (style & PluralErrorNode)
    {
        *ss << "<OtherRoot> <Errors>";
    }

    *ss << "<Error>";

    if (style & IllFormed)
    {
        response->SetResponseCode(HttpResponseCode::SERVICE_UNAVAILABLE);
    }
    else 
    {
        *ss << "<Code>" << exception << "</Code>";
    }


    *ss << "<Message>" << message << "</Message>"
        << "<Resource>/mybucket/myfoto.jpg</Resource> "
        << "<RequestId>4442587FB7D0A2F9</RequestId>"
        << "</Error>";
    if (style & PluralErrorNode)
    {
        *ss << "</Errors> </OtherRoot>";
    }
    return response;
}

TEST(XmlErrorMarshallerTest, TestXmlErrorPayload)
{
    XmlErrorMarshaller awsErrorMarshaller;
    Aws::String message = "Test Message";
    AWSError<CoreErrors> error = awsErrorMarshaller.Marshall(*BuildHttpXmlResponse("IncompleteSignatureException", message));
    ASSERT_EQ(CoreErrors::INCOMPLETE_SIGNATURE, error.GetErrorType());
    ASSERT_EQ("IncompleteSignatureException", error.GetExceptionName());
    ASSERT_EQ(message, error.GetMessage());
    ASSERT_FALSE(error.ShouldRetry());

    error = awsErrorMarshaller.Marshall(*BuildHttpXmlResponse("IncompleteSignatureException", message, PluralErrorNode));
    ASSERT_EQ(CoreErrors::INCOMPLETE_SIGNATURE, error.GetErrorType());
    ASSERT_EQ("IncompleteSignatureException", error.GetExceptionName());
    ASSERT_EQ(message, error.GetMessage());
    ASSERT_FALSE(error.ShouldRetry());<commit_msg>Fixed build in core test<commit_after>/*
 * Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 * 
 *  http://aws.amazon.com/apache2.0
 * 
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */


#include <aws/external/gtest.h>
#include <aws/core/client/AWSErrorMarshaller.h>
#include <aws/core/client/AWSError.h>
#include <aws/core/client/CoreErrors.h>
#include <aws/core/http/standard/StandardHttpResponse.h>
#include <aws/core/http/standard/StandardHttpRequest.h>
#include <memory>

using namespace Aws::Client;
AWS_CORE_API extern const char* MESSAGE_LOWER_CASE;
AWS_CORE_API extern const char* MESSAGE_CAMEL_CASE;
AWS_CORE_API extern const char* ERROR_TYPE_HEADER;
AWS_CORE_API extern const char* TYPE;

enum JsonErrorResponseStyle
{
    LowerCaseMessage = 1, // error message is in JSON body keyed with "message"
    UpperCaseMessage = 2, // error message is in JSON body keyed with "MESSAGE"
    Header = 4, // exception is stated in the header
    ResponseCode = 8 // exception should be derived from HTTP response code
};

enum XmlErrorResponseStyle
{
    SingularErrorNode = 1,
    PluralErrorNode = 2,
    IllFormed = 4
};

static const char ERROR_MARSHALLER_TEST_ALLOC_TAG[] = "ErrorMarshllerTestAllocTag";

static Aws::UniquePtr<Aws::Http::HttpResponse> BuildHttpResponse(const Aws::String& exception, const Aws::String& message, int style = LowerCaseMessage)
{
    using namespace Aws::Http;
    using namespace Aws::Http::Standard;
    StandardHttpRequest fakeRequest("/some/uri", Aws::Http::HttpMethod::HTTP_GET);
    auto ss = Aws::New<Aws::StringStream>(ERROR_MARSHALLER_TEST_ALLOC_TAG);
    fakeRequest.SetResponseStreamFactory([=] { return ss; });
    if (style & LowerCaseMessage)
    {
        *ss << "{\"" << MESSAGE_LOWER_CASE << "\":\"" << message << "\"";
    }
    else
    {
        *ss << "{\"" << MESSAGE_CAMEL_CASE << "\":\"" << message << "\"";
    }

    auto response = Aws::MakeUnique<StandardHttpResponse>(ERROR_MARSHALLER_TEST_ALLOC_TAG, fakeRequest);

    if (!(style & Header)) 
    {
        *ss << ",\"" << TYPE << "\":\"" << exception << "\"}";
    }
    else 
    {
        *ss << "}";
        response->AddHeader(ERROR_TYPE_HEADER, exception);
    }

    return std::move(response);
}

static Aws::UniquePtr<Aws::Http::HttpResponse> BuildHttpXmlResponse(const Aws::String& exception, const Aws::String& message, int style = SingularErrorNode)
{
    using namespace Aws::Http;
    using namespace Aws::Http::Standard;
    StandardHttpRequest fakeRequest("/some/uri", Aws::Http::HttpMethod::HTTP_GET);
    auto ss = Aws::New<Aws::StringStream>(ERROR_MARSHALLER_TEST_ALLOC_TAG);
    fakeRequest.SetResponseStreamFactory([=] { return ss; });
    auto response = Aws::MakeUnique<StandardHttpResponse>(ERROR_MARSHALLER_TEST_ALLOC_TAG, fakeRequest);

    *ss << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    if (style & PluralErrorNode)
    {
        *ss << "<OtherRoot> <Errors>";
    }

    *ss << "<Error>";

    if (style & IllFormed)
    {
        response->SetResponseCode(HttpResponseCode::SERVICE_UNAVAILABLE);
    }
    else 
    {
        *ss << "<Code>" << exception << "</Code>";
    }


    *ss << "<Message>" << message << "</Message>"
        << "<Resource>/mybucket/myfoto.jpg</Resource> "
        << "<RequestId>4442587FB7D0A2F9</RequestId>"
        << "</Error>";
    if (style & PluralErrorNode)
    {
        *ss << "</Errors> </OtherRoot>";
    }
    return std::move(response);
}

TEST(XmlErrorMarshallerTest, TestXmlErrorPayload)
{
    XmlErrorMarshaller awsErrorMarshaller;
    Aws::String message = "Test Message";
    AWSError<CoreErrors> error = awsErrorMarshaller.Marshall(*BuildHttpXmlResponse("IncompleteSignatureException", message));
    ASSERT_EQ(CoreErrors::INCOMPLETE_SIGNATURE, error.GetErrorType());
    ASSERT_EQ("IncompleteSignatureException", error.GetExceptionName());
    ASSERT_EQ(message, error.GetMessage());
    ASSERT_FALSE(error.ShouldRetry());

    error = awsErrorMarshaller.Marshall(*BuildHttpXmlResponse("IncompleteSignatureException", message, PluralErrorNode));
    ASSERT_EQ(CoreErrors::INCOMPLETE_SIGNATURE, error.GetErrorType());
    ASSERT_EQ("IncompleteSignatureException", error.GetExceptionName());
    ASSERT_EQ(message, error.GetMessage());
    ASSERT_FALSE(error.ShouldRetry());
{'fraction_non_alphanumeric': 0.1368521868681494, 'fraction_numerical': 0.007023518144088539, 'mean_word_length': 4.195135433941404, 'pattern_counts': {'":': 6, '<': 177, '<?xml version=': 2, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4937693', 'n_tokens_mistral': 2937, 'n_tokens_neox': 2870, 'n_words': 649}