req icon indicating copy to clipboard operation
req copied to clipboard

"Request" local files

Open waiting-for-dev opened this issue 7 years ago • 7 comments
trafficstars

Hi there. First of all, thanks for this great work.

I'm currently developing an application which needs to request some user provided URLs. My issue is related with testing it.

Testing has a lot to do with trusting, and I trust that req works fine because it has its own test suite and it is being used by the community. For this reason, I don't like and I don't see the reason why my tests should be non-deterministic because of the need to reach external end-points which could be not available at some point.

For my own confidence, it would be enough to be able to provide in my test suite a location for a local file which content would act as a response body.

Of course, I see some inconvenient with this. Maybe it is pushing the abstraction out of its scope, because, which method should that request have and which headers should the response have?

Maybe another module Nework.HTTP.Req.Testing could be added implementing the needed logic, but surely some changes in the core system would be necessary. Maybe adding another Scheme constructor called Local, or maybe to make req function not depend on Url but on something implementing a new Url type class.

How do you see it? Am I missing a better approach?

Thanks

waiting-for-dev avatar Jul 30 '18 14:07 waiting-for-dev

I think I found the solution.

waiting-for-dev avatar Jul 31 '18 17:07 waiting-for-dev

Note that the current version of the library works a bit differently. You still should be able to use req', see how pure-tests is written.

mrkkrp avatar Jul 31 '18 17:07 mrkkrp

I see. The problem is that what I'm testing is a function that internally uses req and not req'. You can think of this function as a function taking an URL as parameter, performing the request with req and returning a handcrafted version of the response within an IO.

So I think I come back to my first thought... Considering the option to support the file scheme in req.

waiting-for-dev avatar Aug 01 '18 10:08 waiting-for-dev

Does http-client support file scheme?

mrkkrp avatar Aug 01 '18 13:08 mrkkrp

No, as far as I know, it doesn't :disappointed:

waiting-for-dev avatar Aug 01 '18 14:08 waiting-for-dev

The most logical way to add this is to add the feature to http-client and then add a new scheme to Req (Req is built on top of http-client).

mrkkrp avatar Aug 03 '18 05:08 mrkkrp

I agree this should be the way to go. I submitted an issue there.

waiting-for-dev avatar Aug 03 '18 07:08 waiting-for-dev