REST API makes tests failed

I am building REST API on top of a rails application. Consider the
following lines:

respond_to do |format|

respond_to do |format|
if entity.details.collect{|d| d.detail_id.to_i}.include?
params[“detail_id”].to_i
flash[“error”]=t(“error_incorrect_data”)

    format.html { redirect_to :action => "show", :id

=>params[“id”] and return}
format.json { render :json => ‘Bad Request’, :status => 400
and return}

  end

end

That is, if the request format is html(means we are being accessed
through Interface), we simply redirect he user to other action and if
the format is JSON, we simply return the status code along with a
short message. This works as expected when accessed through cURL or
any browser. But when it comes to the tests, the format.html block
does not executes and as a results some of the tests start to fail.
Even if you include the format parameter along with the request in the
tests, even then that does not work. How should I get around it?

Regards,
Mohsin