package api
import (
"net/http"
"strings"
"testing"
)
func TestGetLabel_LabelEscaping(t *testing.T) {
var gotURL string
client := newAuthTestClient(func(req *http.Request) (*http.Response, error) {
gotURL = req.URL.String()
return authTestResponse(http.StatusOK, `{"name":"risk/high","color":"red","description":"High risk"}`), nil
})
client.SetToken("test-token", "test")
_, err := GetLabel(client, "owner", "repo", "risk/high")
if err != nil {
t.Fatalf("GetLabel() error = %v", err)
}
expectedEscaped := "risk%2Fhigh"
if !strings.Contains(gotURL, expectedEscaped) {
t.Errorf("Expected escaped label '%s' in URL, got %s", expectedEscaped, gotURL)
}
}
func TestUpdateLabel_LabelEscaping(t *testing.T) {
var gotURL string
client := newAuthTestClient(func(req *http.Request) (*http.Response, error) {
gotURL = req.URL.String()
return authTestResponse(http.StatusOK, `{"name":"bug/critical","color":"red"}`), nil
})
client.SetToken("test-token", "test")
opts := &UpdateLabelOptions{Color: "blue"}
_, err := UpdateLabel(client, "owner", "repo", "bug/critical", opts)
if err != nil {
t.Fatalf("UpdateLabel() error = %v", err)
}
expectedEscaped := "bug%2Fcritical"
if !strings.Contains(gotURL, expectedEscaped) {
t.Errorf("Expected escaped label '%s' in URL, got %s", expectedEscaped, gotURL)
}
}
func TestDeleteLabel_LabelEscaping(t *testing.T) {
var gotURL string
client := newAuthTestClient(func(req *http.Request) (*http.Response, error) {
gotURL = req.URL.String()
return authTestResponse(http.StatusNoContent, ""), nil
})
client.SetToken("test-token", "test")
err := DeleteLabel(client, "owner", "repo", "status/verified")
if err != nil {
t.Fatalf("DeleteLabel() error = %v", err)
}
expectedEscaped := "status%2Fverified"
if !strings.Contains(gotURL, expectedEscaped) {
t.Errorf("Expected escaped label '%s' in URL, got %s", expectedEscaped, gotURL)
}
}
func TestRemoveLabelFromIssue_LabelEscaping(t *testing.T) {
var gotURL string
client := newAuthTestClient(func(req *http.Request) (*http.Response, error) {
gotURL = req.URL.String()
return authTestResponse(http.StatusNoContent, ""), nil
})
client.SetToken("test-token", "test")
err := RemoveLabelFromIssue(client, "owner", "repo", 123, "type/bug")
if err != nil {
t.Fatalf("RemoveLabelFromIssue() error = %v", err)
}
expectedEscaped := "type%2Fbug"
if !strings.Contains(gotURL, expectedEscaped) {
t.Errorf("Expected escaped label '%s' in URL, got %s", expectedEscaped, gotURL)
}
}
func TestGetLabel_NormalLabel(t *testing.T) {
var gotPath string
client := newAuthTestClient(func(req *http.Request) (*http.Response, error) {
gotPath = req.URL.Path
return authTestResponse(http.StatusOK, `{"name":"bug","color":"red"}`), nil
})
client.SetToken("test-token", "test")
_, err := GetLabel(client, "owner", "repo", "bug")
if err != nil {
t.Fatalf("GetLabel() error = %v", err)
}
expectedPath := "/api/v5/repos/owner/repo/labels/bug"
if gotPath != expectedPath {
t.Errorf("Expected path '%s', got %s", expectedPath, gotPath)
}
}
func TestUpdateLabel_NormalLabel(t *testing.T) {
var gotPath string
client := newAuthTestClient(func(req *http.Request) (*http.Response, error) {
gotPath = req.URL.Path
return authTestResponse(http.StatusOK, `{"name":"bug","color":"blue"}`), nil
})
client.SetToken("test-token", "test")
opts := &UpdateLabelOptions{Color: "blue"}
_, err := UpdateLabel(client, "owner", "repo", "bug", opts)
if err != nil {
t.Fatalf("UpdateLabel() error = %v", err)
}
expectedPath := "/api/v5/repos/owner/repo/labels/bug"
if gotPath != expectedPath {
t.Errorf("Expected path '%s', got %s", expectedPath, gotPath)
}
}
func TestRemoveLabelFromIssue_NormalLabel(t *testing.T) {
var gotPath string
client := newAuthTestClient(func(req *http.Request) (*http.Response, error) {
gotPath = req.URL.Path
return authTestResponse(http.StatusNoContent, ""), nil
})
client.SetToken("test-token", "test")
err := RemoveLabelFromIssue(client, "owner", "repo", 123, "bug")
if err != nil {
t.Fatalf("RemoveLabelFromIssue() error = %v", err)
}
expectedPath := "/api/v5/repos/owner/repo/issues/123/labels/bug"
if gotPath != expectedPath {
t.Errorf("Expected path '%s', got %s", expectedPath, gotPath)
}
}
func TestDeleteLabel_NormalLabel(t *testing.T) {
var gotPath string
client := newAuthTestClient(func(req *http.Request) (*http.Response, error) {
gotPath = req.URL.Path
return authTestResponse(http.StatusNoContent, ""), nil
})
client.SetToken("test-token", "test")
err := DeleteLabel(client, "owner", "repo", "feature")
if err != nil {
t.Fatalf("DeleteLabel() error = %v", err)
}
expectedPath := "/api/v5/repos/owner/repo/labels/feature"
if gotPath != expectedPath {
t.Errorf("Expected path '%s', got %s", expectedPath, gotPath)
}
}
func TestCreateLabel_SlashLabel(t *testing.T) {
var gotURL string
client := newAuthTestClient(func(req *http.Request) (*http.Response, error) {
gotURL = req.URL.String()
return authTestResponse(http.StatusOK, `{"name":"risk/high","color":"red"}`), nil
})
client.SetToken("test-token", "test")
opts := &CreateLabelOptions{Name: "risk/high", Color: "red"}
_, err := CreateLabel(client, "owner", "repo", opts)
if err != nil {
t.Fatalf("CreateLabel() error = %v", err)
}
expectedEscaped := "name=risk%2Fhigh"
if !strings.Contains(gotURL, expectedEscaped) {
t.Errorf("Expected escaped query param '%s' in URL, got %s", expectedEscaped, gotURL)
}
}