I’m trying to use webmockr to mock some HTTP responses for testing a package of mine that wraps an API (using httr), but I’m having difficulty creating stubbed requests whose content behaves the same way as the actual API responses. Here is a reprex:
library("webmockr")
library("httr")
library("magrittr")
## Some fake data
dat_json <- '{"odata.metadata":["https://someurlhere.com"],"odata.count":["110"],"value":[{"SomeData":{"whatever":{"species":["Mouse"]}},"Comments":{}}]}'
httr_mock(on = TRUE) # turn on mocking
## Stub request
stub <- stub_request('get', uri = 'https://httpbin.org/get') %>%
to_return(
body = dat_json,
headers = list('Content-Type' = 'application/json; charset=utf-8')
)
(res <- GET("https://httpbin.org/get"))
#> Response [https://httpbin.org/get]
#> Date: NULL
#> Status: 200
#> Content-Type: application/json; charset=utf-8
#> Size: 140 B
## Get parsed content. IRL, this is one line within the function I am trying to
## test. On real data it works fine but with the stubbed data it throws an
## error:
content(res, "parsed")
#> Error: No automatic parser available for application/octet-stream.
httr_mock(on = FALSE) # turn off mocking
When I actually query the API, calling content()
on the response gives data that looks like this:
## Expected result:
(dat <- list(
odata.metadata = "https://someurlhere.com",
odata.count = "110",
value = list(
list(
SomeData = list(whatever = list(species = "Mouse")),
Comments = NULL
)
)
))
#> $odata.metadata
#> [1] "https://someurlhere.com"
#>
#> $odata.count
#> [1] "110"
#>
#> $value
#> $value[[1]]
#> $value[[1]]$SomeData
#> $value[[1]]$SomeData$whatever
#> $value[[1]]$SomeData$whatever$species
#> [1] "Mouse"
#>
#>
#>
#> $value[[1]]$Comments
#> NULL
Created on 2019-02-01 by the reprex package (v0.2.1)
Am I doing something wrong here?