Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_download fails with custom url #357

Open
AdroMine opened this issue Sep 29, 2023 · 0 comments
Open

get_download fails with custom url #357

AdroMine opened this issue Sep 29, 2023 · 0 comments

Comments

@AdroMine
Copy link

AdroMine commented Sep 29, 2023

I have a app that can take query parameters to set up some inputs. I am using the approach highlighted in #219 (comment) to test the app with the custom url. However, the get_download function fails and instead of downloading the correct file, downloads an html file.

Here's a small reprex:

library(shiny)
library(shinytest2)

# sample test app
ui <- fluidPage(
    downloadButton("downloadData", "Download")
)

server <- function(input, output) {
    # Our dataset
    data <- mtcars
    
    output$downloadData <- downloadHandler(
        filename = function() {
            paste("data-", Sys.Date(), ".csv", sep="")
        },
        content = function(file) {
            write.csv(data, file)
        }
    )
}

my_app <- shinyApp(ui, server)

# Test with base URL
test_app1 <- AppDriver$new(my_app)
test_app1$get_download('downloadData', 'temp_file.csv')
#> ./temp_file.csv

# reads correctly
head(read.csv("temp_file.csv"))
#>                   X  mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> 1         Mazda RX4 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> 2     Mazda RX4 Wag 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> 3        Datsun 710 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#> 4    Hornet 4 Drive 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#> 5 Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
#> 6           Valiant 18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

# Test with custom URL
current_url <- test_app1$get_url()
test_app2 <- AppDriver$new(paste0(current_url, "?foo=bar"))
test_app2$get_download("downloadData", "temp_file.csv")
#> ./temp_file.csv

# HTML file gets downloaded
readLines("temp_file.csv", 5) 
#> [1] "<!DOCTYPE html>"                                                           
#> [2] "<html>"                                                                    
#> [3] "<head>"                                                                    
#> [4] "  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>"
#> [5] "  <script type=\"application/shiny-singletons\"></script>"

Created on 2023-09-29 with reprex v2.0.2

I believe the issue seems to be within app_download function when it generates the full_url

full_url <- paste0(private$shiny_url$get(), sub_url)

In my case this becomes http://127.0.0.1:4895/?foo=barsession/d7b1669e84114236fc18752d3fd5c547/download/downloadData?w= . If I modify this to remove the query part http://127.0.0.1:4895/session/d7b1669e84114236fc18752d3fd5c547/download/downloadData?w=, then the download works as expected.

As a temporary fix, I am opening the app with the custom URL so that the app can use the query parameters while loading and before downloading, set the url manually to remove the query part.

test_app2$.__enclos_env__$private$shiny_url$set(base_url)

But this is not desirable. Is there a better way until there's a fix?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant