Suppose light ray passing through a medium with refractive index $n=n(y)$. In the case of an inhomogeneous medium in which $n$ varies continuously in the $y$-direction, We have curved rays that satisfy Snell's law in the form:
$$n\cos\psi=\mathrm{constant}$$ where the angle $\psi$ is the slope of the tangent to the path.
Fermat's principle states that
The actual path taken by a light ray between two fixed points makes the travel time of the ray stationary.
So that $$T[\mathcal{P}]=c^{-1}\int_{\mathcal{P}}nds$$ which reduce to (in present case) $$T[y]=c^{-1}\int_{x_0}^{x_1}dx \ n(1+\dot{y}^2)^{1/2}$$ with the help of Euler-Lagrange's Equation $$\frac{n}{(1+\dot{y}^2)^{1/2}}=\mathrm{constant}$$
And on writing $\dot{y}=\tan\psi$, this gives snell's law.
Question: If I put $\dot{y}=0 \Rightarrow y=$ constant that is not extremals and therefore not rays. But since such a ray would experience a constant value of $n$, How does the ray know that it must bend?