test_idle_timeout (Test *test,
                   gconstpointer data)
{
  GAsyncResult *result = NULL;
  CockpitWebService *service;
  JsonObject *login_response;
  GError *error = NULL;
  GHashTable *headers;
  gboolean flag = FALSE;
  gboolean idling = FALSE;

  /* The idle timeout is one second */
  g_assert (cockpit_ws_service_idle == 1);

  headers = mock_auth_basic_header ("me", "this is the password");
  cockpit_auth_login_async (test->auth, "/cockpit", NULL, headers, on_ready_get_result, &result);
  g_hash_table_unref (headers);

  while (result == NULL)
    g_main_context_iteration (NULL, TRUE);

  headers = web_socket_util_new_headers ();
  login_response = cockpit_auth_login_finish (test->auth, result, NULL, headers, &error);
  g_assert (login_response != NULL);
  json_object_unref (login_response);
  g_object_unref (result);
  g_assert_no_error (error);

  /* Logged in ... the webservice is idle though */
  mock_auth_include_cookie_as_if_client (headers, headers, "cockpit");
  service = cockpit_auth_check_cookie (test->auth, "/cockpit", headers);
  g_assert (service != NULL);
  g_assert (cockpit_web_service_get_idling (service));
  g_object_unref (service);

  g_assert (cockpit_ws_process_idle == 2);
  g_signal_connect (test->auth, "idling", G_CALLBACK (on_idling_set_flag), &idling);

  /* Now wait for 2 seconds, and the service should be gone */
  g_timeout_add_seconds (2, on_timeout_set_flag, &flag);
  while (!flag)
    g_main_context_iteration (NULL, TRUE);

  /* Timeout, no longer logged in */
  service = cockpit_auth_check_cookie (test->auth, "/cockpit", headers);
  g_assert (service == NULL);

  /* Now wait for 3 seconds, and the auth should have said its idling */
  flag = FALSE;
  g_timeout_add_seconds (3, on_timeout_set_flag, &flag);
  while (!flag)
    g_main_context_iteration (NULL, TRUE);

  g_assert (idling == TRUE);
  g_hash_table_destroy (headers);
}
test_idle_timeout (Test *test,
                   gconstpointer data)
{
  GAsyncResult *result = NULL;
  CockpitWebService *service;
  JsonObject *login_response;
  GError *error = NULL;
  GHashTable *headers;
  gboolean flag = FALSE;
  gboolean idling = FALSE;

   
  g_assert (cockpit_ws_service_idle == 1);

  headers = mock_auth_basic_header ("me", "this is the password");
  cockpit_auth_login_async (test->auth, "/cockpit", NULL, headers, on_ready_get_result, &result);
  g_hash_table_unref (headers);

  while (result == NULL)
    g_main_context_iteration (NULL, TRUE);

  headers = web_socket_util_new_headers ();
  login_response = cockpit_auth_login_finish (test->auth, result, NULL, headers, &error);
  g_assert (login_response != NULL);
  json_object_unref (login_response);
  g_object_unref (result);
  g_assert_no_error (error);

   
  mock_auth_include_cookie_as_if_client (headers, headers, "cockpit");
  service = cockpit_auth_check_cookie (test->auth, "/cockpit", headers);
  g_assert (service != NULL);
  g_assert (cockpit_web_service_get_idling (service));
  g_object_unref (service);

  g_assert (cockpit_ws_process_idle == 2);
  g_signal_connect (test->auth, "idling", G_CALLBACK (on_idling_set_flag), &idling);

   
  g_timeout_add_seconds (2, on_timeout_set_flag, &flag);
  while (!flag)
    g_main_context_iteration (NULL, TRUE);

   
  service = cockpit_auth_check_cookie (test->auth, "/cockpit", headers);
  g_assert (service == NULL);

   
  flag = FALSE;
  g_timeout_add_seconds (3, on_timeout_set_flag, &flag);
  while (!flag)
    g_main_context_iteration (NULL, TRUE);

  g_assert (idling == TRUE);
  g_hash_table_destroy (headers);
}