Skip to content

Commit 9e403f5

Browse files
CopilotJoannaaKL
andcommitted
Reuse mock.EndpointPattern constants instead of hardcoded paths
- Add mock import to code_scanning_test.go and git_test.go - Replace hardcoded paths with mock.GetRepos*.Pattern references - Ensures consistency with existing test patterns and easier maintenance Co-authored-by: JoannaaKL <67866556+JoannaaKL@users.noreply.github.com>
1 parent f4f426b commit 9e403f5

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

pkg/github/code_scanning_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/github/github-mcp-server/pkg/translations"
1111
"github.com/google/go-github/v79/github"
1212
"github.com/google/jsonschema-go/jsonschema"
13+
"github.com/migueleliasweb/go-github-mock/src/mock"
1314
"github.com/stretchr/testify/assert"
1415
"github.com/stretchr/testify/require"
1516
)
@@ -50,7 +51,7 @@ func Test_GetCodeScanningAlert(t *testing.T) {
5051
{
5152
name: "successful alert fetch",
5253
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
53-
"GET /repos/{owner}/{repo}/code-scanning/alerts/{alert_number}": mockResponse(t, http.StatusOK, mockAlert),
54+
mock.GetReposCodeScanningAlertsByOwnerByRepoByAlertNumber.Method + " " + mock.GetReposCodeScanningAlertsByOwnerByRepoByAlertNumber.Pattern: mockResponse(t, http.StatusOK, mockAlert),
5455
}),
5556
requestArgs: map[string]interface{}{
5657
"owner": "owner",
@@ -63,7 +64,7 @@ func Test_GetCodeScanningAlert(t *testing.T) {
6364
{
6465
name: "alert fetch fails",
6566
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
66-
"GET /repos/{owner}/{repo}/code-scanning/alerts/{alert_number}": http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
67+
mock.GetReposCodeScanningAlertsByOwnerByRepoByAlertNumber.Method + " " + mock.GetReposCodeScanningAlertsByOwnerByRepoByAlertNumber.Pattern: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
6768
w.WriteHeader(http.StatusNotFound)
6869
_, _ = w.Write([]byte(`{"message": "Not Found"}`))
6970
}),
@@ -165,7 +166,7 @@ func Test_ListCodeScanningAlerts(t *testing.T) {
165166
{
166167
name: "successful alerts listing",
167168
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
168-
"GET /repos/{owner}/{repo}/code-scanning/alerts": expectQueryParams(t, map[string]string{
169+
mock.GetReposCodeScanningAlertsByOwnerByRepo.Method + " " + mock.GetReposCodeScanningAlertsByOwnerByRepo.Pattern: expectQueryParams(t, map[string]string{
169170
"ref": "main",
170171
"state": "open",
171172
"severity": "high",
@@ -188,7 +189,7 @@ func Test_ListCodeScanningAlerts(t *testing.T) {
188189
{
189190
name: "alerts listing fails",
190191
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
191-
"GET /repos/{owner}/{repo}/code-scanning/alerts": http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
192+
mock.GetReposCodeScanningAlertsByOwnerByRepo.Method + " " + mock.GetReposCodeScanningAlertsByOwnerByRepo.Pattern: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
192193
w.WriteHeader(http.StatusUnauthorized)
193194
_, _ = w.Write([]byte(`{"message": "Unauthorized access"}`))
194195
}),

pkg/github/git_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/github/github-mcp-server/pkg/translations"
1212
"github.com/google/go-github/v79/github"
1313
"github.com/google/jsonschema-go/jsonschema"
14+
"github.com/migueleliasweb/go-github-mock/src/mock"
1415
"github.com/stretchr/testify/assert"
1516
"github.com/stretchr/testify/require"
1617
)
@@ -71,8 +72,8 @@ func Test_GetRepositoryTree(t *testing.T) {
7172
{
7273
name: "successfully get repository tree",
7374
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
74-
"GET /repos/{owner}/{repo}": mockResponse(t, http.StatusOK, mockRepo),
75-
"GET /repos/{owner}/{repo}/git/trees/{tree}": mockResponse(t, http.StatusOK, mockTree),
75+
mock.GetReposByOwnerByRepo.Method + " " + mock.GetReposByOwnerByRepo.Pattern: mockResponse(t, http.StatusOK, mockRepo),
76+
mock.GetReposGitTreesByOwnerByRepoByTreeSha.Method + " " + mock.GetReposGitTreesByOwnerByRepoByTreeSha.Pattern: mockResponse(t, http.StatusOK, mockTree),
7677
}),
7778
requestArgs: map[string]interface{}{
7879
"owner": "owner",
@@ -82,8 +83,8 @@ func Test_GetRepositoryTree(t *testing.T) {
8283
{
8384
name: "successfully get repository tree with path filter",
8485
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
85-
"GET /repos/{owner}/{repo}": mockResponse(t, http.StatusOK, mockRepo),
86-
"GET /repos/{owner}/{repo}/git/trees/{tree}": mockResponse(t, http.StatusOK, mockTree),
86+
mock.GetReposByOwnerByRepo.Method + " " + mock.GetReposByOwnerByRepo.Pattern: mockResponse(t, http.StatusOK, mockRepo),
87+
mock.GetReposGitTreesByOwnerByRepoByTreeSha.Method + " " + mock.GetReposGitTreesByOwnerByRepoByTreeSha.Pattern: mockResponse(t, http.StatusOK, mockTree),
8788
}),
8889
requestArgs: map[string]interface{}{
8990
"owner": "owner",
@@ -94,7 +95,7 @@ func Test_GetRepositoryTree(t *testing.T) {
9495
{
9596
name: "repository not found",
9697
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
97-
"GET /repos/{owner}/{repo}": http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
98+
mock.GetReposByOwnerByRepo.Method + " " + mock.GetReposByOwnerByRepo.Pattern: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
9899
w.WriteHeader(http.StatusNotFound)
99100
_, _ = w.Write([]byte(`{"message": "Not Found"}`))
100101
}),
@@ -109,8 +110,8 @@ func Test_GetRepositoryTree(t *testing.T) {
109110
{
110111
name: "tree not found",
111112
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
112-
"GET /repos/{owner}/{repo}": mockResponse(t, http.StatusOK, mockRepo),
113-
"GET /repos/{owner}/{repo}/git/trees/{tree}": http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
113+
mock.GetReposByOwnerByRepo.Method + " " + mock.GetReposByOwnerByRepo.Pattern: mockResponse(t, http.StatusOK, mockRepo),
114+
mock.GetReposGitTreesByOwnerByRepoByTreeSha.Method + " " + mock.GetReposGitTreesByOwnerByRepoByTreeSha.Pattern: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
114115
w.WriteHeader(http.StatusNotFound)
115116
_, _ = w.Write([]byte(`{"message": "Not Found"}`))
116117
}),

0 commit comments

Comments
 (0)