throw when rawResponse was extracted (#1825)

* throw when rawResponse was extracted
This commit is contained in:
Victor Vazquez 2021-03-09 21:50:08 -08:00 committed by GitHub
parent bc564dbddd
commit a4110e44e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 70 additions and 1 deletions

View File

@ -11,6 +11,7 @@
#include "azure/core/http/http.hpp"
#include "azure/core/nullable.hpp"
#include <memory> // for unique_ptr
#include <stdexcept>
#include <utility> // for move
namespace Azure {
@ -50,7 +51,14 @@ public:
* @brief Get raw HTTP response.
*/
// Do not give up raw response ownership.
Azure::Core::Http::RawResponse& GetRawResponse() { return *this->m_rawResponse; }
Azure::Core::Http::RawResponse& GetRawResponse() const
{
if (!m_rawResponse)
{
throw std::runtime_error("The raw response was extracted before.");
}
return *this->m_rawResponse;
}
/**
* @brief Check whether a value is contained.

View File

@ -52,6 +52,7 @@ add_executable (
operation_status.cpp
pipeline.cpp
policy.cpp
response_t.cpp
simplified_header.cpp
string.cpp
telemetry_policy.cpp

View File

@ -0,0 +1,60 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: MIT
#include <gtest/gtest.h>
#include <azure/core/http/http.hpp>
#include <azure/core/response.hpp>
#include <memory>
#include <stdexcept>
#include <string>
using namespace Azure::Core;
using namespace Azure::Core::Http;
TEST(ResponseT, extractAndGet)
{
// Create Response<T> and extract the rawResponse
auto rawResponse = std::make_unique<RawResponse>(1, 1, HttpStatusCode::Accepted, "Something");
std::string fakeT("pretending this is the T");
Azure::Response<std::string> response(fakeT, std::move(rawResponse));
// rawResponse moved to Response<T>
EXPECT_EQ(nullptr, rawResponse);
// This is fine because the rawResponse is still in the Response<T>
EXPECT_NO_THROW(response.GetRawResponse());
rawResponse = response.ExtractRawResponse();
// rawResponse is now valid again
EXPECT_NE(nullptr, rawResponse);
// Can't get a ref from rawResponse since it was extracted
EXPECT_THROW(response.GetRawResponse(), std::runtime_error);
// This is fine, we can keep extracting the rawReponse, only that the second time the it would be
// a nullptr
EXPECT_NO_THROW(rawResponse = response.ExtractRawResponse());
EXPECT_EQ(nullptr, rawResponse);
}
TEST(ResponseT, value)
{
// Create Response<T> and test API
auto rawResponse = std::make_unique<RawResponse>(1, 1, HttpStatusCode::Accepted, "Something");
std::string fakeT("pretending this is the T");
Azure::Response<std::string> response(fakeT, std::move(rawResponse));
EXPECT_EQ(fakeT, *response);
// use the reference to update the T inside the Response.
EXPECT_NO_THROW(response->clear());
EXPECT_EQ("", *response);
// const Response
std::string constFakeT("pretending this is the T");
Azure::Response<std::string> const constResponse(constFakeT, std::move(rawResponse));
// Use * and -> on const Response
EXPECT_EQ(constFakeT, *constResponse);
EXPECT_EQ(*constFakeT.data(), *constResponse->data());
}