BOOST_FIXTURE_TEST_CASE( updates_created_different_versions, context ) { const json::array response = json_multiple_post( subscribe_to_node1() << asio_mocks::json_msg( "{ 'id': '192.168.210.1:9999/0' }" ) << boost::bind( defered_update_node, boost::ref( data_ ), node1, json::string( "update1"), boost::ref( static_cast< boost::asio::io_service& >( *this ) ) ) << asio_mocks::json_msg( "{ 'id': '192.168.210.1:9999/0' }" ) << asio_mocks::disconnect_read() ); const json::value version1 = response.at( 1u ) .upcast< json::object >() .at( json::string( "update" ) ) .upcast< json::array >() .at( 0 ) .upcast< json::object >() .at( json::string( "version") ); const json::value version2 = response.at( 2u ) .upcast< json::object >() .at( json::string( "update" ) ) .upcast< json::array >() .at( 0 ) .upcast< json::object >() .at( json::string( "version") ); BOOST_CHECK_NE( version1, version2 ); }
void response_base< Timer >::handle_connect( const json::object& request, const boost::shared_ptr< response_interface >& self, bool last_message ) { assert( session_ ); if ( !check_connection_type( request, session_->session_id() ) ) return; // when there are already messages to be send, then there is no point in blocking const bool do_not_block = !bayeux_response_.empty() || !last_message || zero_timeout_advice( request ); const json::array messages = do_not_block ? session_->events() : session_->wait_for_events( self ); if ( !messages.empty() ) { bayeux_response_ += messages; bayeux_response_.add( build_connect_response( request, session_->session_id() ) ); } else { if ( do_not_block ) { bayeux_response_.add( build_connect_response( request, session_->session_id() ) ); } else { blocking_connect_ = request; } } }
void AutoNetServerImpl::HandleSubscribe(websocketpp::connection_hdl hdl) { m_Subscribers.insert(hdl); Json::array types; for(const auto& type : m_AllTypes) { types.push_back(type.first); } SendMessage(hdl, "subscribed", types); auto root = GetContext(); for (auto ctxt : ContextEnumerator{ GetContext() }) { // Send update about this newly discovered context NewContext( ctxt == root ? nullptr : ctxt->GetParentContext().get(), *ctxt ); // Build total image of all objects, recursively: for (const auto* pObj : ctxt->BuildObjectState()) NewObject(*ctxt, *pObj); } // Send breakpoint message for(const auto& breakpoint : m_breakpoints) SendMessage(hdl, "breakpoint", breakpoint); }
/** * @test the current behaviour is, that a first update to a subscription is delivered with the a second * http request. Delivering the initial version of the data with the first http request would be * fine too. */ BOOST_FIXTURE_TEST_CASE( response_to_subscription, context ) { answer_validation_request( node1, true ); answer_authorization_request( node1, true ); answer_initialization_request( node1, json::number( 42 ) ); tools::run( *this ); const json::array response = json_multiple_post( asio_mocks::read_plan() << asio_mocks::json_msg( "{ 'cmd': [ { 'subscribe': { 'a':'1' ,'b':'1' } } ] }" ) << asio_mocks::json_msg( "{ 'id': '192.168.210.1:9999/0' }" ) << asio_mocks::disconnect_read() ); BOOST_REQUIRE_EQUAL( response.length(), 2u ); json::array updates = response.at( 1 ).upcast< json::object >().at( json::string( "update" ) ).upcast< json::array >(); BOOST_REQUIRE_EQUAL( updates.length(), 1u ); json::object update = updates.at( 0 ).upcast< json::object >(); BOOST_CHECK_EQUAL( update.at( json::string( "key" ) ), json::parse_single_quoted( "{ 'a':'1' ,'b':'1' }" )); BOOST_CHECK_EQUAL( update.at( json::string( "data" ) ), json::number( 42 ) ); }
// durring tests, there was a situation, where after a browser refresh, the server did not respond to a subscription BOOST_FIXTURE_TEST_CASE( second_subscription_to_invalid_node_must_be_communicated, context ) { data_.add_configuration( pubsub::node_group(), pubsub::configurator().authorization_not_required() ); answer_validation_request( node1, false ); // create a subscriber, subscribe to node1 const boost::shared_ptr< pubsub::test::subscriber > first_subscriber( new pubsub::test::subscriber ); data_.subscribe( first_subscriber, node1 ); tools::run( *this ); // make sure, that the subscriber is subscribed BOOST_REQUIRE( first_subscriber->on_invalid_node_subscription_called( node1 ) ); const json::array response = json_multiple_post( asio_mocks::read_plan() << asio_mocks::json_msg( "{ 'cmd': [ { 'subscribe': { 'a':'1' ,'b':'1' } } ] }" ) << asio_mocks::json_msg( "{ 'id': '192.168.210.1:9999/0' }" ) << asio_mocks::disconnect_read() ); BOOST_REQUIRE_GE( response.length(), 2u ); // in the first, or the second response, there should be an update of node1 BOOST_CHECK_NE( response.find( json::parse_single_quoted( "{ " "'id' : '192.168.210.1:9999/0'," "'resp' : [ { " " 'subscribe': {'a': '1', 'b': '1'}," " 'error' : 'invalid node' } ]" "}" ) ), -1 ); }
Json JsonSerializer::Serialize(Colliders &colliders) { Json::array items; for (auto &collider : colliders) items.push_back(Serialize(collider)); return Json(items); }
BOOST_FIXTURE_TEST_CASE( defered_response_to_subscription_if_validation_was_asynchronous, context ) { const json::array responses = json_multiple_post( asio_mocks::read_plan() << asio_mocks::json_msg( "{ 'cmd': [ { 'subscribe': { 'a':'1' ,'b':'1' } } ] }" ) << boost::bind( update_node1_to_42, boost::ref( static_cast< pubsub::test::adapter&>( *this ) ), node1 ) << asio_mocks::json_msg( "{ 'id': '192.168.210.1:9999/0' }" ) << asio_mocks::disconnect_read() ); BOOST_REQUIRE_EQUAL( responses.length(), 2u ); BOOST_CHECK( find_update( responses.at( 1u ).upcast< json::object >(), "{ 'a':'1' ,'b':'1' }", "42" ) ); }
json::object response_base::build_response( const json::string& session_id, const json::array& response, const json::array& updates ) const { json::object result; result.add( id_token, session_id ); if ( !response.empty() ) result.add( response_token, response ); if ( !updates.empty() ) result.add( update_token, updates ); return result; }
std::vector< boost::asio::const_buffer > response_base< Timer >::build_response( const json::array& bayeux_response ) { static const char response_header[] = "HTTP/1.1 200 OK\r\n" "Content-Type: application/json\r\n" "Content-Length: "; response_buffer_ = tools::as_string( bayeux_response.size() ) + "\r\n\r\n"; std::vector< boost::asio::const_buffer > result; result.push_back( boost::asio::buffer( response_header, sizeof response_header -1 ) ); result.push_back( boost::asio::buffer( response_buffer_ ) ); bayeux_response.to_json( result ); return result; }
void Scraper::scrape() { bool done = false; unsigned int page = 0; std::string responseBuffer; while (!done) { responseBuffer.clear(); const char *APIBlogUrl = getAPIBlogUrlForUsername("david-meade", page++); makeRequest(APIBlogUrl, &responseBuffer); std::string error; Json resJson = Json::parse(responseBuffer, error)["response"]; Json blogJson = resJson["blog"]; Json::array postsJson = resJson["posts"].array_items(); for(unsigned int i = 0; i < postsJson.size(); i++) { Json::object postObj = postsJson[i].object_items(); } done = postsJson.size() < 20; }; }
BOOST_FIXTURE_TEST_CASE( defered_error_message_if_not_authorized, context ) { const json::array response = json_multiple_post( asio_mocks::read_plan() << asio_mocks::json_msg( "{ 'cmd': [ { 'subscribe': { 'a':'1' ,'b':'1' } } ] }" ) << asio_mocks::json_msg( "{ 'id': '192.168.210.1:9999/0' }" ) << boost::bind( unauthorized_node_subject, boost::ref( static_cast< pubsub::test::adapter&>( *this ) ), node1 ) << asio_mocks::disconnect_read() ); BOOST_REQUIRE_EQUAL( response.length(), 2u ); json::array resp = response.at( 1 ).upcast< json::object >().at( json::string( "resp" ) ).upcast< json::array >(); BOOST_REQUIRE_EQUAL( resp.length(), 1u ); BOOST_CHECK_EQUAL( resp.at( 0 ), json::parse_single_quoted( "{" "'error': 'not allowed'" "'subscribe' :{ 'a':'1' ,'b':'1' } }" ) ); }
BOOST_FIXTURE_TEST_CASE( error_message_if_subscription_subject_is_invalid, context ) { answer_validation_request( node1, false ); const json::array response = json_multiple_post( asio_mocks::read_plan() << asio_mocks::json_msg( "{ 'cmd': [ { 'subscribe': { 'a':'1' ,'b':'1' } } ] }" ) << asio_mocks::json_msg( "{ 'id': '192.168.210.1:9999/0' }" ) << asio_mocks::disconnect_read() ); BOOST_REQUIRE_EQUAL( response.length(), 2u ); json::array resp = response.at( 1 ).upcast< json::object >().at( json::string( "resp" ) ).upcast< json::array >(); BOOST_REQUIRE_EQUAL( resp.length(), 1u ); BOOST_CHECK_EQUAL( resp.at( 0 ), json::parse_single_quoted( "{" "'error': 'invalid node'" "'subscribe' :{ 'a':'1' ,'b':'1' } }" ) ); }
void AutoNetServerImpl::SendEvent(const std::string& rawEvent, const std::vector<std::string>& args) { // Prepend '$' to custum event to avoid namespace collitions with internal events std::string event("$"); event.append(rawEvent); Json::array jsonArgs; for (const auto& a : args) { jsonArgs.push_back(a); } *this += [this, event, jsonArgs] { for(auto hdl : m_Subscribers) { Json msg = Json::object{ {"type", event}, {"args", jsonArgs} }; m_transport->Send(hdl, msg.dump()); } }; }
BOOST_FIXTURE_TEST_CASE( failed_initialization, context ) { answer_validation_request( node1, true ); answer_authorization_request( node1, true ); skip_initialization_request( node1 ); const json::array response = json_multiple_post( asio_mocks::read_plan() << asio_mocks::json_msg( "{ 'cmd': [ { 'subscribe': { 'a':'1' ,'b':'1' } } ] }" ) << asio_mocks::json_msg( "{ 'id': '192.168.210.1:9999/0' }" ) << asio_mocks::disconnect_read() ); BOOST_REQUIRE_EQUAL( response.length(), 2u ); json::array resp = response.at( 1 ).upcast< json::object >().at( json::string( "resp" ) ).upcast< json::array >(); BOOST_REQUIRE_EQUAL( resp.length(), 1u ); BOOST_CHECK_EQUAL( resp.at( 0 ), json::parse_single_quoted( "{" "'error': 'node initialization failed'" "'subscribe' :{ 'a':'1' ,'b':'1' } }" ) ); }
BOOST_FIXTURE_TEST_CASE( unsubscribe_from_not_subscribed_node_http, context ) { const json::array response = json_multiple_post( subscribe_to_node1() << asio_mocks::json_msg( "{" " 'id': '192.168.210.1:9999/0', " " 'cmd': [ " " { 'unsubscribe': { 'a': '1', 'b': '2' } } " " ]" "}" ) << asio_mocks::disconnect_read() ); BOOST_REQUIRE_EQUAL( response.length(), 2u ); BOOST_CHECK_EQUAL( response.at( 1u ), json::parse_single_quoted("" "{" " 'id': '192.168.210.1:9999/0'," " 'resp': [{" " 'unsubscribe': { 'a': '1', 'b': '2' }," " 'error': 'not subscribed'" " }]" "}" ) ); }
void AutoNetServerImpl::NewObject(CoreContext& ctxt, const CoreObjectDescriptor& object){ int contextID = ResolveContextID(&ctxt); *this += [this, object, contextID]{ Json::object objData; Json::object types; // Add object data objData["name"] = autowiring::demangle(object.type); objData["id"] = autowiring::demangle(object.value.type()); // Add slots for this object { Json::array slots; for(const SlotInformation* slot = object.stump->pHead; slot; slot = slot->pFlink) { slots.push_back(Json::object{ {"id", autowiring::demangle(slot->type)}, {"autoRequired", slot->autoRequired}, {"offset", int(slot->slotOffset)} }); } objData["slots"] = slots; } // Add type information auto member = object.pContextMember; if(member) { types["contextMember"] = true; } auto runnable = object.pCoreRunnable; if(runnable) { types["coreRunnable"] = true; } auto thread = object.pBasicThread; if(thread) { // Create slot in map m_Threads[thread->GetSelf<BasicThread>()]; types["thread"] = Json::object{ {"kernal", 0.0}, {"user", 0.0} }; } // Check if type implements an AutoFilter if (!object.subscriber.empty()) { Json::object args; for (auto pArg = object.subscriber.GetAutoFilterArguments(); *pArg; ++pArg) { args[autowiring::demangle(pArg->id)] = Json::object{ {"id", autowiring::demangle(pArg->id)}, {"isInput", pArg->is_input}, {"isOutput", pArg->is_output} }; } types["autoFilter"] = args; } auto filter = object.pFilter; if(filter) { types["exceptionFilter"] = true; } auto bolt = object.pBoltBase; if(bolt) { Json::array sigils; for(auto cur = bolt->GetContextSigils(); *cur; cur++){ sigils.push_back(autowiring::demangle(*cur)); } types["bolt"] = sigils; } BroadcastMessage("newObject", contextID, types, objData); }; }
Json RiotService::GetMatchFeedByIds::internalCall() { Json summoners = _service->_api.getSummonerByIds(_params["ids"]); Json::object diff; Json::array matches; std::string err; auto cmp = [](const Json & a, const Json & b) { return a["createDate"].number_value() > b["createDate"].number_value(); }; for (auto & kv : summoners.object_items()) { Json cache = _service->getSummonerInfoCache(kv.first); printf("cache : %s\nnew: %s", cache.dump().c_str(), kv.second.dump().c_str()); if (cache.is_null() || cache["revisionDate"].number_value() < kv.second["revisionDate"].number_value()) { diff[kv.first] = kv.second; } else { matches.insert(matches.end(), cache["games"].array_items().begin(), cache["games"].array_items().end()); } } std::sort(matches.begin(), matches.end(), cmp); if (!matches.empty()) { _onRead(matches); } for (auto & kv : diff) { auto future = std::async(std::launch::async, [this, kv, cmp, &matches]() { Json newInfo = _service->_api.getRecentGamesBySummonerId(kv.first); if (!newInfo.is_null()) { Json::array modifiedMatches; for (auto & match : newInfo["games"].array_items()) { auto item = match.object_items(); item["summonerId"] = kv.first; item["name"] = kv.second["name"].string_value(); modifiedMatches.push_back(item); } matches.insert(matches.end(), modifiedMatches.begin(), modifiedMatches.end()); sort(matches.begin(), matches.end(), cmp); Json::object updateObject(kv.second.object_items()); updateObject["games"] = modifiedMatches; _service->saveSummonerInfo(kv.first, Json(updateObject)); } _onRead(matches); }); } return Json(matches); }
LOAD_PARAM(request, req, db, int_value, 0); LOAD_PARAM(request, req, raw, bool_value, true); /* LOAD_PARAM(request, req, url, string_value, ""); LOAD_PARAM(request, req, content, string_value, ""); */ LOAD_PARAM(request, req, type, string_value, ""); LOAD_PARAM(request, req, K, int_value, -1); LOAD_PARAM(request, req, R, number_value, NAN); LOAD_PARAM(request, req, hint_K, int_value, -1); LOAD_PARAM(request, req, hint_R, number_value, NAN); string params_l1; LOAD_PARAM1(request, params_l1, params_l1, string_value, ""); req.params_l1.decode(params_l1); Json::array results; auto it = request.object_items().find("urls"); for (auto const &v: it->second.array_items()) { req.url = v.string_value(); SearchResponse resp; server->search(req, &resp); Json::array hits; for (auto const &hit: resp.hits) { hits.push_back(Json::object{ {"key", hit.key}, {"meta", hit.meta}, {"details", hit.details}, {"score", hit.score}}); } results.push_back(Json::object{ {"time", resp.time},