第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

我可以為我的測(cè)試用例編寫我自己版本的 go 的 http client.Do() 函數(shù)嗎?

我可以為我的測(cè)試用例編寫我自己版本的 go 的 http client.Do() 函數(shù)嗎?

Go
九州編程 2022-11-23 20:25:10
我有一個(gè)名為 user-service.go 的文件和相應(yīng)的測(cè)試文件,名為 user-service_test.go。當(dāng)我試圖獲得完整的代碼覆蓋率時(shí),我正在努力讓一些錯(cuò)誤條件真正發(fā)生。這是函數(shù):GetOrCreateByAccessToken()//GetOrCreateByAccessToken gets a user from the database with the given access tokenfunc (s *service) GetOrCreateByAccessToken(aT string, client *Client) (*user.User, fcerr.FCErr) {var currentUser user.OauthUserreq, err := http.NewRequest("GET", "https://openidconnect.googleapis.com/v1/userinfo?access_token="+aT, nil)if err != nil {    return nil, fcerr.NewInternalServerError("Error when setting up the network request")}response, err := client.httpClient.Do(req)if err != nil {    fmt.Println("error when getting the userinfo with the access token")    return nil, fcerr.NewInternalServerError("Error when trying to verify user identity")}defer response.Body.Close()contents, err := io.ReadAll(response.Body)if err != nil {    return nil, fcerr.NewInternalServerError("Error when trying to read response from Google about user identity")}我對(duì)測(cè)試的主要控制是我可以傳入 *Client。這是測(cè)試用例的一部分,我想讓 io.ReadAll 拋出一個(gè)錯(cuò)誤:h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {    //manually return the message google would return on an actual request    w.Write([]byte(googleAPIOKResponse))})//Call the testHTTPClient() function defined in the test file to substitute my own HandlerFunchttpClient, teardown := testHTTPClient(h)defer teardown()//Call the real NewClient() from my user-service.goclient := NewClient()//Substitute the default httpClient for the one I've just set up.client.httpClient = httpClientresultingUser, err := userService.GetOrCreateByAccessToken(nU.AccessToken, client)assert.Nil(t, resultingUser)assert.NotNil(t, err)assert.Equal(t, http.StatusInternalServerError, err.Status())有什么地方我可以編寫自己的 .Do() 方法版本,該方法會(huì)在響應(yīng)中放置一些內(nèi)容,這會(huì)導(dǎo)致 io.ReadAll 返回錯(cuò)誤?或者是否有更好的方法來僅使用我已經(jīng)使用的預(yù)烘焙響應(yīng)文本來實(shí)現(xiàn)錯(cuò)誤?
查看完整描述

1 回答

?
蕭十郎

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超13個(gè)贊

沒有一種方法可以替代 Do 方法,但是有一種方法可以實(shí)現(xiàn)您的目標(biāo)。


創(chuàng)建一個(gè)返回任意響應(yīng)主體的往返類型:


type respondWithReader struct{ body io.Reader }


func (rr respondWithReader) RoundTrip(req *http.Request) (*http.Response, error) {

    return &http.Response{

        Proto:      "HTTP/1.0",

        ProtoMajor: 1,

        Header:     make(http.Header),

        Close:      true,

        Body:       ioutil.NopCloser(rr.body),

    }, nil


}

創(chuàng)建一個(gè)失敗的 io.Reader:


var errReadFail = errors.New("blah!")


type failReader int


func (failReader) Read([]byte) (int, error) {

    return 0, errReadFail

}

將 stock 客戶端與上面的傳輸和閱讀器一起使用:


c := http.Client{Transport: respondWithReader{body: failReader(0)}}

resp, err := c.Get("http://whatever.com")

if err != nil {

    t.Error(err)

}

defer resp.Body.Close()


// ReadAll returns errReadFail

_, err = ioutil.ReadAll(resp.Body)

if err != errReadFail {

    t.Errorf("got err %v, expect %v", err, errReadFail)

}


查看完整回答
反對(duì) 回復(fù) 2022-11-23
  • 1 回答
  • 0 關(guān)注
  • 106 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)