Haskell: Testing the Web API

I am wondering what methods (if any) are used to test Haskell applications that execute queries over the network? Coming from the land of Ruby, I was looking for anything that could either drown out or "simulate" network calls in order to test Haskell functions. I am particularly interested in the β€œVCR” (for example: https://github.com/myronmarston/vcr ), since it seems to be the most viable option, IMHO.

So, I want to be able to record network requests / response pairs once, and then reuse these records for subsequent tests. I came up with my own simplified mix that runs the web server (warp) before the tests, serving the pre-written answers, but I have to point all the URLs in the application to "localhost". I suppose it is not always possible to replace all the URLs in an application. Although I am quite pleased with my own setup described above (and would like to make a special testing tool / framework "plug-in" out of it), but I would not reinvent the wheel.

+6
source share
1 answer

Check out Control.Proxy.Tutorial . If you can write a proxy wrapper around your type, then you can easily replace the test interface and the real interface as follows:

client <-< simulatedServer client <-< realServer 

Edit: To answer your question in a comment, you use Server to write a wrapper around your simpleHTTP requests:

 realServer :: HStream ty => Request ty -> Server (Request ty) (Result (Response ty)) IO r realServer = foreverK $ \req -> do result <- lift $ simpleHTTP req respond result 

The simulated server will look like this:

 simulatedServer :: (Monad m, HStream ty) => Request ty -> Server (Request ty) (Result (Response ty)) mr simulatedServer = foreverK $ \req -> do result <- lift $ simulatedRequest req respond result 

And your client will look like this:

 client :: (Monad m, HStream ty) => () -> Client (Request ty) (Result (Response ty)) mr client () = forever $ do let req = <something> result <- request req lift $ doSomethingWith result 

Then you can test the real server and the fake server using:

 -- Test the real server main = runSession $ client <-< realServer -- Test the fake server main = runSession $ client <-< simulatedServer 

client and simulatedServer are polymorphic in the base monad just because I don’t know which base monad they will use for your testing. The only requirement is that the two things you compose have the same base monad or that at least one is polymorphic in the base monad.

+3
source

Source: https://habr.com/ru/post/925415/


All Articles