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

How can i mock a multiple page response? #1760

Open
TheBestPessimist opened this issue Oct 24, 2022 · 1 comment
Open

How can i mock a multiple page response? #1760

TheBestPessimist opened this issue Oct 24, 2022 · 1 comment

Comments

@TheBestPessimist
Copy link

TheBestPessimist commented Oct 24, 2022

Hello,

Sample repro here: https://github.com/TheBestPessimist/Youtube-Subscriptions-Mailer/blob/2bf38ec27b2841102e3e1431b6d318bb378ccd0a/src/test/kotlin/land/tbp/youtube/SubscriptionsFetcherTest.kt#L214-L220

I am trying to test my integration with the Youtube API.
For that one i'm creating a mockHttpTransport with a mockResponse (or a mockRequest, the same issue presented below arises):

        val mockHttpTransport = MockHttpTransport.Builder()
//            .setLowLevelHttpRequest(mockRequest)
            .setLowLevelHttpResponse(mockResponse)
            .build()

        val youtube = YouTube.Builder(
            mockHttpTransport,
            mockGoogleCredential.jsonFactory,
            mockGoogleCredential
        ).build()

I want to get all my subscriptions, but they are more than 50 so I get a paged response ("nextPageToken": "CAUQAA"), so i need to make a second request to execute:

        val request = youtube
            .subscriptions()
            .list(listOf("snippet", "contentDetails"))
            .setMaxResults(5000)
            .setMine(true)
            .setFields("*")
            .setPrettyPrint(true)

        val responseWhichReturnsTheCorrectSubscriptionListPage: SubscriptionListResponse = request.execute()
        println(responseWhichReturnsTheCorrectSubscriptionListPage)

  val responseWhichThrowsIAE: SubscriptionListResponse = request.setPageToken("CAUQAA").execute()
        println(responseWhichThrowsIAE)

Q1: why can't i return the same mock response multiple times? the second iteration throws java.lang.IllegalArgumentException: no JSON input found
Q2: How can i pass multiple different mockResponses to the transport, to simulate that i have to fetch multiple pages from youtube?

I have already tried searching on Stackoverflow, but i had little success: https://stackoverflow.com/search?tab=votes&q=%5bgoogle-cloud-platform%5d%20%20mock.

@TheBestPessimist
Copy link
Author

bump

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