Skip to content
5 changes: 3 additions & 2 deletions pkg/github/repository_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,14 @@ func RepositoryResourceContentsHandler(resourceURITemplate *uritemplate.Template
}

resp, err := rawClient.GetRawContent(ctx, owner, repo, path, rawOpts)
if err != nil {
return nil, fmt.Errorf("failed to get raw content: %w", err)
}
defer func() {
_ = resp.Body.Close()
}()
// If the raw content is not found, we will fall back to the GitHub API (in case it is a directory)
switch {
case err != nil:
return nil, fmt.Errorf("failed to get raw content: %w", err)
case resp.StatusCode == http.StatusOK:
ext := filepath.Ext(path)
mimeType := resp.Header.Get("Content-Type")
Expand Down
57 changes: 57 additions & 0 deletions pkg/github/repository_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"context"
"errors"
"net/http"
"net/url"
"testing"
Expand All @@ -12,6 +13,15 @@
"github.com/stretchr/testify/require"
)

// errorTransport is a http.RoundTripper that always returns an error.
type errorTransport struct {
err error
}

func (t *errorTransport) RoundTrip(*http.Request) (*http.Response, error) {
return nil, t.err
}

type resourceResponseType int

const (
Expand Down Expand Up @@ -272,3 +282,50 @@
})
}
}

// Test_repositoryResourceContentsHandler_NetworkError tests that a network error
// during raw content fetch does not cause a panic (nil response body dereference).
func Test_repositoryResourceContentsHandler_NetworkError(t *testing.T) {
base, _ := url.Parse("https://raw.example.com/")
networkErr := errors.New("network error: connection refused")

httpClient := &http.Client{Transport: &errorTransport{err: networkErr}}
client := github.NewClient(httpClient)
mockRawClient := raw.NewClient(client, base)
handler := RepositoryResourceContentsHandler(stubGetClientFn(client), stubGetRawClientFn(mockRawClient), repositoryResourceContentURITemplate)

Check failure on line 295 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

too many arguments in call to RepositoryResourceContentsHandler

Check failure on line 295 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

undefined: stubGetRawClientFn

Check failure on line 295 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

undefined: stubGetClientFn

Check failure on line 295 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (macos-latest)

too many arguments in call to RepositoryResourceContentsHandler

Check failure on line 295 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (macos-latest)

undefined: stubGetRawClientFn

Check failure on line 295 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (macos-latest)

undefined: stubGetClientFn

Check failure on line 295 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (macos-latest)

too many arguments in call to RepositoryResourceContentsHandler

Check failure on line 295 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (macos-latest)

undefined: stubGetRawClientFn

Check failure on line 295 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (macos-latest)

undefined: stubGetClientFn

Check failure on line 295 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

too many arguments in call to RepositoryResourceContentsHandler

Check failure on line 295 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

undefined: stubGetRawClientFn

Check failure on line 295 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

undefined: stubGetClientFn

Check failure on line 295 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / lint

too many arguments in call to RepositoryResourceContentsHandler

Check failure on line 295 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: stubGetRawClientFn

Check failure on line 295 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: stubGetClientFn

request := &mcp.ReadResourceRequest{
Params: &mcp.ReadResourceParams{
URI: "repo://owner/repo/contents/README.md",
},
}

// This should not panic, even though the HTTP client returns an error
resp, err := handler(context.TODO(), request)
require.Error(t, err)
require.Nil(t, resp)
require.ErrorContains(t, err, "failed to get raw content")
}

func Test_GetRepositoryResourceContent(t *testing.T) {
mockRawClient := raw.NewClient(github.NewClient(nil), &url.URL{})
tmpl, _ := GetRepositoryResourceContent(nil, stubGetRawClientFn(mockRawClient), translations.NullTranslationHelper)

Check failure on line 312 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

undefined: translations

Check failure on line 312 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

too many arguments in call to GetRepositoryResourceContent

Check failure on line 312 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

undefined: stubGetRawClientFn

Check failure on line 312 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

assignment mismatch: 2 variables but GetRepositoryResourceContent returns 1 value

Check failure on line 312 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (macos-latest)

undefined: translations

Check failure on line 312 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (macos-latest)

too many arguments in call to GetRepositoryResourceContent

Check failure on line 312 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (macos-latest)

undefined: stubGetRawClientFn

Check failure on line 312 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (macos-latest)

assignment mismatch: 2 variables but GetRepositoryResourceContent returns 1 value

Check failure on line 312 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (macos-latest)

undefined: translations

Check failure on line 312 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (macos-latest)

too many arguments in call to GetRepositoryResourceContent

Check failure on line 312 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (macos-latest)

undefined: stubGetRawClientFn

Check failure on line 312 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (macos-latest)

assignment mismatch: 2 variables but GetRepositoryResourceContent returns 1 value

Check failure on line 312 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

undefined: translations

Check failure on line 312 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

too many arguments in call to GetRepositoryResourceContent

Check failure on line 312 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

undefined: stubGetRawClientFn

Check failure on line 312 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

assignment mismatch: 2 variables but GetRepositoryResourceContent returns 1 value

Check failure on line 312 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: translations

Check failure on line 312 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / lint

too many arguments in call to GetRepositoryResourceContent

Check failure on line 312 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: stubGetRawClientFn

Check failure on line 312 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / lint

assignment mismatch: 2 variables but GetRepositoryResourceContent returns 1 value
require.Equal(t, "repo://{owner}/{repo}/contents{/path*}", tmpl.URITemplate)
}

func Test_GetRepositoryResourceBranchContent(t *testing.T) {
mockRawClient := raw.NewClient(github.NewClient(nil), &url.URL{})
tmpl, _ := GetRepositoryResourceBranchContent(nil, stubGetRawClientFn(mockRawClient), translations.NullTranslationHelper)

Check failure on line 318 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

undefined: translations

Check failure on line 318 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

too many arguments in call to GetRepositoryResourceBranchContent

Check failure on line 318 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

undefined: stubGetRawClientFn

Check failure on line 318 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (macos-latest)

undefined: translations

Check failure on line 318 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (macos-latest)

too many arguments in call to GetRepositoryResourceBranchContent

Check failure on line 318 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (macos-latest)

undefined: stubGetRawClientFn

Check failure on line 318 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (macos-latest)

undefined: translations

Check failure on line 318 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (macos-latest)

too many arguments in call to GetRepositoryResourceBranchContent

Check failure on line 318 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (macos-latest)

undefined: stubGetRawClientFn

Check failure on line 318 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

undefined: translations

Check failure on line 318 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

too many arguments in call to GetRepositoryResourceBranchContent

Check failure on line 318 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

undefined: stubGetRawClientFn

Check failure on line 318 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: translations

Check failure on line 318 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / lint

too many arguments in call to GetRepositoryResourceBranchContent

Check failure on line 318 in pkg/github/repository_resource_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: stubGetRawClientFn
require.Equal(t, "repo://{owner}/{repo}/refs/heads/{branch}/contents{/path*}", tmpl.URITemplate)
}
func Test_GetRepositoryResourceCommitContent(t *testing.T) {
mockRawClient := raw.NewClient(github.NewClient(nil), &url.URL{})
tmpl, _ := GetRepositoryResourceCommitContent(nil, stubGetRawClientFn(mockRawClient), translations.NullTranslationHelper)
require.Equal(t, "repo://{owner}/{repo}/sha/{sha}/contents{/path*}", tmpl.URITemplate)
}

func Test_GetRepositoryResourceTagContent(t *testing.T) {
mockRawClient := raw.NewClient(github.NewClient(nil), &url.URL{})
tmpl, _ := GetRepositoryResourceTagContent(nil, stubGetRawClientFn(mockRawClient), translations.NullTranslationHelper)
require.Equal(t, "repo://{owner}/{repo}/refs/tags/{tag}/contents{/path*}", tmpl.URITemplate)
}