An attempt by the European commission to challenge Britain’s right to deny some EU migrants access to child benefit and child tax credits is expected to be thrown out by European judges.

The ruling from the European court of justice is expected to confirm that Britain can insist that only EU citizens with a lawful “right of residence” in the UK can claim social security benefits.

The European commission claimed that the decision by Britain to impose a right of residence test to access certain family benefits amounted to direct discrimination against citizens of other EU member states.

The ruling is expected to the first of several legal challenges to Britain’s attempt to curb what David Cameron has called “benefit tourism” and restrict the impact of the EU’s freedom of movement rules on the UK.

The Polish government and others are expected to challenge Cameron’s emergency brake mechanism, under which new EU migrants will be denied access to in-work welfare benefits and tax credits during their first four years in the UK.

The right to residence test is based on an EU directive that says a citizen of another EU state has the right to live in Britain for more than three months only if they are in work or self-employed, or have sufficient resources and comprehensive health insurance so that they are not a burden on the social security system.

The European commission argued to Europe’s most senior court that the simple fact of habitual residence in another EU state should be enough to qualify for family welfare support. They argued that otherwise some EU citizens could be left without any family support despite the fact they live in the EU and have dependent children.

But in a preliminary opinion, the court’s advocate-general, Pedro Cruz Villalon, said last October that the commission’s challenge should be dismissed. He said that although the right to residence test could be regarded as indirect discrimination, it could be justified by the need to protect Britain’s public finances.

Marley Morris, of the Institute of Public Policy Research, said he expected the court to back Britain’s position. “Barring a major upset, the European court of justice’s decision [on Tuesday] is likely to back the UK’s right to reside test. This is another sign – on top of other recent [European court of justice] judgments – that it is becoming more sympathetic to the UK’s interpretation of free movement rules,” he said.

“But there are no guarantees that this will last forever, and future judgments may go against the UK. Conversely, a vote for Brexit on 23 June is likely to create its own legal quagmire, as the subsequent negotiations will have to resolve the free movement rights of EU citizens currently living in the UK and UK citizens currently living in other EU countries in a fair and consistent way.”