A person arrested on a warrant may not be delivered over to the agent who the executive authority demanding the person has appointed to receive the person unless the person is first taken immediately before a judge or magistrate of this state, who shall inform the person of the demand made for the person's surrender, and of the crime with which the person is charged, and that the person has the right to demand and procure legal counsel. If the prisoner or the prisoner's counsel states a desire to test the legality of the arrest, the judge or magistrate shall fix a reasonable time to be allowed the prisoner within which to apply for a writ of habeas corpus. When that writ is applied for, notice of the application and of the time and place of hearing on it shall be given to the prosecuting attorney of the judicial district in which the arrest is made and in which the accused is in custody, and to the agent of the demanding state.