How can i mock a multiple page response?

未關閉
#1,760 4 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
3/5
預估耗時
1-2 天
新手友好度
25/100
Issue 類型
文件
描述清晰度
基本清楚
活躍度
停滯
技術堆疊
java, kotlin
領域
api, testing

研究方向

從 MockHttpTransport.Builder 和連結的 SubscriptionsFetcherTest.kt 第 214-220 行開始,接著追蹤 mockResponse 如何在重複請求中被使用。完成標準是記錄並測試一種受支援的方式來模擬兩個分頁的 YouTube 回應,其中包括觀察到的第二次請求失敗。

由索引模型根據 Issue 內容生成。

描述

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.

主要語言
Java
星號
1.4k
分支
476
平均合併
10 小時 47 分鐘
30 天內合併 PR
3

環境準備

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

googleapis/google-http-java-client 的其他 Issue

查看 googleapis/google-http-java-client 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。