# IOError: closed stream on Ruby/Rails Test

**URL:** <https://discuss.circleci.com/t/ioerror-closed-stream-on-ruby-rails-test/27836>\
**Category:** Running Tests\
**Tags:** rails\
**Created:** [January 21, 2019, 8:59pm UTC](https://discuss.circleci.com/t/ioerror-closed-stream-on-ruby-rails-test/27836 "2019-01-21T20:59:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![floydj](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/floydj/32/18556_2.png) [@floydj](https://discuss.circleci.com/u/floydj)\
**Post date:** [January 21, 2019, 8:59pm UTC](https://discuss.circleci.com/t/ioerror-closed-stream-on-ruby-rails-test/27836/1 "2019-01-21T20:59:48Z")

</div>

Trying to read a PDF in an integration test. This was working fine using the circleci/ruby:2.4.1-node image, but upon upgrading to ruby 2.6.0 and the circleci/ruby:2.6.0-node-browsers image, I get the following error:

`IOError: closed stream`

This points to a method in my test\_helper file:

```
def read_pdf_from_response(response)
  temp_pdf = Tempfile.new('pdf') << response.body.force_encoding('UTF-8')
  reader = PDF::Reader.new(temp_pdf)
  reader.pages.map(&:text).join('') # this is line that throws the closed stream error
end

```

Any ideas here on how to troubleshoot? This all works fine locally, just not on circleci. And it worked fine before I upgraded to ruby 2.6.

Thanks

---

<div class="post-metadata">

**Author:** ![floydj](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/floydj/32/18556_2.png) [@floydj](https://discuss.circleci.com/u/floydj)\
**Post date:** [April 10, 2019, 4:14pm UTC](https://discuss.circleci.com/t/ioerror-closed-stream-on-ruby-rails-test/27836/2 "2019-04-10T16:14:51Z")

</div>

Here’s how I fixed it. I’m assuming this can only be because of the difference in how macOS and Ubuntu handle temp files. Anyway, this works:

```
def read_pdf_from_response(response)
  tfile = Rails.root.join 'tmp', 'testpdf.pdf'
  File.write tfile, response.body.force_encoding('UTF-8')

  begin
    reader = PDF::Reader.new(tfile)
    reader.pages.map(&:text).join('').squeeze("\n")
  ensure
    File.delete(tfile)
  end
end
```

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/circleci/original/3X/5/1/511608acda8e85906e15909b57bd0a46c1dbe291.png) [@system](https://discuss.circleci.com/u/system)\
**Post date:** [April 20, 2019, 4:14pm UTC](https://discuss.circleci.com/t/ioerror-closed-stream-on-ruby-rails-test/27836/3 "2019-04-20T16:14:55Z")

</div>

Closed on April 20, 2019, 4:14pm UTC

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
