HttpForReal - implementing web_request sending real http requests

This commit is contained in:
moozzyk 2014-10-14 10:30:44 -07:00
Родитель 5e30109a3e
Коммит 54495c9136
7 изменённых файлов: 88 добавлений и 12 удалений

Просмотреть файл

@ -104,12 +104,12 @@
<ClInclude Include="..\..\web_request_factory.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\connection.cpp" />
<ClCompile Include="..\..\stdafx.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="..\..\url_builder.cpp" />
<ClCompile Include="..\..\web_request.cpp" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />

Просмотреть файл

@ -65,10 +65,10 @@
<ClCompile Include="..\..\stdafx.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\connection.cpp">
<ClCompile Include="..\..\url_builder.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\url_builder.cpp">
<ClCompile Include="..\..\web_request.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>

Просмотреть файл

@ -1,9 +0,0 @@
// 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.
#include "stdafx.h"
#include "signalrclient\connection.h"
namespace signalr
{
}

Просмотреть файл

@ -0,0 +1,37 @@
// 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.
#include "stdafx.h"
#include <cpprest\http_client.h>
#include "signalrclient\web_request.h"
namespace signalr
{
void web_request::set_method(const utility::string_t &method)
{
m_method = method;
}
void web_request::set_user_agent(const utility::string_t &user_agent_string)
{
m_user_agent_string = user_agent_string;
}
pplx::task<web_response> web_request::get_response() const
{
web::http::client::http_client client(m_url);
web::http::http_request request(m_method);
request.headers()[_XPLATSTR("User-Agent")] = m_user_agent_string;
return client.request(request)
.then([](web::http::http_response response)
{
return web_response
{
response.status_code(),
response.reason_phrase(),
response.extract_string()
};
});
}
}

Просмотреть файл

@ -98,6 +98,7 @@
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="..\..\url_builder_tests.cpp" />
<ClCompile Include="..\..\web_request_tests.cpp" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\src\SignalRClient\Build\VS2013\SignalRClient.vcxproj">

Просмотреть файл

@ -44,6 +44,9 @@
<ClCompile Include="..\..\request_sender_tests.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\web_request_tests.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />

Просмотреть файл

@ -0,0 +1,44 @@
// 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.
#include "stdafx.h"
#include <cpprest\http_listener.h>
#include "signalrclient\web_request.h"
using namespace web;
using namespace signalr;
TEST(web_request_get_response, sends_request_receives_response)
{
web::uri url(_XPLATSTR("http://localhost:56000/web_request_test"));
auto request_received = false;
utility::string_t user_agent_string;
http::experimental::listener::http_listener listener(url);
listener.support(http::methods::GET, [&request_received, &user_agent_string](http::http_request request)
{
request_received = true;
user_agent_string = request.headers()[_XPLATSTR("User-Agent")];
request.reply(http::status_codes::OK, _XPLATSTR("response"));
});
listener.open()
.then([&url]()
{
web_request request(url);
request.set_method(http::methods::GET);
request.set_user_agent(_XPLATSTR("007"));
request.get_response()
.then([](web_response response)
{
ASSERT_EQ((unsigned short)200, response.status_code);
ASSERT_EQ(_XPLATSTR("OK"), response.reason_phrase);
ASSERT_EQ(_XPLATSTR("response"), response.body.get());
}).wait();
}).wait();
listener.close();
ASSERT_TRUE(request_received);
ASSERT_EQ(_XPLATSTR("007"), user_agent_string);
}