How can i mock a multiple page response?

Open
#1,760 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
25/100
Issue type
Documentation
Clarity
Mostly clear
Activity status
Stale
Tech stack
java, kotlin
Domain
api, testing

Research direction

Start with MockHttpTransport.Builder and the linked SubscriptionsFetcherTest.kt lines 214-220, then trace how mockResponse is consumed across repeated requests. Done means documenting and testing a supported way to simulate the two paginated YouTube responses, including the observed second-request failure.

Written by the indexing model from the issue text.

Description

type: feature request

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.

Dominant language
Java
Stars
1.4k
Forks
476
Avg merge
10h 47m
Merged PRs (30d)
3

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from googleapis/google-http-java-client

All issues in googleapis/google-http-java-client

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.