TEST: `ConnectAndGetToken` Returns Error on Non-200 Response

已關閉 適合新手
#554 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 3 天內回覆

@aniket866 已經在處理了。

開始於 2026年9月28日。

  • #555 來自 @aniket866 —— 未關閉

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
84/100
Issue 類型
功能
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
go
領域
testing

研究方向

從 keycloak_client.go 中的 ConnectAndGetToken 開始,接著使用一個回傳 401 及文件所述 JSON body 的 httptest.Server。驗證回傳的錯誤不為 nil、包含 401 狀態,並具有 KindAPI 分類。測試能可靠地涵蓋 non-200 回應路徑,且整個 Keycloak client package 都保護了這個關鍵情況,即表示完成。

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

描述

Reason/Context
Description

keycloak_client.go ConnectAndGetToken has no tests at all. The entire
Keycloak client package is untested. The most critical path to test is when
Keycloak returns a non-200 status (wrong credentials, realm not found, server
error) — the function should return a KindAPI error with the HTTP status code
and body. Currently nothing verifies this.

Why Necessary

Wrong credentials is the single most common failure a new user encounters. If
this error path regresses — e.g. the function panics, returns a nil error, or
swallows the body — users get no feedback and assume the server is down.

What If Not Present

A change to the response-parsing code (e.g. switching JSON library, restructuring
the token response) could silently break authentication for all users and no test
would catch it before release.

Diagram
sequenceDiagram
    participant T  as Test
    participant KC as keycloakClient
    participant TS as httptest.Server

    T->>TS: register handler → always return 401 {"error":"unauthorized"}
    T->>KC: NewKeycloakClient(server.URL, "id", "secret")
    T->>KC: ConnectAndGetToken()

    KC->>TS: POST /protocol/openid-connect/token
    TS-->>KC: 401 {"error":"unauthorized"}

    KC-->>T: error (KindAPI)

    T->>T: assert error != nil 
    T->>T: assert error contains "401" 
    T->>T: assert KindOf(err) == KindAPI 

Description

N/A

Implementation ideas

No response

主要語言
Go
星號
59
分支
75
平均合併
7 天 12 小時
30 天內合併 PR
20

環境準備

  • 沒有 Dockerfile 或 Docker Compose 檔案
  • 沒有 Pull Request 範本
  • 閱讀貢獻指南

從這裡開始

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

microcks/microcks-cli 的其他 Issue

查看 microcks/microcks-cli 的全部 Issue

相似的 Issue

更多 Go Issue

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

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