Enabling CORS for Testing Azure Functions Locally
Now this is great for running against a deployed Azure Functions app, but what if you wanted to test against the local host that comes with the Azure Functions CLI? Although the Todo backend unit test suite runs locally in the browser, if we start our local host in the usual way with
func host start, we’ll find that we can’t connect to localhost due to CORS restrictions.
I was sure there ought to be a way to enable CORS for the local host, and sure enough thanks to ayls on StackOverflow, I discovered how we can do this. You simply need to pass the
--cors flag followed by the host you want to allow (
* is usually sufficient, although you can provide a list).
What this means is that you can clone my func-todo-backend git repo, point it at a table storage account, and then you can run it locally with
func host start --cors * and try the test runner against it by visiting http://todobackend.com/specs/index.html?http://localhost:7071/api/todos.
And sure enough, you can run the test suite locally: