How to construct a basic_request<Tag> object (cpp-netlib) - cpp-netlib

I'm successfully using cpp-netlib (v0.11.0) and I'm developing some unit tests to test my HTTP server and handler function.
The handler function has the following required signature:
typedef boost::network::http::server<RequestHandler> HttpServerType;
void operator()(const HttpServerType::request& request, HttpServerType::response& response)
What I'd like to be able to do is instantiate the request object so I can call the handler's function call operator. But I can't get it to compile.
In the above signature, request is a typedef for basic_request<Tag>,
which has a constructor which takes a reference to a boost::network::uri::uri.
The code below generates an error when compiled using clang 3.5.
boost::network::uri::uri url;
url << uri::scheme("http") << uri::host(host) << uri::port(port) << uri::path(path);
HttpServerType::request request(url);
No matching constructor for initialization of 'HttpServerType::request' (aka 'basic_request<boost::network::http::tags::http_server>');
What am I doing wrong?

I found a way to do it.
The template is specialised for the http_server tag.
template <>
struct basic_request<tags::http_async_server> :
not_quite_pod_request_base<tags::http_async_server>
{};
and this only has a default ctor.
The following code does compile:
HttpServerType::request request;
request.destination.append("fred");
request.method.append("POST");

Related

How to fix v8 deprecated GetFunction?

I'm trying to repair node-osmium so that it works with Node 12 as I've got some old code I'd like to run.
v8 has now fully deprecated a lot of APIs that do not signal failure properly. These were previously only warnings of deprecation soon, they're now errors so it will no longer build. I (think I've) fixed most of these by following this CPP STYLE GUIDE.md's use maybe version of v8 APIs section.
But, i'm stuck with this error for GetFunction:
../src/utils.hpp:39:67: error: no matching function for call to ‘v8::FunctionTemplate::GetFunction()’
Nan::MaybeLocal<v8::Object> maybe_local = Nan::NewInstance(Nan::New(T::constructor)->GetFunction(), 1, &ext);
I assume it's a similar fix as the other functions, but where do I get the context from in this constructor?
extract from node-osmium/src/utils.hpp:
namespace node_osmium {
template<class T>
auto unwrap(const v8::Local<v8::Object>& object) -> decltype(Nan::ObjectWrap::Unwrap<T>(object)->get()) {
return Nan::ObjectWrap::Unwrap<T>(object)->get();
}
template<class T, class... Args>
v8::Local<v8::Object> new_external(Args&&... args) {
Nan::EscapableHandleScope scope;
v8::Local<v8::Value> ext = Nan::New<v8::External>(new T(std::forward<Args>(args)...));
Nan::MaybeLocal<v8::Object> maybe_local = Nan::NewInstance(Nan::New(T::constructor)->GetFunction(context), 1, &ext);
if (maybe_local.IsEmpty()) Nan::ThrowError("Could not create new Buffer instance");
return scope.Escape(maybe_local.ToLocalChecked());
}
v8::Local<v8::Value> create_js_box(const osmium::Box& box);
osmium::osm_entity_bits::type object_to_entity_bits(v8::Local<v8::Object> options);
} // namespace node_osmium

Broken code generation for out function parameters

I made my own interface, simplistic version looks like this:
#ifndef _FOO_IDL_
#define _FOO_IDL_
module FOO {
typedef unsigned long Bar;
interface FOOInterface {
void getBar(out FOO::Bar b);
};
};
#endif
After that I made "REDHAWK IDL Project", used that IDL, compiled, installed.
Then I made Redhawk component, added output port and used that interface on it, did code generation. During compilation I got error:
port_impl.h:26:29: error: expected ‘,’ or ‘...’ before ‘&&’ token
void getBar(FOO::Bar&& b);
It looks like code generator adds excessive ampersand. What could I do about it?
Thank you.

GoogleTest CMake doesn't recognize TEST_F: Like it's not recognizing GTest something

OK, I admit it, this is a unique case. When we build our application we are using make, so I've included my tests in a test folder under src. Then at the same level as our release folder we have created a unit-test folder that includes all of our source files and our test source files.
But my IDE is CLion, which uses CMake. In my CMakeLists.txt file, I have included:
enable_testing()
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})
add_executable(TestProject ${SOURCE_FILES})
target_link_libraries(TestProject ${GTEST_BOTH_LIBRARIES})
I am creating my first Test Fixture. Here is the code:
#include "OPProperties.h"
#include "gtest/gtest.h"
namespace {
// The fixture for testing class OPPropertiesTestTest.
class OPPropertiesTestTest : public ::testing::Test {
protected:
// You can remove any or all of the following functions if its body
// is empty.
OPPropertiesTestTest() {
// You can do set-up work for each test here.
}
virtual ~OPPropertiesTestTest() {
// You can do clean-up work that doesn't throw exceptions here.
}
// If the constructor and destructor are not enough for setting up
// and cleaning up each test, you can define the following methods:
virtual void SetUp() {
// Code here will be called immediately after the constructor (right
// before each test).
}
virtual void TearDown() {
// Code here will be called immediately after each test (right
// before the destructor).
}
// Objects declared here can be used by all tests in the test case for OPPropertiesTestTest.
};
TEST_F(OPPropertiesTestTest, ThisTestWillFail) {
EXPECT_EQ(0, 2);
}
} // namespace
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Here is an image capture:
Notice the syntax checker errors in my TEST_F function. When I started to type TEST_F code completion is trying to find a Boost Test Function.
Can someone tell me what else I need to add to the CMakeLists.txt file or what I am not doing that GTest functions are not being recognized?
As πάντα ῥεῖ pointed out, I hadn't actually tried to build the code. When I did I first received a linker error for pthread, so we added the following line the the CMakeLists.txt file:
target_link_libraries(OnePrint pthread)
Then I tried again to build and received these errors:
/home/user/gtest-1.7.0/lib/.libs/libgtest.so: undefined reference to `pthread_key_create'
/home/user/gtest-1.7.0/lib/.libs/libgtest.so: undefined reference to `pthread_getspecific'
/home/user/gtest-1.7.0/lib/.libs/libgtest.so: undefined reference to `pthread_key_delete'
/home/user/gtest-1.7.0/lib/.libs/libgtest.so: undefined reference to `pthread_setspecific'
collect2: error: ld returned 1 exit status
So, I ran a search on these errors and found this question.
The answer that worked for me was here.

nodejs module unit test method gets called in private class

I'm trying to unit test that a method gets called in my module. The class and method are private and not exposed using module.exports. The modules I'm using for the tests are: mocha, rewire, assert, sinon.spy. The call I want to test is my error method, this currently throws an error, but might change later - so I don't want to test that an error is thrown, just test that class.error() gets called. Not sure how to procede and have tried numerous tuts online.
The class is (currently accessed in tests using rewire):
var MyClass = function MyClass(o){
var self = this
if(!o || typeof o !== 'object')
self.error('No configuration passed to MyClass')
}
MyClass.prototype.error = function(msg){
throw Error(msg)
}
My test currently, which is not working:
it('Constructs MyClass', function(done){
//check constructs normally (this passes and works)
var actual = obj.__get__("MyClass.config")
assert.deepEqual(actual, config)
/**
* check calls error method
*/
//stub class.error ?
//construct class without config
//check if class.error is called
done()
})
In pseudo code, what I'm hoping to do is:
var stub = stub(MyClass)
->do('construct', null) //no config passed
->didCall('error') //check error method is called
This may be a duplicate of: Mocking modules in Node.js for unit testing
But it is throwing an error me: Object #<Object> has no method 'expect'
To solve it I imported the private class constructor using rewire, then I overrode the error method, setting it as a sinon.spy. I construct the class, then check if the spy was called:
//check calls error method if no config passed
var obj = rewire('./path/to/my/module')
, MyClass = obj.__get__("MyClass")
, spy = sinon.spy()
MyClass.prototype.error = spy
var foo = new MyClass()
assert.equal(spy.called, true)
done()

error: cannot allocate an object of abstract type ‘FRONTEND_RFInfo_In_i

Using:
Redhawk 1.9 / CentOS 6.4 (32 bit) / C++ implementation
Creating a new FRONTEND::TUNER device
Using default setting on code generation
The following error message happens when I add the following port required for FRONTEND Digital Tuner and regenerate the code.
<ports>
<provides repid="IDL:FRONTEND/DigitalTuner:1.0" providesname="DigitalTuner"/>
<provides repid="IDL:FRONTEND/RFInfo:1.0" providesname="RFInfo"/>
</ports>
Error message (Problems window):
cannot allocate an object of abstract type
‘FRONTEND_RFInfo_In_i’ TestFrontEndDevice_base.cpp /TestFrontEndDevice/cpp line 50 C/C++ Problem
Error message (console):
port_impl.h:56: note: because the following virtual functions are
pure within ‘FRONTEND_RFInfo_In_i’:
/usr/local/redhawk/core/include/redhawk/FRONTEND/RFInfo.h:323: note:
virtual void FRONTEND::_impl_RFInfo::rf_flow_id(const char*)
/usr/local/redhawk/core/include/redhawk/FRONTEND/RFInfo.h:325: note:
virtual void FRONTEND::_impl_RFInfo::rfinfo_pkt(const
FRONTEND::RFInfoPkt&)
make: * [TestFrontEndDevice-TestFrontEndDevice_base.o] Error 1
There appears to be a bug in the code generation for the RFInfo class. If you compare the signatures of the generated code in the port_impl.h file to those of the "unimplemented" ones above, you'll notice that for the rf_flow_id function in port_impl.h there is no const keyword. The same can be said about the rfinfo_pkt method. It is missing the const keyword and an ampersand in the function declaration.
To fix this, simply add the const keywords and the ampersand in the appropriate places in both the declaration in the port_impl.h file and the definition in the port_impl.cpp file.
This is a known issue that has been fixed for the 1.9.1 release.
The problem is the result of the RFInfo port function signatures in
the generated port_impl.* files being different from those in the
parent/base class, which also happen to be pure virtual. To fix the
issue in your code, you'll need to add "const" to rf_flow_id, and both
"const" and "&" to rfinfo_pkt, as shown below:
In port_impl.h:
- void rf_flow_id( char* data);
+ void rf_flow_id( const char* data);
- void rfinfo_pkt( FRONTEND::RFInfoPkt data);
+ void rfinfo_pkt( const FRONTEND::RFInfoPkt& data);
In port_impl.cpp:
-void FRONTEND_RFInfo_In_i::rf_flow_id( char* data)
+void FRONTEND_RFInfo_In_i::rf_flow_id( const char* data)
-void FRONTEND_RFInfo_In_i::rfinfo_pkt( FRONTEND::RFInfoPkt data)
+void FRONTEND_RFInfo_In_i::rfinfo_pkt( const FRONTEND::RFInfoPkt& data)

Resources