WASHINGTON (AP) — Congress passed a one-week bill late Friday night to avert a partial shutdown of the Homeland Security Department, as leaders in both political parties quelled a revolt by House conservatives furious that the measure left President Barack Obama's immigration policy intact.

The final vote of a long day and night was a bipartisan 357-60 in the House of Representatives, a little more than an hour after the Senate cleared the measure without so much as a roll call. That sent the legislation to the White House for Obama's signature, and capped a day of bruising political battles and rhetoric to match.

Spending for the department has been held hostage in a proxy battle over Obama's recent executive actions sparing millions of immigrants in this country illegally from deportation. Republicans won full control of Congress in November's midterm elections.

"You have made a mess," House Democratic Leader Nancy Pelosi said at one point to Republicans, as recriminations filled the House chamber and the midnight deadline neared for a partial shutdown of an agency with major anti-terrorism responsibilities.

Even some Republicans readily agreed.

"There are terrorist attacks all over world and we're talking about closing down Homeland Security. This is like living in world of crazy people," tweeted Rep. Peter King of New York, a former chairman of the Homeland Security Committee.

Hours after conservatives joined with Democrats to vote down a three-week funding measure, 224-203, the Senate presented a one-week alternative to keep open the agency, which has responsibility for border control as well as anti-terrorist measures.

That amounted to a take-it-or-leave it offer less than three hours before the deadline.

Some Republican opponents — members of a "Freedom Caucus" — sat together in the chamber as the vote total mounted in the legislation's favor.

This time, Pelosi urged her rank-and-file to support the short-term measure, saying it would lead to passage next week of a bill to fund the agency through the Sept. 30 end of the budget year without immigration add-ons. Aides to Speaker John Boehner promptly said there had been no such promise made.

Taken together, the day's roller-coaster events at the Capitol underscored the difficulty Republicans have had so far this year in translating last fall's election gains into legislative accomplishment — a step its own leaders say is necessary to establish the party's credentials as a responsible, governing party. Republicans gained control of the Senate in November's balloting, and emerged with their largest House majority in more than 70 years.

Further demonstrating Republican woes, House Republican leaders abruptly called off a vote on a major education bill that had attracted significant opposition from conservatives as well as Democrats and the White House. Aides attributed that decision to the need to work separately on rounding up enough votes to pass the funding measure for Homeland Security.

For their part, tea party conservatives in the House unflinchingly defended their actions.

"It does not make any difference whether the funding is for three weeks, three months or a full fiscal year. If it's illegal, it's illegal," said Republican Rep. Mo Brooks.

He referred to a pair of immigration directives issued by Obama. The first, in 2012, lifted the threat of deportation from many immigrants brought to the country illegally as youngsters. Another order last fall applied to millions more who are in the United States unlawfully.

The unexpected House defeat of a three-week spending bill was accomplished by 52 conservatives upset by the deletion of the immigration provisions, alongside solid opposition from Democrats who wanted the agency funded through Sept. 30.

That set an unpredictable chain of events in motion. Homeland Security officials circulated a lengthy contingency plan indicating that about 30,000 employees could expect to be furloughed without passage of funding legislation.

Then the White House announced Obama had spoken with Pelosi and Senate Democratic Leader Harry Reid. Moments later, Majority Leader Mitch McConnell of Kentucky strode onto the Senate floor and swiftly gained approval for the seven-day measure.

The Senate had waited all day to play its part in the funding of the agency.

___

Associated Press writers Charles Babington, Andrew Taylor, Matthew Daly and Laurie Kellman contributed to this report.