# Insert Rack Middleware during Tests

Posted on October 24, 2014 by Carsten Zimmermann

We recently deployed a wee internal Rails Engine that logs raw API data in order to save the original payload that was pushed over the wire. That little piece of software, however, had a bug (which is pretty uncommon for software, right?): The logged payload was always empty.

The Engine’s host application uses Rails’ ActionDispatch::XmlParamsParser middleware. It would seem that it messed up the contents of rack.input which in turn holds the POST data.

rack.input is an IO-like object and it needs to be rewound after it has been read from. XmlParamsParser reads from it, but apparently doesn’t rewind it. It seams easy enough to fix, but how to testdrive it?

I didn’t want to statically include a rack.input-modifying middleware into the Engine’s dummy app. Instead I wanted add another context that tested against a version of the dummy infected with a mock middleware and have all other tests run without it.

Here’s how I did it. Enjoy!