diff --git a/dev/articles/how-to.html b/dev/articles/how-to.html index 1bcbbe7..584a829 100644 --- a/dev/articles/how-to.html +++ b/dev/articles/how-to.html @@ -131,7 +131,7 @@
http <- webfakes::local_app_process(webfakes::httpbin_app(), start = TRUE)
http$local_env(list(GITHUB_API = "{url}"))
Sys.getenv("GITHUB_API")
-#> [1] "http://127.0.0.1:40897/"
+#> [1] "http://127.0.0.1:46449/"
http$stop()
Sys.getenv("GITHUB_API")
#> [1] ""
@@ -154,14 +154,14 @@
web <- webfakes::new_app_process(time)
web$url()
-#> [1] "http://127.0.0.1:41519/"
+#> [1] "http://127.0.0.1:42131/"
Use web$url()
to query the URL of the app. For
example:
web$stop()
stops the app and the subprocess as well:
web$stop()
@@ -345,8 +345,8 @@ How do I test a sequence of reques
#> Request failed [401]. Retrying in 1 seconds...
#> Request failed [401]. Retrying in 1 seconds...
#> Request failed [401]. Retrying in 3.7 seconds...
-#> Response [http://127.0.0.1:44631/unstable]
-#> Date: 2025-01-13 21:26
+#> Response [http://127.0.0.1:46793/unstable]
+#> Date: 2025-01-14 09:13
#> Status: 200
#> Content-Type: application/json
#> Size: 17 B
web <- webfakes::local_app_process(
webfakes::httpbin_app(),
- opts = webfakes::server_opts(num_threads = 3)
+ opts = webfakes::server_opts(num_threads = 6, enable_keep_alive = TRUE)
)
-test_that("", {
- url <- web$url("/delay/1")
+testthat::test_that("parallel requests", {
+ url <- web$url("/delay/0.5")
p <- curl::new_pool()
- handles <- replicate(3, curl::new_handle(url = url, http_version = 2))
+ handles <- replicate(3, curl::new_handle(url = url))
resps <- list()
for (handle in handles) {
curl::multi_add(
handle,
- done = function(x) message("one is done"),
+ done = function(x) resps <<- c(resps, list(x)),
fail = stop,
pool = p
)
}
- st <- system.time(curl::multi_run(timeout = 5, pool = p))
- print(st)
- expect_true(st[["elapsed"]] < 3.0)
+ st <- system.time(curl::multi_run(timeout = 3, pool = p))
+ testthat::expect_true(st[["elapsed"]] < 1.5)
})
-#> one is done
-#> one is done
-#> one is done
-#> user system elapsed
-#> 0.002 0.001 1.039
#> Test passed π
(The http_version = 2
option is currently needed because
-of a bug (https://github.com/r-lib/webfakes/issues/108).)
(If this should fail for you and webfakes appears to process the +requests sequentially, see issue #108 for +possible workarounds.)
throttle
gives the number of bytes per second, so
downloading 200 random bytes from the fake app will take about 2
seconds.
web <- webfakes::new_app_process(time)
web$url()
-#> [1] "http://127.0.0.1:38937/"
+#> [1] "http://127.0.0.1:43371/"
Use web$url()
to query the URL of the app. For
example:
web$stop()
stops the app and the subprocess as well:
web$stop()
diff --git a/dev/articles/oauth.html b/dev/articles/oauth.html
index 25a2502..5e2a7af 100644
--- a/dev/articles/oauth.html
+++ b/dev/articles/oauth.html
@@ -118,9 +118,9 @@ The OAuth2.0 resource an
#> auto_start:
#> TRUE
#> process id:
-#> 9317
+#> 9266
#> http url:
-#> http://127.0.0.1:46005/
+#> http://127.0.0.1:36859/
#> fields and methods:
#> get_app() # get the app object
#> get_port() # query (first) port of the app
@@ -156,9 +156,9 @@ OAuth2.0 app creation & registra
#> auto_start:
#> TRUE
#> process id:
-#> 9330
+#> 9279
#> http url:
-#> http://127.0.0.1:37423/
+#> http://127.0.0.1:33549/
#> fields and methods:
#> get_app() # get the app object
#> get_port() # query (first) port of the app
@@ -183,8 +183,8 @@ OAuth2.0 app creation & registra
)
reg_resp <- httr::GET(url)
reg_resp
-#> Response [http://127.0.0.1:46005/register?name=3P%20app&redirect_uri=http://127.0.0.1:37423/login/redirect]
-#> Date: 2025-01-13 21:26
+#> Response [http://127.0.0.1:36859/register?name=3P%20app&redirect_uri=http://127.0.0.1:33549/login/redirect]
+#> Date: 2025-01-14 09:14
#> Status: 200
#> Content-Type: application/json
#> Size: 184 B
@@ -197,17 +197,17 @@ OAuth2.0 app creation & registra
#>
#> $client_id
#> $client_id[[1]]
-#> [1] "id-95d943f6c6dd352a1cb06ad05f3ac5"
+#> [1] "id-71bc087ed76a8feb1c761d5743da97"
#>
#>
#> $client_secret
#> $client_secret[[1]]
-#> [1] "secret-6c96fbab12fc439eea5a0dd05206ef"
+#> [1] "secret-0c69b132f31e1e2aec6af2a7df5c07"
#>
#>
#> $redirect_uri
#> $redirect_uri[[1]]
-#> [1] "http://127.0.0.1:37423/login/redirect"
The resource app replies with the client id and the client secret. Weβll use them to authenticate the third party app. In real life they are included in the config of the third party app by its admin. Our @@ -226,8 +226,8 @@
resp_data <- httr::GET(tpapp2$url("/data"))
resp_data
-#> Response [http://127.0.0.1:34853/data]
-#> Date: 2025-01-13 21:26
+#> Response [http://127.0.0.1:43333/data]
+#> Date: 2025-01-14 09:14
#> Status: 200
#> Content-Type: application/json
#> Size: 24 B
@@ -399,8 +399,8 @@ OAuth2.0 app creation & regist
)
reg_resp3 <- httr::GET(url3)
reg_resp3
-#> Response [http://127.0.0.1:46005/register?name=3P%20app2&redirect_uri=http://localhost:1410/]
-#> Date: 2025-01-13 21:26
+#> Response [http://127.0.0.1:36859/register?name=3P%20app2&redirect_uri=http://localhost:1410/]
+#> Date: 2025-01-14 09:14
#> Status: 200
#> Content-Type: application/json
#> Size: 170 B
@@ -413,12 +413,12 @@ OAuth2.0 app creation & regist
#>
#> $client_id
#> $client_id[[1]]
-#> [1] "id-cad660ce51543ffee2d7e4d3639af8"
+#> [1] "id-eefc6f17f2775997497365921db914"
#>
#>
#> $client_secret
#> $client_secret[[1]]
-#> [1] "secret-927663c2f2cdec693f52b8caaa192c"
+#> [1] "secret-8632991fdd4c1adc9430558682ca2a"
#>
#>
#> $redirect_uri
@@ -449,18 +449,18 @@ OAuth2.0 app creation & regist
token
#> <Token>
#> <oauth_endpoint>
-#> authorize: http://127.0.0.1:46005/authorize
-#> access: http://127.0.0.1:46005/token
+#> authorize: http://127.0.0.1:36859/authorize
+#> access: http://127.0.0.1:36859/token
#> <oauth_app> 3P app2
-#> key: id-cad660ce51543ffee2d7e4d3639af8
+#> key: id-eefc6f17f2775997497365921db914
#> secret: <hidden>
#> <credentials> access_token, expiry, refresh_token
#> ---
Without the token, the query to the resource server fails:
httr::GET(rsapp$url("/data"))
-#> Response [http://127.0.0.1:46005/data]
-#> Date: 2025-01-13 21:26
+#> Response [http://127.0.0.1:36859/data]
+#> Date: 2025-01-14 09:14
#> Status: 401
#> Content-Type: text/plain
#> Size: 20 B
http <- webfakes::local_app_process(webfakes::httpbin_app(), start = TRUE)
http$local_env(list(GITHUB_API = "{url}"))
Sys.getenv("GITHUB_API")
-#> [1] "http://127.0.0.1:59481/"
+#> [1] "http://127.0.0.1:64362/"
http$stop()
Sys.getenv("GITHUB_API")
#> [1] ""
web <- webfakes::new_app_process(time)
web$url()
-#> [1] "http://127.0.0.1:59482/"
Use web$url()
to query the URL of the app. For example:
web$stop()
stops the app and the subprocess as well:
web$stop()
web$get_state()
@@ -258,11 +258,11 @@ How do I test a sequence of reque
pr <- webfakes::new_app_process(flaky)
url <- pr$url("/unstable")
httr::RETRY("GET", url, times = 4)
-#> Request failed [401]. Retrying in 1.6 seconds...
-#> Request failed [401]. Retrying in 1.8 seconds...
-#> Request failed [401]. Retrying in 3.4 seconds...
-#> Response [http://127.0.0.1:59492/unstable]
-#> Date: 2025-01-12 02:08
+#> Request failed [401]. Retrying in 1 seconds...
+#> Request failed [401]. Retrying in 1 seconds...
+#> Request failed [401]. Retrying in 2.1 seconds...
+#> Response [http://127.0.0.1:64374/unstable]
+#> Date: 2025-01-14 10:07
#> Status: 200
#> Content-Type: application/json
#> Size: 17 B
@@ -307,8 +307,8 @@ How do I test a sequence of reque
)
}
post_package("vcr")
-#> Response [http://127.0.0.1:59498/packages?name=vcr]
-#> Date: 2025-01-12 02:08
+#> Response [http://127.0.0.1:64380/packages?name=vcr]
+#> Date: 2025-01-14 10:07
#> Status: 200
#> Content-Type: application/json
#> Size: 18 B
@@ -321,8 +321,8 @@ How do I test a sequence of reque
#> [1] "vcr"
post_package("httptest")
-#> Response [http://127.0.0.1:59498/packages?name=httptest]
-#> Date: 2025-01-12 02:08
+#> Response [http://127.0.0.1:64380/packages?name=httptest]
+#> Date: 2025-01-14 10:07
#> Status: 200
#> Content-Type: application/json
#> Size: 29 B
@@ -536,33 +536,27 @@ Can I test asynchron
Each request takes 1 second to answer, but if the web server has more than three threads, together they'll still take about 1 second.
web <- webfakes::local_app_process(
webfakes::httpbin_app(),
- opts = webfakes::server_opts(num_threads = 3)
+ opts = webfakes::server_opts(num_threads = 6, enable_keep_alive = TRUE)
)
-test_that("", {
- url <- web$url("/delay/1")
+testthat::test_that("parallel requests", {
+ url <- web$url("/delay/0.5")
p <- curl::new_pool()
- handles <- replicate(3, curl::new_handle(url = url, http_version = 2))
+ handles <- replicate(3, curl::new_handle(url = url))
resps <- list()
for (handle in handles) {
curl::multi_add(
handle,
- done = function(x) message("one is done"),
+ done = function(x) resps <<- c(resps, list(x)),
fail = stop,
pool = p
)
}
- st <- system.time(curl::multi_run(timeout = 5, pool = p))
- print(st)
- expect_true(st[["elapsed"]] < 3.0)
+ st <- system.time(curl::multi_run(timeout = 3, pool = p))
+ testthat::expect_true(st[["elapsed"]] < 1.5)
})
-#> one is done
-#> one is done
-#> one is done
-#> user system elapsed
-#> 0.002 0.001 1.094
#> Test passed
-(The http_version = 2
option is currently needed because of a bug
-(https://github.com/r-lib/webfakes/issues/108).)
+(If this should fail for you and webfakes appears to process the
+requests sequentially, see issue #108 for possible workarounds.)
@@ -598,9 +592,9 @@ How do I simulate a slow
resp <- curl::curl_fetch_memory(slow$url("/bytes/200"))
resp$times
#> redirect namelookup connect pretransfer starttransfer
-#> 0.000000 0.000097 0.000315 0.000339 0.004731
+#> 0.000000 0.000087 0.000266 0.000284 0.004878
#> total
-#> 2.013523
+#> 2.013756
throttle
gives the number of bytes per second, so downloading 200 random bytes from the fake app will take about 2 seconds.
web <- webfakes::new_app_process(time)
web$url()
-#> [1] "http://127.0.0.1:59478/"
Use web$url()
to query the URL of the app. For example:
web$stop()
stops the app and the subprocess as well: