A $512.5 billion Pentagon appropriations bill cleared the House Wednesday evening after the leadership narrowly beat back efforts to curb the National Security Agency’s authority to collect private call records and metadata on telephone customers in the U.S.

The pivotal 217-205 vote was the first real test of political sentiment since former National Security Agency contractor Edward Snowden leaked documents that revealed the secret program....

The pattern among members fit no simple ideological or generational divide. But with the White House and NSA brass weighing in, both parties’ top leaders—who enjoy more access to intelligence matters—tended to vote in opposition to the amendment.