899 lines
30 KiB
Go
899 lines
30 KiB
Go
// Package gitlab provides client and data types for the GitLab REST API.
|
|
package gitlab
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// TestNewClient verifies the constructor creates a properly configured client.
|
|
func TestNewClient(t *testing.T) {
|
|
client := NewClient("test-token", "https://gitlab.example.com", "123")
|
|
|
|
if client.Token != "test-token" {
|
|
t.Errorf("Token = %q, want %q", client.Token, "test-token")
|
|
}
|
|
if client.BaseURL != "https://gitlab.example.com" {
|
|
t.Errorf("BaseURL = %q, want %q", client.BaseURL, "https://gitlab.example.com")
|
|
}
|
|
if client.ProjectID != "123" {
|
|
t.Errorf("ProjectID = %q, want %q", client.ProjectID, "123")
|
|
}
|
|
if client.HTTPClient == nil {
|
|
t.Error("HTTPClient is nil, want non-nil default client")
|
|
}
|
|
}
|
|
|
|
// TestClientWithHTTPClient verifies the builder pattern for custom HTTP client.
|
|
func TestClientWithHTTPClient(t *testing.T) {
|
|
customClient := &http.Client{Timeout: 60 * time.Second}
|
|
client := NewClient("token", "https://gitlab.example.com", "123").
|
|
WithHTTPClient(customClient)
|
|
|
|
if client.HTTPClient != customClient {
|
|
t.Error("HTTPClient not set to custom client")
|
|
}
|
|
// Original values preserved
|
|
if client.Token != "token" {
|
|
t.Errorf("Token = %q, want %q", client.Token, "token")
|
|
}
|
|
}
|
|
|
|
// TestBuildURL verifies URL construction for API endpoints.
|
|
func TestBuildURL(t *testing.T) {
|
|
client := NewClient("token", "https://gitlab.example.com", "123")
|
|
|
|
tests := []struct {
|
|
name string
|
|
path string
|
|
params map[string]string
|
|
wantURL string
|
|
}{
|
|
{
|
|
name: "issues endpoint",
|
|
path: "/projects/123/issues",
|
|
params: nil,
|
|
wantURL: "https://gitlab.example.com/api/v4/projects/123/issues",
|
|
},
|
|
{
|
|
name: "with query params",
|
|
path: "/projects/123/issues",
|
|
params: map[string]string{"state": "opened", "per_page": "100"},
|
|
wantURL: "https://gitlab.example.com/api/v4/projects/123/issues",
|
|
},
|
|
{
|
|
name: "issue links",
|
|
path: "/projects/123/issues/42/links",
|
|
params: nil,
|
|
wantURL: "https://gitlab.example.com/api/v4/projects/123/issues/42/links",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := client.buildURL(tt.path, tt.params)
|
|
if !strings.HasPrefix(got, tt.wantURL) {
|
|
t.Errorf("buildURL(%q) = %q, want prefix %q", tt.path, got, tt.wantURL)
|
|
}
|
|
// Verify query params are included
|
|
for k, v := range tt.params {
|
|
if !strings.Contains(got, k+"="+v) {
|
|
t.Errorf("buildURL missing param %s=%s in %q", k, v, got)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestFetchIssues_Success verifies fetching issues from GitLab API.
|
|
func TestFetchIssues_Success(t *testing.T) {
|
|
// Mock GitLab API server
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
// Verify request
|
|
if r.Method != http.MethodGet {
|
|
t.Errorf("Method = %s, want GET", r.Method)
|
|
}
|
|
if r.Header.Get("PRIVATE-TOKEN") != "test-token" {
|
|
t.Errorf("PRIVATE-TOKEN header = %q, want %q", r.Header.Get("PRIVATE-TOKEN"), "test-token")
|
|
}
|
|
if !strings.Contains(r.URL.Path, "/projects/123/issues") {
|
|
t.Errorf("URL path = %s, want to contain /projects/123/issues", r.URL.Path)
|
|
}
|
|
|
|
// Return mock response
|
|
issues := []Issue{
|
|
{ID: 1, IID: 1, Title: "First issue", State: "opened"},
|
|
{ID: 2, IID: 2, Title: "Second issue", State: "opened"},
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(w).Encode(issues)
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := NewClient("test-token", server.URL, "123")
|
|
ctx := context.Background()
|
|
|
|
issues, err := client.FetchIssues(ctx, "opened")
|
|
if err != nil {
|
|
t.Fatalf("FetchIssues() error = %v", err)
|
|
}
|
|
|
|
if len(issues) != 2 {
|
|
t.Errorf("FetchIssues() returned %d issues, want 2", len(issues))
|
|
}
|
|
if issues[0].Title != "First issue" {
|
|
t.Errorf("issues[0].Title = %q, want %q", issues[0].Title, "First issue")
|
|
}
|
|
}
|
|
|
|
// TestFetchIssues_Pagination verifies client handles paginated responses.
|
|
func TestFetchIssues_Pagination(t *testing.T) {
|
|
page := 0
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
page++
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
if page == 1 {
|
|
// First page - indicate more pages via X-Next-Page header
|
|
w.Header().Set("X-Next-Page", "2")
|
|
w.Header().Set("X-Total-Pages", "2")
|
|
issues := []Issue{{ID: 1, IID: 1, Title: "Issue 1"}}
|
|
_ = json.NewEncoder(w).Encode(issues)
|
|
} else {
|
|
// Second page - no more pages
|
|
w.Header().Set("X-Total-Pages", "2")
|
|
issues := []Issue{{ID: 2, IID: 2, Title: "Issue 2"}}
|
|
_ = json.NewEncoder(w).Encode(issues)
|
|
}
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := NewClient("token", server.URL, "123")
|
|
ctx := context.Background()
|
|
|
|
issues, err := client.FetchIssues(ctx, "all")
|
|
if err != nil {
|
|
t.Fatalf("FetchIssues() error = %v", err)
|
|
}
|
|
|
|
if len(issues) != 2 {
|
|
t.Errorf("FetchIssues() returned %d issues, want 2 (from 2 pages)", len(issues))
|
|
}
|
|
}
|
|
|
|
// TestFetchIssuesSince verifies incremental sync with updated_after param.
|
|
func TestFetchIssuesSince(t *testing.T) {
|
|
since := time.Date(2024, 1, 15, 10, 0, 0, 0, time.UTC)
|
|
var capturedURL string
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
capturedURL = r.URL.String()
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(w).Encode([]Issue{})
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := NewClient("token", server.URL, "123")
|
|
ctx := context.Background()
|
|
|
|
_, err := client.FetchIssuesSince(ctx, "all", since)
|
|
if err != nil {
|
|
t.Fatalf("FetchIssuesSince() error = %v", err)
|
|
}
|
|
|
|
// Verify updated_after param in ISO8601 format
|
|
if !strings.Contains(capturedURL, "updated_after=2024-01-15") {
|
|
t.Errorf("URL = %s, want to contain updated_after=2024-01-15", capturedURL)
|
|
}
|
|
}
|
|
|
|
// TestCreateIssue_Success verifies creating an issue via POST.
|
|
func TestCreateIssue_Success(t *testing.T) {
|
|
var capturedBody map[string]interface{}
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
t.Errorf("Method = %s, want POST", r.Method)
|
|
}
|
|
if !strings.Contains(r.URL.Path, "/projects/123/issues") {
|
|
t.Errorf("URL path = %s, want to contain /projects/123/issues", r.URL.Path)
|
|
}
|
|
|
|
// Capture request body
|
|
_ = json.NewDecoder(r.Body).Decode(&capturedBody)
|
|
|
|
// Return created issue
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusCreated)
|
|
_ = json.NewEncoder(w).Encode(Issue{
|
|
ID: 100,
|
|
IID: 42,
|
|
Title: "New issue",
|
|
State: "opened",
|
|
})
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := NewClient("token", server.URL, "123")
|
|
ctx := context.Background()
|
|
|
|
issue, err := client.CreateIssue(ctx, "New issue", "Description here", []string{"bug", "priority::high"})
|
|
if err != nil {
|
|
t.Fatalf("CreateIssue() error = %v", err)
|
|
}
|
|
|
|
if issue.IID != 42 {
|
|
t.Errorf("issue.IID = %d, want 42", issue.IID)
|
|
}
|
|
if capturedBody["title"] != "New issue" {
|
|
t.Errorf("request body title = %v, want %q", capturedBody["title"], "New issue")
|
|
}
|
|
if capturedBody["description"] != "Description here" {
|
|
t.Errorf("request body description = %v, want %q", capturedBody["description"], "Description here")
|
|
}
|
|
}
|
|
|
|
// TestUpdateIssue_Success verifies updating an issue via PUT.
|
|
func TestUpdateIssue_Success(t *testing.T) {
|
|
var capturedBody map[string]interface{}
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPut {
|
|
t.Errorf("Method = %s, want PUT", r.Method)
|
|
}
|
|
if !strings.Contains(r.URL.Path, "/projects/123/issues/42") {
|
|
t.Errorf("URL path = %s, want to contain /projects/123/issues/42", r.URL.Path)
|
|
}
|
|
|
|
_ = json.NewDecoder(r.Body).Decode(&capturedBody)
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(w).Encode(Issue{
|
|
ID: 100,
|
|
IID: 42,
|
|
Title: "Updated title",
|
|
State: "opened",
|
|
})
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := NewClient("token", server.URL, "123")
|
|
ctx := context.Background()
|
|
|
|
updates := map[string]interface{}{
|
|
"title": "Updated title",
|
|
"state_event": "close",
|
|
}
|
|
issue, err := client.UpdateIssue(ctx, 42, updates)
|
|
if err != nil {
|
|
t.Fatalf("UpdateIssue() error = %v", err)
|
|
}
|
|
|
|
if issue.Title != "Updated title" {
|
|
t.Errorf("issue.Title = %q, want %q", issue.Title, "Updated title")
|
|
}
|
|
if capturedBody["title"] != "Updated title" {
|
|
t.Errorf("request body title = %v, want %q", capturedBody["title"], "Updated title")
|
|
}
|
|
}
|
|
|
|
// TestGetIssueLinks_Success verifies fetching issue links.
|
|
func TestGetIssueLinks_Success(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if !strings.Contains(r.URL.Path, "/projects/123/issues/42/links") {
|
|
t.Errorf("URL path = %s, want to contain /projects/123/issues/42/links", r.URL.Path)
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
links := []IssueLink{
|
|
{
|
|
SourceIssue: &Issue{IID: 42, Title: "Source"},
|
|
TargetIssue: &Issue{IID: 43, Title: "Target"},
|
|
LinkType: "blocks",
|
|
},
|
|
}
|
|
_ = json.NewEncoder(w).Encode(links)
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := NewClient("token", server.URL, "123")
|
|
ctx := context.Background()
|
|
|
|
links, err := client.GetIssueLinks(ctx, 42)
|
|
if err != nil {
|
|
t.Fatalf("GetIssueLinks() error = %v", err)
|
|
}
|
|
|
|
if len(links) != 1 {
|
|
t.Errorf("GetIssueLinks() returned %d links, want 1", len(links))
|
|
}
|
|
if links[0].LinkType != "blocks" {
|
|
t.Errorf("links[0].LinkType = %q, want %q", links[0].LinkType, "blocks")
|
|
}
|
|
}
|
|
|
|
// TestRateLimiting verifies retry behavior on 429 responses.
|
|
func TestRateLimiting(t *testing.T) {
|
|
attempts := 0
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
attempts++
|
|
if attempts == 1 {
|
|
w.WriteHeader(http.StatusTooManyRequests)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(w).Encode([]Issue{{ID: 1, IID: 1, Title: "After retry"}})
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := NewClient("token", server.URL, "123")
|
|
ctx := context.Background()
|
|
|
|
issues, err := client.FetchIssues(ctx, "all")
|
|
if err != nil {
|
|
t.Fatalf("FetchIssues() error = %v, want success after retry", err)
|
|
}
|
|
|
|
if attempts < 2 {
|
|
t.Errorf("attempts = %d, want >= 2 (retry after 429)", attempts)
|
|
}
|
|
if len(issues) != 1 {
|
|
t.Errorf("FetchIssues() returned %d issues after retry, want 1", len(issues))
|
|
}
|
|
}
|
|
|
|
// TestErrorHandling verifies error responses are properly reported.
|
|
func TestErrorHandling(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
_, _ = w.Write([]byte(`{"message": "401 Unauthorized"}`))
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := NewClient("bad-token", server.URL, "123")
|
|
ctx := context.Background()
|
|
|
|
_, err := client.FetchIssues(ctx, "all")
|
|
if err == nil {
|
|
t.Fatal("FetchIssues() error = nil, want error for 401")
|
|
}
|
|
if !strings.Contains(err.Error(), "401") {
|
|
t.Errorf("error = %v, want to contain '401'", err)
|
|
}
|
|
}
|
|
|
|
// TestProjectIDURLEncoding verifies path-based project IDs are URL-encoded.
|
|
func TestProjectIDURLEncoding(t *testing.T) {
|
|
// Test URL construction directly (HTTP server decodes paths automatically)
|
|
client := NewClient("token", "https://gitlab.example.com", "group/subgroup/project")
|
|
|
|
// Build URL and verify encoding
|
|
url := client.buildURL("/projects/"+client.projectPath()+"/issues", nil)
|
|
|
|
// URL should contain encoded slashes: group%2Fsubgroup%2Fproject
|
|
if !strings.Contains(url, "group%2Fsubgroup%2Fproject") {
|
|
t.Errorf("buildURL = %s, want to contain URL-encoded project ID 'group%%2Fsubgroup%%2Fproject'", url)
|
|
}
|
|
|
|
// Also verify it works with a mock server (server receives decoded path, but request succeeds)
|
|
var serverReceived bool
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
serverReceived = true
|
|
// Server receives decoded path - this is expected HTTP behavior
|
|
if !strings.Contains(r.URL.Path, "group/subgroup/project") {
|
|
t.Errorf("Server path = %s, want to contain decoded 'group/subgroup/project'", r.URL.Path)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(w).Encode([]Issue{})
|
|
}))
|
|
defer server.Close()
|
|
|
|
client = NewClient("token", server.URL, "group/subgroup/project")
|
|
ctx := context.Background()
|
|
|
|
_, err := client.FetchIssues(ctx, "all")
|
|
if err != nil {
|
|
t.Fatalf("FetchIssues() error = %v", err)
|
|
}
|
|
if !serverReceived {
|
|
t.Error("Server did not receive request")
|
|
}
|
|
}
|
|
|
|
// TestWithEndpoint verifies the builder pattern for custom API endpoint.
|
|
func TestWithEndpoint(t *testing.T) {
|
|
client := NewClient("token", "https://gitlab.example.com", "123").
|
|
WithEndpoint("https://custom.gitlab.com/api/v4")
|
|
|
|
if client.BaseURL != "https://custom.gitlab.com/api/v4" {
|
|
t.Errorf("BaseURL = %q, want %q", client.BaseURL, "https://custom.gitlab.com/api/v4")
|
|
}
|
|
// Original values preserved
|
|
if client.Token != "token" {
|
|
t.Errorf("Token = %q, want %q", client.Token, "token")
|
|
}
|
|
if client.ProjectID != "123" {
|
|
t.Errorf("ProjectID = %q, want %q", client.ProjectID, "123")
|
|
}
|
|
}
|
|
|
|
// TestFetchIssueByIID verifies fetching a single issue by its project-scoped IID.
|
|
func TestFetchIssueByIID(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
// Verify request path includes specific issue IID
|
|
if !strings.Contains(r.URL.Path, "/issues/42") {
|
|
t.Errorf("URL path = %s, want to contain /issues/42", r.URL.Path)
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(w).Encode(Issue{
|
|
ID: 100,
|
|
IID: 42,
|
|
Title: "Single issue",
|
|
State: "opened",
|
|
})
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := NewClient("token", server.URL, "123")
|
|
ctx := context.Background()
|
|
|
|
issue, err := client.FetchIssueByIID(ctx, 42)
|
|
if err != nil {
|
|
t.Fatalf("FetchIssueByIID() error = %v", err)
|
|
}
|
|
|
|
if issue.IID != 42 {
|
|
t.Errorf("issue.IID = %d, want 42", issue.IID)
|
|
}
|
|
if issue.Title != "Single issue" {
|
|
t.Errorf("issue.Title = %q, want %q", issue.Title, "Single issue")
|
|
}
|
|
}
|
|
|
|
// TestCreateIssueLink verifies creating a link between two issues.
|
|
func TestCreateIssueLink(t *testing.T) {
|
|
var capturedPath string
|
|
var capturedBody map[string]interface{}
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
capturedPath = r.URL.Path
|
|
if r.Method != http.MethodPost {
|
|
t.Errorf("Method = %s, want POST", r.Method)
|
|
}
|
|
|
|
_ = json.NewDecoder(r.Body).Decode(&capturedBody)
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusCreated)
|
|
_ = json.NewEncoder(w).Encode(IssueLink{
|
|
SourceIssue: &Issue{IID: 42},
|
|
TargetIssue: &Issue{IID: 43},
|
|
LinkType: "blocks",
|
|
})
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := NewClient("token", server.URL, "123")
|
|
ctx := context.Background()
|
|
|
|
link, err := client.CreateIssueLink(ctx, 42, 43, "blocks")
|
|
if err != nil {
|
|
t.Fatalf("CreateIssueLink() error = %v", err)
|
|
}
|
|
|
|
// Verify URL path
|
|
if !strings.Contains(capturedPath, "/issues/42/links") {
|
|
t.Errorf("URL path = %s, want to contain /issues/42/links", capturedPath)
|
|
}
|
|
|
|
// Verify request body
|
|
if capturedBody["target_project_id"] != "123" {
|
|
t.Errorf("target_project_id = %v, want %q", capturedBody["target_project_id"], "123")
|
|
}
|
|
if int(capturedBody["target_issue_iid"].(float64)) != 43 {
|
|
t.Errorf("target_issue_iid = %v, want 43", capturedBody["target_issue_iid"])
|
|
}
|
|
if capturedBody["link_type"] != "blocks" {
|
|
t.Errorf("link_type = %v, want %q", capturedBody["link_type"], "blocks")
|
|
}
|
|
|
|
// Verify response
|
|
if link.LinkType != "blocks" {
|
|
t.Errorf("link.LinkType = %q, want %q", link.LinkType, "blocks")
|
|
}
|
|
}
|
|
|
|
// TestUpdateIssue_Error verifies error handling for UpdateIssue.
|
|
func TestUpdateIssue_Error(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
_, _ = w.Write([]byte(`{"message": "Issue not found"}`))
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := NewClient("token", server.URL, "123")
|
|
ctx := context.Background()
|
|
|
|
_, err := client.UpdateIssue(ctx, 999, map[string]interface{}{"title": "Updated"})
|
|
if err == nil {
|
|
t.Fatal("UpdateIssue() error = nil, want error for 404")
|
|
}
|
|
if !strings.Contains(err.Error(), "failed to update issue") {
|
|
t.Errorf("error = %v, want to contain 'failed to update issue'", err)
|
|
}
|
|
}
|
|
|
|
// TestUpdateIssue_InvalidJSON verifies JSON parse error handling.
|
|
func TestUpdateIssue_InvalidJSON(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{invalid json`))
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := NewClient("token", server.URL, "123")
|
|
ctx := context.Background()
|
|
|
|
_, err := client.UpdateIssue(ctx, 42, map[string]interface{}{"title": "Updated"})
|
|
if err == nil {
|
|
t.Fatal("UpdateIssue() error = nil, want error for invalid JSON")
|
|
}
|
|
if !strings.Contains(err.Error(), "failed to parse update response") {
|
|
t.Errorf("error = %v, want to contain 'failed to parse update response'", err)
|
|
}
|
|
}
|
|
|
|
// TestGetIssueLinks_Error verifies error handling for GetIssueLinks.
|
|
func TestGetIssueLinks_Error(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusForbidden)
|
|
_, _ = w.Write([]byte(`{"message": "Access denied"}`))
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := NewClient("token", server.URL, "123")
|
|
ctx := context.Background()
|
|
|
|
_, err := client.GetIssueLinks(ctx, 42)
|
|
if err == nil {
|
|
t.Fatal("GetIssueLinks() error = nil, want error for 403")
|
|
}
|
|
if !strings.Contains(err.Error(), "failed to get issue links") {
|
|
t.Errorf("error = %v, want to contain 'failed to get issue links'", err)
|
|
}
|
|
}
|
|
|
|
// TestGetIssueLinks_InvalidJSON verifies JSON parse error handling.
|
|
func TestGetIssueLinks_InvalidJSON(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`not valid json`))
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := NewClient("token", server.URL, "123")
|
|
ctx := context.Background()
|
|
|
|
_, err := client.GetIssueLinks(ctx, 42)
|
|
if err == nil {
|
|
t.Fatal("GetIssueLinks() error = nil, want error for invalid JSON")
|
|
}
|
|
if !strings.Contains(err.Error(), "failed to parse issue links response") {
|
|
t.Errorf("error = %v, want to contain 'failed to parse issue links response'", err)
|
|
}
|
|
}
|
|
|
|
// TestFetchIssueByIID_Error verifies error handling for FetchIssueByIID.
|
|
func TestFetchIssueByIID_Error(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
_, _ = w.Write([]byte(`{"message": "Issue not found"}`))
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := NewClient("token", server.URL, "123")
|
|
ctx := context.Background()
|
|
|
|
_, err := client.FetchIssueByIID(ctx, 999)
|
|
if err == nil {
|
|
t.Fatal("FetchIssueByIID() error = nil, want error for 404")
|
|
}
|
|
if !strings.Contains(err.Error(), "failed to fetch issue 999") {
|
|
t.Errorf("error = %v, want to contain 'failed to fetch issue 999'", err)
|
|
}
|
|
}
|
|
|
|
// TestFetchIssueByIID_InvalidJSON verifies JSON parse error handling.
|
|
func TestFetchIssueByIID_InvalidJSON(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{malformed`))
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := NewClient("token", server.URL, "123")
|
|
ctx := context.Background()
|
|
|
|
_, err := client.FetchIssueByIID(ctx, 42)
|
|
if err == nil {
|
|
t.Fatal("FetchIssueByIID() error = nil, want error for invalid JSON")
|
|
}
|
|
if !strings.Contains(err.Error(), "failed to parse issue response") {
|
|
t.Errorf("error = %v, want to contain 'failed to parse issue response'", err)
|
|
}
|
|
}
|
|
|
|
// TestCreateIssueLink_Error verifies error handling for CreateIssueLink.
|
|
func TestCreateIssueLink_Error(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
_, _ = w.Write([]byte(`{"message": "Target issue not found"}`))
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := NewClient("token", server.URL, "123")
|
|
ctx := context.Background()
|
|
|
|
_, err := client.CreateIssueLink(ctx, 42, 999, "blocks")
|
|
if err == nil {
|
|
t.Fatal("CreateIssueLink() error = nil, want error for 400")
|
|
}
|
|
if !strings.Contains(err.Error(), "failed to create issue link") {
|
|
t.Errorf("error = %v, want to contain 'failed to create issue link'", err)
|
|
}
|
|
}
|
|
|
|
// TestCreateIssueLink_InvalidJSON verifies JSON parse error handling.
|
|
func TestCreateIssueLink_InvalidJSON(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusCreated)
|
|
_, _ = w.Write([]byte(`{broken json`))
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := NewClient("token", server.URL, "123")
|
|
ctx := context.Background()
|
|
|
|
_, err := client.CreateIssueLink(ctx, 42, 43, "blocks")
|
|
if err == nil {
|
|
t.Fatal("CreateIssueLink() error = nil, want error for invalid JSON")
|
|
}
|
|
if !strings.Contains(err.Error(), "failed to parse issue link response") {
|
|
t.Errorf("error = %v, want to contain 'failed to parse issue link response'", err)
|
|
}
|
|
}
|
|
|
|
// TestCreateIssue_InvalidJSON verifies JSON parse error handling.
|
|
func TestCreateIssue_InvalidJSON(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusCreated)
|
|
_, _ = w.Write([]byte(`{not valid json`))
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := NewClient("token", server.URL, "123")
|
|
ctx := context.Background()
|
|
|
|
_, err := client.CreateIssue(ctx, "Test", "Description", []string{})
|
|
if err == nil {
|
|
t.Fatal("CreateIssue() error = nil, want error for invalid JSON")
|
|
}
|
|
if !strings.Contains(err.Error(), "failed to parse create response") {
|
|
t.Errorf("error = %v, want to contain 'failed to parse create response'", err)
|
|
}
|
|
}
|
|
|
|
// TestFetchIssuesSince_Error verifies error handling for FetchIssuesSince.
|
|
func TestFetchIssuesSince_Error(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
_, _ = w.Write([]byte(`{"message": "Server error"}`))
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := NewClient("token", server.URL, "123")
|
|
ctx := context.Background()
|
|
|
|
_, err := client.FetchIssuesSince(ctx, "all", time.Now().Add(-24*time.Hour))
|
|
if err == nil {
|
|
t.Fatal("FetchIssuesSince() error = nil, want error for 500")
|
|
}
|
|
}
|
|
|
|
// TestRetryMarshalError verifies that json.Marshal errors during retry are logged
|
|
// and cause the retry to continue (not silently swallowed).
|
|
func TestRetryMarshalError(t *testing.T) {
|
|
// This test verifies that when rate-limited and retrying with a request body,
|
|
// the error from json.Marshal is handled properly (not ignored with _).
|
|
//
|
|
// The current bug: line 114 does `jsonBody, _ := json.Marshal(body)`
|
|
// which silently ignores marshal errors during retry.
|
|
//
|
|
// Note: In practice, json.Marshal rarely fails for a body that successfully
|
|
// marshaled before (only happens with channels, functions, or cycles).
|
|
// This test documents that the error IS handled, not swallowed.
|
|
|
|
attempts := 0
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
attempts++
|
|
if attempts <= 2 {
|
|
// First two attempts return rate limit
|
|
w.WriteHeader(http.StatusTooManyRequests)
|
|
return
|
|
}
|
|
// Third attempt succeeds
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(w).Encode(Issue{ID: 1, IID: 1, Title: "After retry"})
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := NewClient("token", server.URL, "123")
|
|
ctx := context.Background()
|
|
|
|
// Create issue with body - this exercises the retry path with json.Marshal
|
|
issue, err := client.CreateIssue(ctx, "Test", "Description", []string{"test"})
|
|
if err != nil {
|
|
t.Fatalf("CreateIssue() error = %v, want success after retries", err)
|
|
}
|
|
|
|
if attempts < 3 {
|
|
t.Errorf("attempts = %d, want >= 3 (initial + 2 retries)", attempts)
|
|
}
|
|
if issue.Title != "After retry" {
|
|
t.Errorf("issue.Title = %q, want %q", issue.Title, "After retry")
|
|
}
|
|
}
|
|
|
|
// TestFetchIssues_PaginationLimit verifies that FetchIssues stops after MaxPages to prevent infinite loops.
|
|
func TestFetchIssues_PaginationLimit(t *testing.T) {
|
|
requestCount := 0
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
requestCount++
|
|
w.Header().Set("Content-Type", "application/json")
|
|
// Always return X-Next-Page to simulate infinite pagination (malformed response)
|
|
w.Header().Set("X-Next-Page", "999")
|
|
_ = json.NewEncoder(w).Encode([]Issue{{ID: requestCount, IID: requestCount, Title: "Issue"}})
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := NewClient("token", server.URL, "123")
|
|
ctx := context.Background()
|
|
|
|
_, err := client.FetchIssues(ctx, "all")
|
|
|
|
// Should error due to pagination limit exceeded
|
|
if err == nil {
|
|
t.Fatal("FetchIssues() error = nil, want pagination limit error")
|
|
}
|
|
if !strings.Contains(err.Error(), "pagination limit exceeded") {
|
|
t.Errorf("error = %v, want to contain 'pagination limit exceeded'", err)
|
|
}
|
|
// Should have stopped at MaxPages (1000) rather than looping forever
|
|
if requestCount > MaxPages+1 {
|
|
t.Errorf("requestCount = %d, want <= %d (MaxPages+1)", requestCount, MaxPages+1)
|
|
}
|
|
}
|
|
|
|
// TestFetchIssuesSince_PaginationLimit verifies that FetchIssuesSince stops after MaxPages.
|
|
func TestFetchIssuesSince_PaginationLimit(t *testing.T) {
|
|
requestCount := 0
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
requestCount++
|
|
w.Header().Set("Content-Type", "application/json")
|
|
// Always return X-Next-Page to simulate infinite pagination
|
|
w.Header().Set("X-Next-Page", "999")
|
|
_ = json.NewEncoder(w).Encode([]Issue{{ID: requestCount, IID: requestCount, Title: "Issue"}})
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := NewClient("token", server.URL, "123")
|
|
ctx := context.Background()
|
|
|
|
_, err := client.FetchIssuesSince(ctx, "all", time.Now().Add(-24*time.Hour))
|
|
|
|
// Should error due to pagination limit exceeded
|
|
if err == nil {
|
|
t.Fatal("FetchIssuesSince() error = nil, want pagination limit error")
|
|
}
|
|
if !strings.Contains(err.Error(), "pagination limit exceeded") {
|
|
t.Errorf("error = %v, want to contain 'pagination limit exceeded'", err)
|
|
}
|
|
if requestCount > MaxPages+1 {
|
|
t.Errorf("requestCount = %d, want <= %d (MaxPages+1)", requestCount, MaxPages+1)
|
|
}
|
|
}
|
|
|
|
// TestFetchIssues_ContextCancellation verifies that FetchIssues respects context cancellation
|
|
// or pagination limits to prevent infinite loops.
|
|
func TestFetchIssues_ContextCancellation(t *testing.T) {
|
|
var requestCount atomic.Int64
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
count := requestCount.Add(1)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
// Always return X-Next-Page to continue pagination
|
|
w.Header().Set("X-Next-Page", "2")
|
|
_ = json.NewEncoder(w).Encode([]Issue{{ID: int(count), IID: int(count), Title: "Issue"}})
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := NewClient("token", server.URL, "123")
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
// Cancel context after a short delay
|
|
go func() {
|
|
time.Sleep(50 * time.Millisecond)
|
|
cancel()
|
|
}()
|
|
|
|
issues, err := client.FetchIssues(ctx, "all")
|
|
|
|
// Should return an error that stops the loop (context cancellation OR pagination limit)
|
|
if err == nil {
|
|
t.Fatal("FetchIssues() error = nil, want error to stop infinite loop")
|
|
}
|
|
// Accept either context cancellation or pagination limit - both are valid ways to stop the loop
|
|
isContextCanceled := err == context.Canceled || strings.Contains(err.Error(), "context canceled")
|
|
isPaginationLimit := strings.Contains(err.Error(), "pagination limit exceeded")
|
|
if !isContextCanceled && !isPaginationLimit {
|
|
t.Errorf("error = %v, want context.Canceled, 'context canceled', or 'pagination limit exceeded'", err)
|
|
}
|
|
// Verify the loop was stopped (not infinite) - requestCount should be reasonable
|
|
if requestCount.Load() > MaxPages+1 {
|
|
t.Errorf("requestCount = %d, expected loop to stop", requestCount.Load())
|
|
}
|
|
// Note: partial results may or may not be returned depending on whether cancellation
|
|
// was caught by our loop check (returns partial) or by doRequest (returns nil)
|
|
t.Logf("Loop stopped after %d requests, %d issues returned, error: %v", requestCount.Load(), len(issues), err)
|
|
}
|
|
|
|
// TestFetchIssuesSince_ContextCancellation verifies that FetchIssuesSince respects context cancellation
|
|
// or pagination limits to prevent infinite loops.
|
|
func TestFetchIssuesSince_ContextCancellation(t *testing.T) {
|
|
var requestCount atomic.Int64
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
count := requestCount.Add(1)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
// Always return X-Next-Page to continue pagination
|
|
w.Header().Set("X-Next-Page", "2")
|
|
_ = json.NewEncoder(w).Encode([]Issue{{ID: int(count), IID: int(count), Title: "Issue"}})
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := NewClient("token", server.URL, "123")
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
// Cancel context after a short delay
|
|
go func() {
|
|
time.Sleep(50 * time.Millisecond)
|
|
cancel()
|
|
}()
|
|
|
|
issues, err := client.FetchIssuesSince(ctx, "all", time.Now().Add(-24*time.Hour))
|
|
|
|
// Should return an error that stops the loop (context cancellation OR pagination limit)
|
|
if err == nil {
|
|
t.Fatal("FetchIssuesSince() error = nil, want error to stop infinite loop")
|
|
}
|
|
// Accept either context cancellation or pagination limit - both are valid ways to stop the loop
|
|
isContextCanceled := err == context.Canceled || strings.Contains(err.Error(), "context canceled")
|
|
isPaginationLimit := strings.Contains(err.Error(), "pagination limit exceeded")
|
|
if !isContextCanceled && !isPaginationLimit {
|
|
t.Errorf("error = %v, want context.Canceled, 'context canceled', or 'pagination limit exceeded'", err)
|
|
}
|
|
// Verify the loop was stopped (not infinite) - requestCount should be reasonable
|
|
if requestCount.Load() > MaxPages+1 {
|
|
t.Errorf("requestCount = %d, expected loop to stop", requestCount.Load())
|
|
}
|
|
// Note: partial results may or may not be returned depending on whether cancellation
|
|
// was caught by our loop check (returns partial) or by doRequest (returns nil)
|
|
t.Logf("Loop stopped after %d requests, %d issues returned, error: %v", requestCount.Load(), len(issues), err)
|
|
}
|