-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract parsing function from oauth_flow_fetch() (#309)
And use better error management strategy in preparation for #283.
- Loading branch information
Showing
6 changed files
with
109 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,34 @@ | ||
# errors if response isn't json | ||
# turns oauth errors to R errors | ||
|
||
Code | ||
oauth_flow_fetch(req) | ||
oauth_flow_fetch(req, "test") | ||
Condition | ||
Error in `oauth_flow_fetch()`: | ||
! Failed to process response from "token" endpoint. | ||
Error: | ||
! OAuth failure [1] | ||
* abc | ||
|
||
# forwards turns oauth errors to R errors | ||
# userful errors if response isn't parseable | ||
|
||
Code | ||
oauth_flow_fetch(req) | ||
oauth_flow_parse(resp1, "test") | ||
Condition | ||
Error in `oauth_flow_fetch()`: | ||
! OAuth failure [1] | ||
* abc | ||
Error: | ||
! Failed to parse response from `test` OAuth url. | ||
Caused by error in `resp_body_json()`: | ||
! Unexpected content type "text/plain". | ||
* Expecting type "application/json" or suffix "json". | ||
Code | ||
oauth_flow_parse(resp2, "test") | ||
Condition | ||
Error: | ||
! Failed to parse response from `test` OAuth url. | ||
* Did not contain `access_token`, `device_code`, or `error` field. | ||
|
||
# returns body if known good structure | ||
|
||
Code | ||
oauth_flow_parse(resp, "test") | ||
Condition | ||
Error: | ||
! OAuth failure [10] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,40 @@ | ||
|
||
# oauth_flow_fetch -------------------------------------------------------- | ||
|
||
test_that("errors if response isn't json", { | ||
test_that("turns oauth errors to R errors", { | ||
req <- request("http://example.com") | ||
local_mocked_bindings(req_perform = function(...) { | ||
response(200L, headers = list(`content-type` = "text/plain")) | ||
response_json(400L, body = list(error = "1", error_description = "abc")) | ||
}) | ||
|
||
expect_snapshot(oauth_flow_fetch(req), error = TRUE) | ||
expect_snapshot(oauth_flow_fetch(req, "test"), error = TRUE) | ||
}) | ||
|
||
test_that("forwards turns oauth errors to R errors", { | ||
req <- request("http://example.com") | ||
body <- list(error = "1", error_description = "abc") | ||
local_mocked_bindings(req_perform = function(...) { | ||
response_json(200L, body = body) | ||
}) | ||
# oauth_flow_parse -------------------------------------------------------- | ||
|
||
test_that("userful errors if response isn't parseable", { | ||
resp1 <- response(headers = list(`content-type` = "text/plain")) | ||
resp2 <- response_json(body = list()) | ||
|
||
expect_snapshot(oauth_flow_fetch(req), error = TRUE) | ||
expect_snapshot(error = TRUE, { | ||
oauth_flow_parse(resp1, "test") | ||
oauth_flow_parse(resp2, "test") | ||
}) | ||
}) | ||
|
||
test_that("returns body if known good structure", { | ||
resp <- response_json(body = list(access_token = "10")) | ||
expect_equal(oauth_flow_parse(resp, "test"), list(access_token = "10")) | ||
|
||
test_that("returns body if successful", { | ||
req <- request("http://example.com") | ||
local_mocked_bindings(req_perform = function(...) { | ||
response_json(200L, body = list(access_token = "10", expires_in = "20")) | ||
}) | ||
resp <- response_json(body = list(device_code = "10")) | ||
expect_equal(oauth_flow_parse(resp, "test"), list(device_code = "10")) | ||
|
||
expect_equal( | ||
oauth_flow_fetch(req), | ||
list(access_token = "10", expires_in = 20) | ||
) | ||
resp <- response_json(403L, body = list(error = "10")) | ||
expect_snapshot(oauth_flow_parse(resp, "test"), error = TRUE) | ||
}) | ||
|
||
test_that("converts expires_in to numeric", { | ||
resp <- response_json(200L, body = list(access_token = "10", expires_in = "20")) | ||
body <- oauth_flow_parse(resp, "test") | ||
expect_equal(body$expires_in, 20) | ||
}) | ||
|