12

Our application supports CORS configurations headers. I have configured testApp separately on two different hosts. Both the setups work independent of each other. Application on host1 is configured with CORS header Access-Control-Allow-Origin to pointing to application on host2. When I access the application pages of host2 am expecting it to show Access-Control-Allow-Origin header in response. But which is missing.

How to test to CORS headers to confirm its working properly or coded properly to support cross domain resource sharing.

bhuvi
  • 121
  • 1
  • 1
  • 4

4 Answers4

8

If your application returns the header: Access-Control-Allow-Origin then it should work. In my particular use case I set it to "*".

Otherwise testing will show an error, viewable from a browser console. It will say something like: Access to ... has been blocked by CORS policy

CORS not enabled error message from browser console - screen grab

You can test if the CORS headers are working properly using your browser. I used this one and hope this helps. You will find the instructions in it. https://github.com/cactuz/cors-tester-from-browser

RudyD
  • 497
  • 6
  • 10
  • 4
    There are extremely few use cases in which you want to set `Access-Control-Allow-Origin: *`. This will essentially disable authentication for your application, as any website can now hijack your users' sessions. – ATOMP Aug 04 '20 at 15:01
  • 4
    @ATOMP albeit the * value is not recommend, ACAO header has nothing to do with authentication and no modern website is using this header as an authentication method. – Ofer B Aug 28 '21 at 02:34
  • @OferB I think you might have missed the point. If you set Access-Control-Allow-Origin: * it becomes trivial to use XSS to hijack user sessions, thus essentially disabling authentication. – stoj May 05 '22 at 14:49
7

You could test it with cUrl from terminal.

curl -v --request OPTIONS **'localhost:3000'** --header 'Origin: **http://some.origin.here**'; --header 'Access-Control-Request-Method: GET'
Avi
  • 1,223
  • 8
  • 31
het
  • 484
  • 7
  • 11
2

You can leverage the fetch provided by the browser debugger (F12 on Chrome and Firefox, then go to console):

fetch('https://google.ca')

If you get a CORS error then that means the current site you opened your debugger with (Origin) is not included in the Access-Control-Allow-Origin header by the site you're fetching from.

Symmetry
  • 123
  • 1
  • 4
-4

You can test it with any rest client like POSTMAN Rest Client, or simply you can check it from browser console - > Network tab -> in xhr filter - check the header for the particular request. you can check request and response.