Recently I was looking at a flight from Boston to Los Angeles, and one particular flight suggested was interesting - connecting through Toronto (airlines were Air Canada and WestJet). Due to the hassle of crossing an international border I am not going to be booking this flight, but I am curious about how customs/immigration work in this case? Would I need to clear customs when landing in Toronto, then reclear US immigration at Toronto?
Yep, you'd have to clear two sets of customs and immigration in Toronto. I'd initially assumed they'd have some program to let connecting passengers from the US directly into the post-preclearance terminal area, but https://www.torontopearson.com/connecting.aspx suggests that's not an option (possibly because of the airline/terminal change).
This is a form of international air travel called "cabotage" which is not allowed under most bilateral air transportation treaties, including the agreement between the US and Canada. You are not allowed to transit Canada on a domestic US itinerary unless the stop is longer than 4 hours, at which point your Canadian connection is defined as a stopover, as opposed to a layover. Neither the Westjet nor Air Canada websites should sell you a ticket with a shorter layover, and if you piece together an itinerary like that on your own you will likely be denied boarding at the airport.
That being said, if you were to book this ticket and it did meet the four hour minimum requirement, yes you would have to clear customs twice at YYZ, once to enter Canada and once to reenter the US.
Here are a few resources: