mardi 3 avril 2018

How do I construct HTTP requests in R for testing?

I've written a small API in R, using plumber. (See https://www.rplumber.io/ ) I would like to write a test that can be run against the source code, to verify I get the expected response. I would like the test to pass in a HTTP-request object to the API's endpoint. The usual library for constructing requests seems to be httr. (See https://cran.r-project.org/web/packages/httr/vignettes/quickstart.html ), but the request_build function I'd like to use isn't exposed at the outermost scope.

Is there a way to access the internal function of a package? library(httr) doesn't give me access to its internal request_build)

Alternately, I'm looking at the library httptest, but I'm having trouble getting it to generate a request object. (See https://cran.r-project.org/web/packages/httptest/vignettes/httptest.html ) I see that it's supposed to record requests for later use, but that's also giving me trouble:

with_mock_api({ 
    a <- POST("http://127.0.0.1:9999/endpoint", body=list(raw="This is some sample text."), config=list("content-type"="application/x-www-form-urlencoded"))
    print(a)
})


Error: POST http://127.0.0.1:9999/endpoint list(raw = "This is some sample text.") (127.0.0.1-9999/endpoint-93904a-POST.json)

I'm not sure what the problem is here; Postman ( https://www.getpostman.com/ ) confirms the API is up and functioning.

Also, is it possible to record multiple requests and specify which I want during test?

Aucun commentaire:

Enregistrer un commentaire