HisResponseWasExceptional - throwing exception for non-200 web responses
This commit is contained in:
Родитель
a094146f6a
Коммит
ff6600ca68
|
@ -0,0 +1,19 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdexcept>
|
||||
#include <cpprest\basic_types.h>
|
||||
#include <cpprest\asyncrt_utils.h>
|
||||
|
||||
namespace signalr
|
||||
{
|
||||
class web_exception : public std::runtime_error
|
||||
{
|
||||
public:
|
||||
web_exception(const utility::string_t &what)
|
||||
: runtime_error(utility::conversions::to_utf8string(what))
|
||||
{}
|
||||
};
|
||||
}
|
|
@ -13,7 +13,8 @@ namespace signalr
|
|||
{
|
||||
struct web_response
|
||||
{
|
||||
unsigned short code;
|
||||
unsigned short status_code;
|
||||
utility::string_t reason_phrase;
|
||||
pplx::task<utility::string_t> body;
|
||||
};
|
||||
}
|
|
@ -90,6 +90,7 @@
|
|||
<ClInclude Include="..\..\..\..\include\signalrclient\connection.h" />
|
||||
<ClInclude Include="..\..\..\..\include\signalrclient\trace_level.h" />
|
||||
<ClInclude Include="..\..\..\..\include\signalrclient\transport_type.h" />
|
||||
<ClInclude Include="..\..\..\..\include\signalrclient\web_exception.h" />
|
||||
<ClInclude Include="..\..\..\..\include\signalrclient\web_request.h" />
|
||||
<ClInclude Include="..\..\..\..\include\signalrclient\web_response.h" />
|
||||
<ClInclude Include="..\..\..\..\include\signalrclient\_exports.h" />
|
||||
|
|
|
@ -48,6 +48,9 @@
|
|||
<ClInclude Include="..\..\http_sender.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\..\..\include\signalrclient\web_exception.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="..\..\stdafx.cpp">
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include <cpprest\basic_types.h>
|
||||
#include <cpprest\http_client.h>
|
||||
#include "signalrclient\web_response.h"
|
||||
#include "signalrclient\web_exception.h"
|
||||
|
||||
namespace signalr
|
||||
{
|
||||
|
@ -23,10 +24,15 @@ namespace signalr
|
|||
// TODO: set headers, user agent etc.
|
||||
request.set_user_agent(_XPLATSTR(""));
|
||||
|
||||
// TODO: check status code, catch exceptions
|
||||
|
||||
return request.get_response().then([](web_response response)
|
||||
{
|
||||
if (response.status_code != 200)
|
||||
{
|
||||
utility::ostringstream_t oss;
|
||||
oss << _XPLATSTR("web exception: ") << response.status_code << _XPLATSTR(" ") << response.reason_phrase;
|
||||
throw web_exception(oss.str());
|
||||
}
|
||||
|
||||
return response.body;
|
||||
});
|
||||
}
|
||||
|
|
|
@ -3,21 +3,20 @@
|
|||
|
||||
#include "stdafx.h"
|
||||
#include <cpprest\basic_types.h>
|
||||
#include <cpprest\asyncrt_utils.h>
|
||||
#include "http_sender.h"
|
||||
#include "signalrclient\web_exception.h"
|
||||
|
||||
using namespace signalr;
|
||||
|
||||
struct web_request_stub
|
||||
{
|
||||
unsigned short m_status_code;
|
||||
utility::string_t m_reason_code;
|
||||
utility::string_t m_response_body;
|
||||
utility::string_t m_method;
|
||||
utility::string_t m_user_agent_string;
|
||||
|
||||
web_request_stub(unsigned short status_code, utility::string_t response_body)
|
||||
: m_status_code(status_code), m_response_body(response_body)
|
||||
{}
|
||||
|
||||
void set_method(const utility::string_t &method)
|
||||
{
|
||||
m_method = method;
|
||||
|
@ -31,14 +30,33 @@ struct web_request_stub
|
|||
pplx::task<web_response> get_response()
|
||||
{
|
||||
return pplx::task_from_result<web_response>(
|
||||
web_response{ m_status_code, pplx::task_from_result<utility::string_t>(m_response_body) });
|
||||
web_response{ m_status_code, m_reason_code, pplx::task_from_result<utility::string_t>(m_response_body) });
|
||||
}
|
||||
};
|
||||
|
||||
TEST(http_sender_get_response, request_prepared_and_sent)
|
||||
{
|
||||
utility::string_t response_body{ _XPLATSTR("response body") };
|
||||
web_request_stub request{ (unsigned short)200, response_body };
|
||||
web_request_stub request{ (unsigned short)200, _XPLATSTR("OK"), response_body };
|
||||
|
||||
ASSERT_EQ(response_body, http_sender::get<web_request_stub>(request).get());
|
||||
}
|
||||
|
||||
|
||||
TEST(http_sender_get_response, exception_thrown_if_status_code_not_200)
|
||||
{
|
||||
utility::string_t response_phrase(_XPLATSTR("Custom Not Found"));
|
||||
web_request_stub request{ (unsigned short)404, response_phrase };
|
||||
|
||||
try
|
||||
{
|
||||
http_sender::get(request).get();
|
||||
ASSERT_TRUE(false); // exception not thrown
|
||||
}
|
||||
catch (const web_exception &e)
|
||||
{
|
||||
ASSERT_EQ(
|
||||
_XPLATSTR("web exception: 404 ") + response_phrase,
|
||||
utility::conversions::print_string(e.what()));
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче