Upgrading ring & issue with unit tests?

34 views
Skip to first unread message

Reynald Borer

unread,
Jun 10, 2016, 9:43:01 AM6/10/16
to Ring
Hello everyone,

I'm working on a project with an old ring version (1.1.8) that I would like to try to upgrade to a more recent version (1.5.0). I updated the dependencies, and when I test my application with a browser, it works fine.

But, when I try to run unit tests, some of them now fail. Specifically, the ones that are passing parameters as x-www-form-urlencoded are not present within :params anymore. While at the same time, the same endpoints works fine through a real browser (I run `lein ring server-headless` to spin a test server).

I played a bit with versions of ring-core dependency, and was able to figure out that something between versions 1.2.2 and 1.3.0 changed and made those unit tests fail. With 1.2.2 they run fine, and as soon as I switch to 1.3.0 (that's the only change), some unit tests fails.

Does that ring a bell to someone?

I admit I'm kinda lost here. I checked the changelog (https://github.com/ring-clojure/ring/blob/master/HISTORY.md#130-2014-06-02), I've already ensured that none of my wrappers uses content-type, :content-length and :character-encoding keys from the request.

Any advices on how I could try to figure out what's going on?

Thanks in advance,
Reynald

James Reeves

unread,
Jun 10, 2016, 9:57:04 AM6/10/16
to Ring
How do your unit tests work?

In Ring 1.2.2, whether a form is urlencoded is checked like:

  (defn urlencoded-form? [request]
    (if-let [^String type (:content-type request)]
      (.startsWith type "application/x-www-form-urlencoded")))

In Ring 1.3.0, because :content-type has been deprecated, the function has changed to:

  (defn content-type [request]
    (if-let [type (get-in request [:headers "content-type"])]
      (second (re-find #"^(.*?)(?:;|$)" type))))

  (defn urlencoded-form? [request]
    (if-let [^String type (content-type request)]
      (.startsWith type "application/x-www-form-urlencoded")))

The Ring adapter sets both headers and the :content-type key, but my guess is that your unit tests take a shortcut and only set the :content-type key. If you change your unit tests to set the "content-type" header instead, they should work.

- James

--
You received this message because you are subscribed to the Google Groups "Ring" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ring-clojure...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reynald Borer

unread,
Jun 10, 2016, 10:17:37 AM6/10/16
to ring-c...@googlegroups.com
I'm using ring-mock 0.3.0 to generate requests.

For example:

(request/request :put (str "/~api/papers/" 123 "/feature") {:featured "true"
                                                            :origin "paper.li:main"})


Does generate the following request:

{:remote-addr "localhost",
 :headers {"host" "localhost", "content-type" "application/x-www-form-urlencoded", "content-length" "36"},
 :server-port 80,
 :content-length 36,
 :content-type "application/x-www-form-urlencoded",
 :uri "/~api/papers/123/feature",
 :server-name "localhost",
 :query-string nil,
 :body #object[java.io.ByteArrayInputStream 0x4f68b7ae "java.io.ByteArrayInputStream@4f68b7ae"],
 :scheme :http,
 :request-method :put}

I'm using the same wrappers in unit tests than when running `lein ring server-headless`. I'm using the site-defaults provided in ring-defaults.

Cheers,
Reynald

Reynald Borer

unread,
Jun 10, 2016, 10:45:00 AM6/10/16
to ring-c...@googlegroups.com
Oh!

I've finally figured out the issue, all on my side.

Within the test code, there is a function that inject a fake authentication cookie. The function was written in such a way that all previous headers (generated by ring-mock) were removed, only :cookies remained! So this prevented ring to use the body as a form submission and nothing was parsed.

Thanks a lot for your help James, you forced me to look at the proper location :-)

Cheers,
Reynald

James Reeves

unread,
Jun 10, 2016, 3:58:19 PM6/10/16
to Ring
Glad I could be of (indirect) help!

- James
Reply all
Reply to author
Forward
0 new messages