test_custom_success (Test *test,
                     gconstpointer data)
{
  GAsyncResult *result = NULL;
  CockpitWebService *service;
  JsonObject *response;
  CockpitCreds *creds;
  GError *error = NULL;
  GHashTable *headers;
  JsonObject *login_data;
  const SuccessFixture *fix = data;
  const gchar *path = fix->path ? fix->path : "/cockpit";
  const gchar *password = fix->password ? fix->password : "this is the password";
  const gchar *application = fix->application ? fix->application : "cockpit";

  if (fix->warning)
    cockpit_expect_warning (fix->warning);

  headers = web_socket_util_new_headers ();
  g_hash_table_insert (headers, g_strdup ("Authorization"), g_strdup (fix->header));
  if (fix->authorized)
    g_hash_table_insert (headers, g_strdup ("X-Authorize"), g_strdup ("password"));
  cockpit_auth_login_async (test->auth, path, 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 ();
  response = cockpit_auth_login_finish (test->auth, result, NULL, headers, &error);
  g_object_unref (result);
  g_assert_no_error (error);
  g_assert (response != NULL);
  json_object_unref (response);

  mock_auth_include_cookie_as_if_client (headers, headers,
                               fix->cookie_name ? fix->cookie_name : "cockpit");
  service = cockpit_auth_check_cookie (test->auth, path, headers);
  creds = cockpit_web_service_get_creds (service);
  g_assert_cmpstr (application, ==, cockpit_creds_get_application (creds));
  if (fix->authorized && g_str_has_prefix (fix->header, "Basic"))
    {
      if (password == NULL)
        g_assert_null (cockpit_creds_get_password (creds));
      else
        g_assert_cmpstr (g_bytes_get_data (cockpit_creds_get_password (creds), NULL), ==, password);
    }
  else
    {
      g_assert_null (cockpit_creds_get_password (creds));
    }

  login_data = cockpit_creds_get_login_data (creds);
  if (fix->data)
    g_assert_cmpstr (json_object_get_string_member (login_data, "login"),
                     ==, fix->data);
  else
    g_assert_null (login_data);

  g_hash_table_destroy (headers);
  g_object_unref (service);
}
test_custom_success (Test *test,
                     gconstpointer data)
{
  GAsyncResult *result = NULL;
  CockpitWebService *service;
  JsonObject *response;
  CockpitCreds *creds;
  GError *error = NULL;
  GHashTable *headers;
  JsonObject *login_data;
  const SuccessFixture *fix = data;
  const gchar *path = fix->path ? fix->path : "/cockpit";
  const gchar *password = fix->password ? fix->password : "this is the password";
  const gchar *application = fix->application ? fix->application : "cockpit";

  if (fix->warning)
    cockpit_expect_warning (fix->warning);

  headers = web_socket_util_new_headers ();
  g_hash_table_insert (headers, g_strdup ("Authorization"), g_strdup (fix->header));
  if (fix->authorized)
    g_hash_table_insert (headers, g_strdup ("X-Authorize"), g_strdup ("password"));
  cockpit_auth_login_async (test->auth, path, 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 ();
  response = cockpit_auth_login_finish (test->auth, result, NULL, headers, &error);
  g_object_unref (result);
  g_assert_no_error (error);
  g_assert (response != NULL);
  json_object_unref (response);

  mock_auth_include_cookie_as_if_client (headers, headers,
                               fix->cookie_name ? fix->cookie_name : "cockpit");
  service = cockpit_auth_check_cookie (test->auth, path, headers);
  creds = cockpit_web_service_get_creds (service);
  g_assert_cmpstr (application, ==, cockpit_creds_get_application (creds));
  if (fix->authorized && g_str_has_prefix (fix->header, "Basic"))
    {
      if (password == NULL)
        g_assert_null (cockpit_creds_get_password (creds));
      else
        g_assert_cmpstr (g_bytes_get_data (cockpit_creds_get_password (creds), NULL), ==, password);
    }
  else
    {
      g_assert_null (cockpit_creds_get_password (creds));
    }

  login_data = cockpit_creds_get_login_data (creds);
  if (fix->data)
    g_assert_cmpstr (json_object_get_string_member (login_data, "login"),
                     ==, fix->data);
  else
    g_assert_null (login_data);

  g_hash_table_destroy (headers);
  g_object_unref (service);
}