jejacks0n / teaspoon

Teaspoon: Javascript test runner for Rails. Use Selenium, BrowserStack, or PhantomJS.
1.43k stars 243 forks source link

Allow binary fixtures #400

Closed timothee-alby closed 9 years ago

timothee-alby commented 9 years ago

Fixtures works well for template files, but don't allow binary files (or any other kind of non-template files).

A typical use case is when testing a front-end app relying on binary files downloaded over the network (e.g. a PDF downloaded by pdfjs)

It appear that the issue originate from commit 1931adc300892b9266986f58e837c8803d8e2e2b, which is forcing the fixtures to be valid, UTF8-encoded templates.

I can submit a PR for a fix, but I wanted to open the discussion first. I'm thinking about either doing some file type recognition to render the correct type of fixture, or creating a new fixture endpoint designed for binary files. Of course, we'll have to keep issue #264 in mind, making sure it won't create a new vulnerability.

semanticart commented 9 years ago

I'm not sure if "fixture" is the right word here, but I also could make use of a way to access static binary files from within teaspoon. I have some javascript that needs to reference some image files and I don't currently have a good place to put those files.

Can you recommend a solution or workaround?

jejacks0n commented 9 years ago

I'd say you can put them in any place that you are serving assets from -- remembering that you can add any paths to your assets path, including those that are within specs (obviously not in prod environments though). Typically you would need to load those files differently than a fixture would load (as in, it doesn't make sense to put them in the DOM or try to parse them, so go ahead and implement what you need in your project. I'd say a simple ajax request should give you a binary file, or in the case of images, putting the url to it as the source of an image tag that's in a fixture. In general though, that's all that makes sense, and adopting more code for a semi-rare use case probably wouldn't be useful.

So, to summarize, if you want it as a "fixture", put an image or anchor tag with a reference to the file you want to load as a src, or href attribute, and then load them within your test, however you think you should.

The one outlier to this is the file input, but since you're within the confines of javascript, you can't do anything here anyway.