r0man / ring-cors

Ring middleware for Cross-Origin Resource Sharing.
http://github.com/r0man/ring-cors
169 stars 44 forks source link

ring-cors breaks 404 routes when origin header is specified #10

Open chenfisher opened 9 years ago

chenfisher commented 9 years ago

Hi,

When a request contains the 'origin' header - ring-cores sets a response map even if the original response was nil (when route not found) thus making the server return 200 status code instead of 404.

I believe it's the same issue as #5 , which I see was not merged in and is now obsolete to due code change.

Thanks

r0man commented 9 years ago

Issue #5 should be covered by this I think: https://github.com/r0man/ring-cors/blob/master/test/ring/middleware/cors_test.clj#L127 If this is not working as expected a patch with test is welcome!

chenfisher commented 9 years ago

@r0man,

I think you have a problem with your test. You're missing: :access-control-allow-methods [:get] so the test passes while it shouldn't pass.

Test should probably be:

(deftest test-no-cors-header-when-handler-returns-nil
  (is (nil? ((wrap-cors (fn [_] nil)
                        :access-control-allow-origin #".*example.com"
                        :access-control-allow-methods [:get])
             {:request-method
              :get :uri "/"
              :headers {"origin" "http://example.com"}}))))
r0man commented 9 years ago

@chenfisher I just pushed ring-cors "0.1.7-SNAPSHOT" to Clojars. Can you confirm this fixes your problem?

ioRekz commented 9 years ago

I'm not sure this is directly related to that issue but I have the following error

java.lang.NullPointerException: Ring handler returned nil

when I add an origin header to a curl request. I'm not sure if I'm doing something wrong or immutant or the cors handler A bit more stack trace

java.lang.NullPointerException: Ring handler returned nil
    at immutant.web.internal.undertow$create_http_handler$reify__4809.handleRequest(undertow.clj:133) ~[na:na]
    at org.projectodd.wunderboss.web.async.websocket.UndertowWebsocket$2.handleRequest(UndertowWebsocket.java:96) ~[wunderboss-web-1.x.incremental.229.jar:na]