Testing the Copernican principle by constraining spatial homogeneity